Skip to content

Commit cb331bb

Browse files
committed
Merge pull request #223 from kamilbielawski/add_top_and_bottom_positions
Add top and bottom positions
2 parents 1f4fb0a + 3199544 commit cb331bb

File tree

6 files changed

+37
-18
lines changed

6 files changed

+37
-18
lines changed

README.rdoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,18 @@ you can do so with a simple environment variable, instead of editing the
154154

155155
Usage: annotate [options] [model_file]*
156156
-d, --delete Remove annotations from all model files or the routes.rb file
157-
-p, --position [before|after] Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)
158-
--pc, --position-in-class [before|after]
157+
-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)
158+
--pc, --position-in-class [before|top|after|bottom]
159159
Place the annotations at the top (before) or the bottom (after) of the model file
160-
--pf, --position-in-factory [before|after]
160+
--pf, --position-in-factory [before|top|after|bottom]
161161
Place the annotations at the top (before) or the bottom (after) of any factory files
162-
--px, --position-in-fixture [before|after]
162+
--px, --position-in-fixture [before|top|after|bottom]
163163
Place the annotations at the top (before) or the bottom (after) of any fixture files
164-
--pt, --position-in-test [before|after]
164+
--pt, --position-in-test [before|top|after|bottom]
165165
Place the annotations at the top (before) or the bottom (after) of any test files
166-
--pr, --position-in-routes [before|after]
166+
--pr, --position-in-routes [before|top|after|bottom]
167167
Place the annotations at the top (before) or the bottom (after) of the routes.rb file
168-
--ps, --position-in-serializer [before|after]
168+
--ps, --position-in-serializer [before|top|after|bottom]
169169
Place the annotations at the top (before) or the bottom (after) of the serializer files
170170
-r, --routes Annotate routes.rb with the output of 'rake routes'
171171
-v, --version Show the current version of this gem

TODO.rdoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
- clean up history
44
- change default position back to "top" for all annotations
5-
- add "top" and "bottom" as synonyms for "before" and "after"
65
- change 'exclude' to 'only' (double negatives are not unconfusing)
76

87
== TODO (proposed)

bin/annotate

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ OptionParser.new do |opts|
3232
target[:task] = :remove_annotations
3333
end
3434

35-
opts.on('-p', '--position [before|after]', ['before', 'after'],
35+
opts.on('-p', '--position [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
3636
"Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)") do |p|
3737
ENV['position'] = p
3838
[
@@ -42,37 +42,37 @@ OptionParser.new do |opts|
4242
end
4343
end
4444

45-
opts.on('--pc', '--position-in-class [before|after]', ['before', 'after'],
45+
opts.on('--pc', '--position-in-class [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
4646
"Place the annotations at the top (before) or the bottom (after) of the model file") do |p|
4747
ENV['position_in_class'] = p
4848
has_set_position['position_in_class'] = true
4949
end
5050

51-
opts.on('--pf', '--position-in-factory [before|after]', ['before', 'after'],
51+
opts.on('--pf', '--position-in-factory [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
5252
"Place the annotations at the top (before) or the bottom (after) of any factory files") do |p|
5353
ENV['position_in_factory'] = p
5454
has_set_position['position_in_factory'] = true
5555
end
5656

57-
opts.on('--px', '--position-in-fixture [before|after]', ['before', 'after'],
57+
opts.on('--px', '--position-in-fixture [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
5858
"Place the annotations at the top (before) or the bottom (after) of any fixture files") do |p|
5959
ENV['position_in_fixture'] = p
6060
has_set_position['position_in_fixture'] = true
6161
end
6262

63-
opts.on('--pt', '--position-in-test [before|after]', ['before', 'after'],
63+
opts.on('--pt', '--position-in-test [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
6464
"Place the annotations at the top (before) or the bottom (after) of any test files") do |p|
6565
ENV['position_in_test'] = p
6666
has_set_position['position_in_test'] = true
6767
end
6868

69-
opts.on('--pr', '--position-in-routes [before|after]', ['before', 'after'],
69+
opts.on('--pr', '--position-in-routes [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
7070
"Place the annotations at the top (before) or the bottom (after) of the routes.rb file") do |p|
7171
ENV['position_in_routes'] = p
7272
has_set_position['position_in_routes'] = true
7373
end
7474

75-
opts.on('--ps', '--position-in-serializer [before|after]', ['before', 'after'],
75+
opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
7676
"Place the annotations at the top (before) or the bottom (after) of the serializer files") do |p|
7777
ENV['position_in_serializer'] = p
7878
has_set_position['position_in_serializer'] = true

lib/annotate/annotate_models.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def get_index_info(klass, options={})
224224
# === Options (opts)
225225
# :force<Symbol>:: whether to update the file even if it doesn't seem to need it.
226226
# :position_in_*<Symbol>:: where to place the annotated section in fixture or model file,
227-
# :before or :after. Default is :before.
227+
# :before, :top, :after or :bottom. Default is :before.
228228
#
229229
def annotate_one_file(file_name, info_block, position, options={})
230230
if File.exist?(file_name)
@@ -259,7 +259,7 @@ def annotate_one_file(file_name, info_block, position, options={})
259259
old_content.sub!(encoding, '')
260260
old_content.sub!(PATTERN, '')
261261

262-
new_content = options[position].to_s == 'after' ?
262+
new_content = %w(after bottom).include?(options[position].to_s) ?
263263
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
264264
(encoding_header + info_block + "\n" + old_content)
265265
end

lib/annotate/annotate_routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module AnnotateRoutes
2323
def self.do_annotations(options={})
2424
return unless(routes_exists?)
2525

26-
position_after = options[:position_in_routes] != 'before'
26+
position_after = ! %w(before top).include?(options[:position_in_routes])
2727

2828
routes_map = `rake routes`.split(/\n/, -1)
2929

spec/annotate/annotate_models_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,16 @@ def encoding_comments_list_each
425425
expect(File.read(@model_file_name)).to eq("#{@schema_info}\n#{@file_content}")
426426
end
427427

428+
it "should put annotation before class if :position == 'top'" do
429+
annotate_one_file :position => "top"
430+
expect(File.read(@model_file_name)).to eq("#{@schema_info}\n#{@file_content}")
431+
end
432+
433+
it "should put annotation before class if :position => :top" do
434+
annotate_one_file :position => :top
435+
expect(File.read(@model_file_name)).to eq("#{@schema_info}\n#{@file_content}")
436+
end
437+
428438
it "should put annotation after class if :position => 'after'" do
429439
annotate_one_file :position => 'after'
430440
expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}")
@@ -435,6 +445,16 @@ def encoding_comments_list_each
435445
expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}")
436446
end
437447

448+
it "should put annotation after class if :position => 'bottom'" do
449+
annotate_one_file :position => 'bottom'
450+
expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}")
451+
end
452+
453+
it "should put annotation after class if :position => :bottom" do
454+
annotate_one_file :position => :bottom
455+
expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}")
456+
end
457+
438458
describe "with existing annotation => :before" do
439459
before do
440460
annotate_one_file :position => :before

0 commit comments

Comments
 (0)