diff --git a/README.rdoc b/README.rdoc index 7a994ba9e..3a18f360b 100644 --- a/README.rdoc +++ b/README.rdoc @@ -154,18 +154,18 @@ you can do so with a simple environment variable, instead of editing the Usage: annotate [options] [model_file]* -d, --delete Remove annotations from all model files or the routes.rb file - -p, --position [before|after] Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s) - --pc, --position-in-class [before|after] + -p, --position [before|top|after|bottom] Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s) + --pc, --position-in-class [before|top|after|bottom] Place the annotations at the top (before) or the bottom (after) of the model file - --pf, --position-in-factory [before|after] + --pf, --position-in-factory [before|top|after|bottom] Place the annotations at the top (before) or the bottom (after) of any factory files - --px, --position-in-fixture [before|after] + --px, --position-in-fixture [before|top|after|bottom] Place the annotations at the top (before) or the bottom (after) of any fixture files - --pt, --position-in-test [before|after] + --pt, --position-in-test [before|top|after|bottom] Place the annotations at the top (before) or the bottom (after) of any test files - --pr, --position-in-routes [before|after] + --pr, --position-in-routes [before|top|after|bottom] Place the annotations at the top (before) or the bottom (after) of the routes.rb file - --ps, --position-in-serializer [before|after] + --ps, --position-in-serializer [before|top|after|bottom] Place the annotations at the top (before) or the bottom (after) of the serializer files -r, --routes Annotate routes.rb with the output of 'rake routes' -v, --version Show the current version of this gem diff --git a/TODO.rdoc b/TODO.rdoc index 026540ade..737004cf1 100644 --- a/TODO.rdoc +++ b/TODO.rdoc @@ -2,7 +2,6 @@ - clean up history - change default position back to "top" for all annotations -- add "top" and "bottom" as synonyms for "before" and "after" - change 'exclude' to 'only' (double negatives are not unconfusing) == TODO (proposed) diff --git a/bin/annotate b/bin/annotate index de2f55118..b4fb90f76 100755 --- a/bin/annotate +++ b/bin/annotate @@ -32,7 +32,7 @@ OptionParser.new do |opts| target[:task] = :remove_annotations end - opts.on('-p', '--position [before|after]', ['before', 'after'], + 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/routes file(s)") do |p| ENV['position'] = p [ @@ -42,37 +42,37 @@ OptionParser.new do |opts| end end - opts.on('--pc', '--position-in-class [before|after]', ['before', 'after'], + 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| ENV['position_in_class'] = p has_set_position['position_in_class'] = true end - opts.on('--pf', '--position-in-factory [before|after]', ['before', 'after'], + 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| ENV['position_in_factory'] = p has_set_position['position_in_factory'] = true end - opts.on('--px', '--position-in-fixture [before|after]', ['before', 'after'], + 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| ENV['position_in_fixture'] = p has_set_position['position_in_fixture'] = true end - opts.on('--pt', '--position-in-test [before|after]', ['before', 'after'], + 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| ENV['position_in_test'] = p has_set_position['position_in_test'] = true end - opts.on('--pr', '--position-in-routes [before|after]', ['before', 'after'], + 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| ENV['position_in_routes'] = p has_set_position['position_in_routes'] = true end - opts.on('--ps', '--position-in-serializer [before|after]', ['before', 'after'], + 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| ENV['position_in_serializer'] = p has_set_position['position_in_serializer'] = true diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 8ab707bed..90f2553aa 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -224,7 +224,7 @@ def get_index_info(klass, options={}) # === Options (opts) # :force:: whether to update the file even if it doesn't seem to need it. # :position_in_*:: where to place the annotated section in fixture or model file, - # :before or :after. Default is :before. + # :before, :top, :after or :bottom. Default is :before. # def annotate_one_file(file_name, info_block, position, options={}) if File.exist?(file_name) @@ -259,7 +259,7 @@ def annotate_one_file(file_name, info_block, position, options={}) old_content.sub!(encoding, '') old_content.sub!(PATTERN, '') - new_content = options[position].to_s == 'after' ? + new_content = %w(after bottom).include?(options[position].to_s) ? (encoding_header + (old_content.rstrip + "\n\n" + info_block)) : (encoding_header + info_block + "\n" + old_content) end diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index e71a93b34..501b59ba5 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -23,7 +23,7 @@ module AnnotateRoutes def self.do_annotations(options={}) return unless(routes_exists?) - position_after = options[:position_in_routes] != 'before' + position_after = ! %w(before top).include?(options[:position_in_routes]) routes_map = `rake routes`.split(/\n/, -1) diff --git a/spec/annotate/annotate_models_spec.rb b/spec/annotate/annotate_models_spec.rb index bc044d824..8cd298477 100644 --- a/spec/annotate/annotate_models_spec.rb +++ b/spec/annotate/annotate_models_spec.rb @@ -425,6 +425,16 @@ def encoding_comments_list_each expect(File.read(@model_file_name)).to eq("#{@schema_info}\n#{@file_content}") end + it "should put annotation before class if :position == 'top'" do + annotate_one_file :position => "top" + expect(File.read(@model_file_name)).to eq("#{@schema_info}\n#{@file_content}") + end + + it "should put annotation before class if :position => :top" do + annotate_one_file :position => :top + expect(File.read(@model_file_name)).to eq("#{@schema_info}\n#{@file_content}") + end + it "should put annotation after class if :position => 'after'" do annotate_one_file :position => 'after' expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}") @@ -435,6 +445,16 @@ def encoding_comments_list_each expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}") end + it "should put annotation after class if :position => 'bottom'" do + annotate_one_file :position => 'bottom' + expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}") + end + + it "should put annotation after class if :position => :bottom" do + annotate_one_file :position => :bottom + expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}") + end + describe "with existing annotation => :before" do before do annotate_one_file :position => :before