Skip to content

Commit

Permalink
Merge pull request #223 from kamilbielawski/add_top_and_bottom_positions
Browse files Browse the repository at this point in the history
Add top and bottom positions
  • Loading branch information
ctran committed Jan 3, 2015
2 parents 1f4fb0a + 3199544 commit cb331bb
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
14 changes: 7 additions & 7 deletions README.rdoc
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion TODO.rdoc
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions bin/annotate
Expand Up @@ -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
[
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/annotate/annotate_models.rb
Expand Up @@ -224,7 +224,7 @@ def get_index_info(klass, options={})
# === Options (opts)
# :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,
# :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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/annotate/annotate_routes.rb
Expand Up @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions spec/annotate/annotate_models_spec.rb
Expand Up @@ -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}")
Expand All @@ -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
Expand Down

0 comments on commit cb331bb

Please sign in to comment.