Commit cb331bbe by Cuong Tran

Merge pull request #223 from kamilbielawski/add_top_and_bottom_positions

Add top and bottom positions
parents 1f4fb0ad 31995449
...@@ -154,18 +154,18 @@ you can do so with a simple environment variable, instead of editing the ...@@ -154,18 +154,18 @@ you can do so with a simple environment variable, instead of editing the
Usage: annotate [options] [model_file]* Usage: annotate [options] [model_file]*
-d, --delete Remove annotations from all model files or the routes.rb 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) -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|after] --pc, --position-in-class [before|top|after|bottom]
Place the annotations at the top (before) or the bottom (after) of the model file 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 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 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 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 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 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' -r, --routes Annotate routes.rb with the output of 'rake routes'
-v, --version Show the current version of this gem -v, --version Show the current version of this gem
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
- clean up history - clean up history
- change default position back to "top" for all annotations - 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) - change 'exclude' to 'only' (double negatives are not unconfusing)
== TODO (proposed) == TODO (proposed)
......
...@@ -32,7 +32,7 @@ OptionParser.new do |opts| ...@@ -32,7 +32,7 @@ OptionParser.new do |opts|
target[:task] = :remove_annotations target[:task] = :remove_annotations
end 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| "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 ENV['position'] = p
[ [
...@@ -42,37 +42,37 @@ OptionParser.new do |opts| ...@@ -42,37 +42,37 @@ OptionParser.new do |opts|
end end
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| "Place the annotations at the top (before) or the bottom (after) of the model file") do |p|
ENV['position_in_class'] = p ENV['position_in_class'] = p
has_set_position['position_in_class'] = true has_set_position['position_in_class'] = true
end 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| "Place the annotations at the top (before) or the bottom (after) of any factory files") do |p|
ENV['position_in_factory'] = p ENV['position_in_factory'] = p
has_set_position['position_in_factory'] = true has_set_position['position_in_factory'] = true
end 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| "Place the annotations at the top (before) or the bottom (after) of any fixture files") do |p|
ENV['position_in_fixture'] = p ENV['position_in_fixture'] = p
has_set_position['position_in_fixture'] = true has_set_position['position_in_fixture'] = true
end 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| "Place the annotations at the top (before) or the bottom (after) of any test files") do |p|
ENV['position_in_test'] = p ENV['position_in_test'] = p
has_set_position['position_in_test'] = true has_set_position['position_in_test'] = true
end 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| "Place the annotations at the top (before) or the bottom (after) of the routes.rb file") do |p|
ENV['position_in_routes'] = p ENV['position_in_routes'] = p
has_set_position['position_in_routes'] = true has_set_position['position_in_routes'] = true
end 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| "Place the annotations at the top (before) or the bottom (after) of the serializer files") do |p|
ENV['position_in_serializer'] = p ENV['position_in_serializer'] = p
has_set_position['position_in_serializer'] = true has_set_position['position_in_serializer'] = true
......
...@@ -224,7 +224,7 @@ module AnnotateModels ...@@ -224,7 +224,7 @@ module AnnotateModels
# === Options (opts) # === Options (opts)
# :force<Symbol>:: whether to update the file even if it doesn't seem to need it. # :force<Symbol>:: whether to update the file even if it doesn't seem to need it.
# :position_in_*<Symbol>:: where to place the annotated section in fixture or model file, # :position_in_*<Symbol>:: 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={}) def annotate_one_file(file_name, info_block, position, options={})
if File.exist?(file_name) if File.exist?(file_name)
...@@ -259,7 +259,7 @@ module AnnotateModels ...@@ -259,7 +259,7 @@ module AnnotateModels
old_content.sub!(encoding, '') old_content.sub!(encoding, '')
old_content.sub!(PATTERN, '') 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 + (old_content.rstrip + "\n\n" + info_block)) :
(encoding_header + info_block + "\n" + old_content) (encoding_header + info_block + "\n" + old_content)
end end
......
...@@ -23,7 +23,7 @@ module AnnotateRoutes ...@@ -23,7 +23,7 @@ module AnnotateRoutes
def self.do_annotations(options={}) def self.do_annotations(options={})
return unless(routes_exists?) 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) routes_map = `rake routes`.split(/\n/, -1)
......
...@@ -425,6 +425,16 @@ end ...@@ -425,6 +425,16 @@ end
expect(File.read(@model_file_name)).to eq("#{@schema_info}\n#{@file_content}") expect(File.read(@model_file_name)).to eq("#{@schema_info}\n#{@file_content}")
end 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 it "should put annotation after class if :position => 'after'" do
annotate_one_file :position => 'after' annotate_one_file :position => 'after'
expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}") expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}")
...@@ -435,6 +445,16 @@ end ...@@ -435,6 +445,16 @@ end
expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}") expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}")
end 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 describe "with existing annotation => :before" do
before do before do
annotate_one_file :position => :before annotate_one_file :position => :before
......
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