Fixed some offenses.

parent d3aebd58
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-01-18 16:32:43 +0100 using RuboCop version 0.36.0.
# on 2016-01-19 22:54:29 +0100 using RuboCop version 0.36.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
......@@ -19,11 +19,10 @@ Lint/Debugger:
Exclude:
- 'spec/integration/common_validation.rb'
# Offense count: 8
# Offense count: 7
# Cop supports --auto-correct.
Lint/DeprecatedClassMethods:
Exclude:
- 'lib/annotate.rb'
- 'lib/annotate/annotate_routes.rb'
- 'spec/fixtures/rails32_boot.rb'
- 'spec/integration/rails_3.2.2/config/boot.rb'
......@@ -379,7 +378,7 @@ Style/IfInsideElse:
Exclude:
- 'lib/annotate/annotate_routes.rb'
# Offense count: 7
# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: MaxLineLength.
Style/IfUnlessModifier:
......@@ -635,7 +634,7 @@ Style/SpaceAfterComma:
Exclude:
- 'lib/annotate/annotate_models.rb'
# Offense count: 58
# Offense count: 50
# Cop supports --auto-correct.
Style/SpaceAfterControlKeyword:
Exclude:
......@@ -720,14 +719,14 @@ Style/SpaceInsideStringInterpolation:
Style/SpecialGlobalVars:
EnforcedStyle: use_perl_names
# Offense count: 403
# Offense count: 373
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
Enabled: false
# Offense count: 3
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: single_quotes, double_quotes
......
#!/usr/bin/env ruby
unless File.exists?('./Rakefile') || File.exists?('./Gemfile')
abort "Please run annotate from the root of the project."
unless File.exist?('./Rakefile') || File.exist?('./Gemfile')
abort 'Please run annotate from the root of the project.'
end
require 'rubygems'
......@@ -20,58 +20,55 @@ Annotate.bootstrap_rake
has_set_position = {}
target_action = :do_annotations
positions = %w(before top after bottom)
OptionParser.new do |opts|
opts.banner = "Usage: annotate [options] [model_file]*"
opts.on('-d', '--delete',
"Remove annotations from all model files or the routes.rb file") do
opts.banner = 'Usage: annotate [options] [model_file]*'
opts.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do
target_action = :remove_annotations
end
opts.on('-p', '--position [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
"Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)") do |p|
opts.on('-p', '--position [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p|
ENV['position'] = p
[
'position_in_class','position_in_factory','position_in_fixture','position_in_test', 'position_in_routes', 'position_in_serializer'
].each do |key|
%w(position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer).each do |key|
ENV[key] = p unless(has_set_position[key])
end
end
opts.on('--pc', '--position-in-class [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
"Place the annotations at the top (before) or the bottom (after) of the model file") do |p|
opts.on('--pc', '--position-in-class [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the model file') do |p|
ENV['position_in_class'] = p
has_set_position['position_in_class'] = true
end
opts.on('--pf', '--position-in-factory [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
"Place the annotations at the top (before) or the bottom (after) of any factory files") do |p|
opts.on('--pf', '--position-in-factory [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |p|
ENV['position_in_factory'] = p
has_set_position['position_in_factory'] = true
end
opts.on('--px', '--position-in-fixture [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
"Place the annotations at the top (before) or the bottom (after) of any fixture files") do |p|
opts.on('--px', '--position-in-fixture [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |p|
ENV['position_in_fixture'] = p
has_set_position['position_in_fixture'] = true
end
opts.on('--pt', '--position-in-test [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
"Place the annotations at the top (before) or the bottom (after) of any test files") do |p|
opts.on('--pt', '--position-in-test [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of any test files') do |p|
ENV['position_in_test'] = p
has_set_position['position_in_test'] = true
end
opts.on('--pr', '--position-in-routes [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
"Place the annotations at the top (before) or the bottom (after) of the routes.rb file") do |p|
opts.on('--pr', '--position-in-routes [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |p|
ENV['position_in_routes'] = p
has_set_position['position_in_routes'] = true
end
opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
"Place the annotations at the top (before) or the bottom (after) of the serializer files") do |p|
opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |p|
ENV['position_in_serializer'] = p
has_set_position['position_in_serializer'] = true
end
......@@ -89,34 +86,32 @@ OptionParser.new do |opts|
ENV['wrapper_close'] = p
end
opts.on('-r', '--routes',
"Annotate routes.rb with the output of 'rake routes'") do
opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do
ENV['routes'] = 'true'
end
opts.on('-v', '--version',
"Show the current version of this gem") do
'Show the current version of this gem') do
puts "annotate v#{Annotate.version}"; exit
end
opts.on('-m', '--show-migration',
"Include the migration version number in the annotation") do
ENV['include_version'] = "yes"
opts.on('-m', '--show-migration', 'Include the migration version number in the annotation') do
ENV['include_version'] = 'yes'
end
opts.on('-k', '--show-foreign-keys',
"List the table's foreign key constraints in the annotation") do
ENV['show_foreign_keys'] = "yes"
ENV['show_foreign_keys'] = 'yes'
end
opts.on('-i', '--show-indexes',
"List the table's database indexes in the annotation") do
ENV['show_indexes'] = "yes"
ENV['show_indexes'] = 'yes'
end
opts.on('-s', '--simple-indexes',
"Concat the column's related indexes in the annotation") do
ENV['simple_indexes'] = "yes"
ENV['simple_indexes'] = 'yes'
end
opts.on('--model-dir dir',
......@@ -131,17 +126,17 @@ OptionParser.new do |opts|
opts.on('--ignore-model-subdirects',
"Ignore subdirectories of the models directory") do |dir|
ENV['ignore_model_sub_dir'] = "yes"
ENV['ignore_model_sub_dir'] = 'yes'
end
opts.on('--sort',
"Sort columns alphabetically, rather than in creation order") do |dir|
ENV['sort'] = "yes"
ENV['sort'] = 'yes'
end
opts.on('--classified-sort',
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |dir|
ENV['classified_sort'] = "yes"
ENV['classified_sort'] = 'yes'
end
opts.on('-R', '--require path',
......@@ -155,10 +150,10 @@ OptionParser.new do |opts|
opts.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
exclusions ||= %w(tests fixtures factories)
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" }
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = 'yes' }
end
opts.on('-f', '--format [bare|rdoc|markdown]', ['bare', 'rdoc', 'markdown'], 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
opts.on('-f', '--format [bare|rdoc|markdown]', %w(bare rdoc markdown), 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
ENV["format_#{fmt}"] = 'yes'
end
......@@ -186,8 +181,8 @@ OptionParser.new do |opts|
ENV['hide_limit_column_types'] = "#{values}"
end
opts.on('--ignore-unknown-models', "don't display warnings for bad model files" ) do |values|
ENV['ignore_unknown_models'] = "true"
opts.on('--ignore-unknown-models', "don't display warnings for bad model files" ) do |_values|
ENV['ignore_unknown_models'] = 'true'
end
end.parse!
......
......@@ -163,11 +163,9 @@ module Annotate
end
require 'rake'
if File.exists?('./Rakefile')
load './Rakefile'
end
load './Rakefile' if File.exist?('./Rakefile')
Rake::Task[:environment].invoke rescue nil
if(!defined?(Rails))
if !defined?(Rails)
# Not in a Rails project, so time to load up the parts of
# ActiveSupport we need.
require 'active_support'
......
......@@ -18,7 +18,7 @@
# Released under the same license as Ruby. No Support. No Warranty.
#
module AnnotateRoutes
PREFIX = "# == Route Map"
PREFIX = '# == Route Map'
def self.do_annotations(options={})
return unless(routes_exists?)
......@@ -37,8 +37,8 @@ module AnnotateRoutes
routes_map.reject!{|line| line.match(/#{options[:ignore_routes]}/)} if options[:ignore_routes]
header = [
"#{PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime("%Y-%m-%d %H:%M")})" : ""),
"#"
"#{PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : ''),
'#'
] + routes_map.map { |line| "# #{line}".rstrip }
existing_text = File.read(routes_file)
......@@ -49,7 +49,7 @@ module AnnotateRoutes
# middle of the file. If the number is
# zero, no annotation was found.
if(position_after)
if position_after
# Ensure we have adequate trailing newlines at the end of the file to
# ensure a blank line separating the content from the annotation.
content << '' if(content.last != '')
......@@ -89,12 +89,12 @@ module AnnotateRoutes
protected
def self.routes_file
@routes_rb ||= File.join("config", "routes.rb")
@routes_rb ||= File.join('config', 'routes.rb')
end
def self.routes_exists?
routes_exists = File.exists?(routes_file)
puts "Can`t find routes.rb" if(!routes_exists)
puts "Can't find routes.rb" if(!routes_exists)
return routes_exists
end
......@@ -105,7 +105,7 @@ protected
return false if existing_text == new_text
File.open(routes_file, "wb") { |f| f.puts(new_text) }
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
return true
end
......@@ -117,13 +117,13 @@ protected
content.split(/\n/, -1).each do |line|
line_number += 1
begin
if(mode == :header)
if(line !~ /\s*#/)
if mode == :header
if line !~ /\s*#/
mode = :content
raise unless (line == '')
end
elsif(mode == :content)
if(line =~ /^\s*#\s*== Route.*$/)
elsif mode == :content
if line =~ /^\s*#\s*== Route.*$/
header_found_at = line_number
mode = :header
else
......@@ -147,9 +147,9 @@ protected
end
def self.strip_on_removal(content, where_header_found)
if(where_header_found == :before)
if where_header_found == :before
content.shift while(content.first == '')
elsif(where_header_found == :after)
elsif where_header_found == :after
content.pop while(content.last == '')
end
# TODO: If the user buried it in the middle, we should probably see about
......
......@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper.rb'
require 'annotate/annotate_routes'
describe AnnotateRoutes do
ROUTE_FILE = "config/routes.rb"
ROUTE_FILE = 'config/routes.rb'
ANNOTATION_ADDED = "#{ROUTE_FILE} annotated."
ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}."
FILE_UNCHANGED = "#{ROUTE_FILE} unchanged."
......@@ -11,28 +11,28 @@ describe AnnotateRoutes do
@mock_file ||= double(File, stubs)
end
it "should check if routes.rb exists" do
it 'should check if routes.rb exists' do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(false)
expect(AnnotateRoutes).to receive(:puts).with("Can`t find routes.rb")
expect(AnnotateRoutes).to receive(:puts).with("Can't find routes.rb")
AnnotateRoutes.do_annotations
end
describe "When adding" do
describe 'When adding' do
before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("")
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return('')
end
it "should insert annotations if file does not contain annotations" do
it 'should insert annotations if file does not contain annotations' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file)
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(@mock_file).to receive(:puts).with("\n# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
AnnotateRoutes.do_annotations
end
it "should skip annotations if file does already contain annotation" do
it 'should skip annotations if file does already contain annotation' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED)
......@@ -41,22 +41,22 @@ describe AnnotateRoutes do
end
describe "When adding with older Rake versions" do
describe 'When adding with older Rake versions' do
before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("(in /bad/line)\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file)
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("(in /bad/line)\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
end
it "should annotate and add a newline!" do
it 'should annotate and add a newline!' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
AnnotateRoutes.do_annotations
end
it "should not add a newline if there are empty lines" do
it 'should not add a newline if there are empty lines' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
AnnotateRoutes.do_annotations
......@@ -64,29 +64,29 @@ describe AnnotateRoutes do
end
describe "When adding with newer Rake versions" do
describe 'When adding with newer Rake versions' do
before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("another good line\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file)
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
end
it "should annotate and add a newline!" do
it 'should annotate and add a newline!' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations
end
it "should not add a newline if there are empty lines" do
it 'should not add a newline if there are empty lines' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations
end
it "should add a timestamp when :timestamp is passed" do
it 'should add a timestamp when :timestamp is passed' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations :timestamp => true
......@@ -94,21 +94,21 @@ describe AnnotateRoutes do
end
describe "When removing" do
describe 'When removing' do
before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file)
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED)
end
it "should remove trailing annotation and trim trailing newlines, but leave leading newlines alone" do
it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n# == Route Map\n#\n# another good line\n# good line\n")
expect(@mock_file).to receive(:puts).with(/\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n/)
AnnotateRoutes.remove_annotations
end
it "should remove prepended annotation and trim leading newlines, but leave trailing newlines alone" do
it 'should remove prepended annotation and trim leading newlines, but leave trailing newlines alone' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("# == Route Map\n#\n# another good line\n# good line\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n/)
AnnotateRoutes.remove_annotations
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment