Skip to content

Commit 675b460

Browse files
committed
Support "routes" => "true" in auto_annotate_models.rake, #245
1 parent 4384721 commit 675b460

File tree

7 files changed

+107
-53
lines changed

7 files changed

+107
-53
lines changed

bin/annotate

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ require 'optparse'
1818
require 'annotate'
1919
Annotate.bootstrap_rake
2020

21-
target = {
22-
:klass => AnnotateModels,
23-
:task => :do_annotations,
24-
}
2521
has_set_position = {}
22+
target_action = :do_annotations
23+
2624
OptionParser.new do |opts|
2725
opts.banner = "Usage: annotate [options] [model_file]*"
2826

2927
opts.on('-d', '--delete',
3028
"Remove annotations from all model files or the routes.rb file") do
3129

32-
target[:task] = :remove_annotations
30+
target_action = :remove_annotations
3331
end
3432

3533
opts.on('-p', '--position [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
@@ -93,10 +91,7 @@ OptionParser.new do |opts|
9391

9492
opts.on('-r', '--routes',
9593
"Annotate routes.rb with the output of 'rake routes'") do
96-
target = {
97-
:klass => AnnotateRoutes,
98-
:task => :do_annotations
99-
}
94+
ENV['routes'] = 'true'
10095
end
10196

10297
opts.on('-v', '--version',
@@ -187,4 +182,6 @@ end.parse!
187182

188183
options = Annotate.setup_options({ :is_rake => ENV['is_rake'] && !ENV['is_rake'].empty? })
189184
Annotate.eager_load(options)
190-
target[:klass].send(target[:task], options)
185+
186+
AnnotateModels.send(target_action, options) if Annotate.include_models?
187+
AnnotateRoutes.send(target_action, options) if Annotate.include_routes?

lib/annotate.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Annotate
3030
:exclude_scaffolds, :exclude_controllers, :exclude_helpers
3131
]
3232
OTHER_OPTIONS=[
33-
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper
33+
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper, :routes
3434
]
3535
PATH_OPTIONS=[
3636
:require, :model_dir, :root_dir
@@ -98,6 +98,14 @@ def self.skip_on_migration?
9898
ENV['skip_on_db_migrate'] =~ TRUE_RE
9999
end
100100

101+
def self.include_routes?
102+
ENV['routes'] =~ TRUE_RE
103+
end
104+
105+
def self.include_models?
106+
true
107+
end
108+
101109
def self.loaded_tasks=(val); @loaded_tasks = val; end
102110
def self.loaded_tasks; return @loaded_tasks; end
103111

lib/annotate/annotate_models.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def do_annotations(options={})
530530
annotate_model_file(annotated, File.join(file), header, options)
531531
end
532532
if annotated.empty?
533-
puts "Nothing to annotate."
533+
puts "Model files unchanged."
534534
else
535535
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
536536
end
@@ -542,7 +542,7 @@ def annotate_model_file(annotated, file, header, options)
542542
klass = get_model_class(file)
543543
if klass && klass < ActiveRecord::Base && !klass.abstract_class? && klass.table_exists?
544544
if annotate(klass, file, header, options)
545-
annotated << klass
545+
annotated << file
546546
end
547547
end
548548
rescue Exception => e

lib/annotate/annotate_routes.rb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def self.do_annotations(options={})
3737
"#"
3838
] + routes_map.map { |line| "# #{line}".rstrip }
3939

40-
(content, where_header_found) = strip_annotations(File.read(routes_file))
40+
existing_text = File.read(routes_file)
41+
(content, where_header_found) = strip_annotations(existing_text)
4142
changed = where_header_found != 0 # This will either be :before, :after, or
4243
# a number. If the number is > 0, the
4344
# annotation was found somewhere in the
@@ -60,21 +61,25 @@ def self.do_annotations(options={})
6061

6162
content = position_after ? (content + header) : header + content
6263

63-
write_contents(content)
64-
65-
puts "Route file annotated."
64+
if write_contents(existing_text, content)
65+
puts "#{routes_file} annotated."
66+
else
67+
puts "#{routes_file} unchanged."
68+
end
6669
end
6770

6871
def self.remove_annotations(options={})
6972
return unless(routes_exists?)
70-
71-
(content, where_header_found) = strip_annotations(File.read(routes_file))
73+
existing_text = File.read(routes_file)
74+
(content, where_header_found) = strip_annotations(existing_text)
7275

7376
content = strip_on_removal(content, where_header_found)
7477

75-
write_contents(content)
76-
77-
puts "Removed annotations from routes file."
78+
if write_contents(existing_text, content)
79+
puts "Removed annotations from #{routes_file}."
80+
else
81+
puts "#{routes_file} unchanged."
82+
end
7883
end
7984

8085
protected
@@ -89,11 +94,15 @@ def self.routes_exists?
8994
return routes_exists
9095
end
9196

92-
def self.write_contents(content)
93-
content << '' unless(content.last == '') # Make sure we end on a trailing
94-
# newline.
97+
def self.write_contents(existing_text, new_content)
98+
# Make sure we end on a trailing newline.
99+
new_content << '' unless(new_content.last == '')
100+
new_text = new_content.join("\n")
101+
102+
return false if existing_text == new_text
95103

96-
File.open(routes_file, "wb") { |f| f.puts(content.join("\n")) }
104+
File.open(routes_file, "wb") { |f| f.puts(new_text) }
105+
return true
97106
end
98107

99108
def self.strip_annotations(content)

lib/generators/annotate/templates/auto_annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if Rails.env.development?
66
# You can override any of these by setting an environment variable of the
77
# same name.
88
Annotate.set_defaults(
9+
'routes' => 'false',
910
'position_in_routes' => 'before',
1011
'position_in_class' => 'before',
1112
'position_in_test' => 'before',

lib/tasks/migrate.rake

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,23 @@ module Annotate
2929
def self.update_annotations
3030
unless @@working || Annotate.skip_on_migration?
3131
@@working = true
32-
if Rake::Task.task_defined?("annotate_models")
33-
Rake::Task["annotate_models"].invoke
34-
elsif Rake::Task.task_defined?("app:annotate_models")
35-
Rake::Task["app:annotate_models"].invoke
36-
else
37-
raise "Don't know how to build task 'annotate_models'"
38-
end
32+
33+
self.update_models if Annotate.include_models?
34+
self.update_routes if Annotate.include_routes?
35+
end
36+
end
37+
38+
def self.update_models
39+
if Rake::Task.task_defined?("annotate_models")
40+
Rake::Task["annotate_models"].invoke
41+
elsif Rake::Task.task_defined?("app:annotate_models")
42+
Rake::Task["app:annotate_models"].invoke
43+
end
44+
end
45+
46+
def self.update_routes
47+
if Rake::Task.task_defined?("annotate_routes")
48+
Rake::Task["annotate_routes"].invoke
3949
end
4050
end
4151
end

spec/annotate/annotate_routes_spec.rb

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,114 @@
22
require 'annotate/annotate_routes'
33

44
describe AnnotateRoutes do
5+
ROUTE_FILE = "config/routes.rb"
6+
ANNOTATION_ADDED = "#{ROUTE_FILE} annotated."
7+
ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}."
8+
FILE_UNCHANGED = "#{ROUTE_FILE} unchanged."
59

610
def mock_file(stubs={})
711
@mock_file ||= double(File, stubs)
812
end
913

1014
it "should check if routes.rb exists" do
11-
expect(File).to receive(:exists?).with("config/routes.rb").and_return(false)
15+
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(false)
1216
expect(AnnotateRoutes).to receive(:puts).with("Can`t find routes.rb")
1317
AnnotateRoutes.do_annotations
1418
end
1519

16-
describe "When Annotating, with older Rake Versions" do
20+
describe "When adding" do
21+
before(:each) do
22+
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
23+
expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("")
24+
end
25+
26+
it "should insert annotations if file does not contain annotations" do
27+
expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
28+
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file)
29+
expect(@mock_file).to receive(:puts).with("\n# == Route Map\n#\n")
30+
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
31+
32+
AnnotateRoutes.do_annotations
33+
end
34+
35+
it "should skip annotations if file does already contain annotation" do
36+
expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n# == Route Map\n#\n")
37+
expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED)
38+
39+
AnnotateRoutes.do_annotations
40+
end
41+
42+
end
43+
44+
describe "When adding with older Rake versions" do
1745

1846
before(:each) do
19-
expect(File).to receive(:exists?).with("config/routes.rb").and_return(true)
47+
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
2048
expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("(in /bad/line)\ngood line")
21-
expect(File).to receive(:open).with("config/routes.rb", "wb").and_yield(mock_file)
22-
expect(AnnotateRoutes).to receive(:puts).with("Route file annotated.")
49+
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file)
50+
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
2351
end
2452

2553
it "should annotate and add a newline!" do
26-
expect(File).to receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo")
54+
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
2755
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
2856
AnnotateRoutes.do_annotations
2957
end
3058

3159
it "should not add a newline if there are empty lines" do
32-
expect(File).to receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo\n")
60+
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
3361
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
3462
AnnotateRoutes.do_annotations
3563
end
3664

3765
end
3866

39-
describe "When Annotating, with newer Rake Versions" do
67+
describe "When adding with newer Rake versions" do
4068

4169
before(:each) do
42-
expect(File).to receive(:exists?).with("config/routes.rb").and_return(true)
70+
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
4371
expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("another good line\ngood line")
44-
expect(File).to receive(:open).with("config/routes.rb", "wb").and_yield(mock_file)
45-
expect(AnnotateRoutes).to receive(:puts).with("Route file annotated.")
72+
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file)
73+
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
4674
end
4775

76+
4877
it "should annotate and add a newline!" do
49-
expect(File).to receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo")
78+
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
5079
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
5180
AnnotateRoutes.do_annotations
5281
end
5382

5483
it "should not add a newline if there are empty lines" do
55-
expect(File).to receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo\n")
84+
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
5685
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
5786
AnnotateRoutes.do_annotations
5887
end
5988

6089
it "should add a timestamp when :timestamp is passed" do
61-
expect(File).to receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo")
90+
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
6291
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/)
6392
AnnotateRoutes.do_annotations :timestamp => true
6493
end
6594

6695
end
6796

68-
describe "When Removing Annotation" do
97+
describe "When removing" do
6998

7099
before(:each) do
71-
expect(File).to receive(:exists?).with("config/routes.rb").and_return(true)
72-
expect(File).to receive(:open).with("config/routes.rb", "wb").and_yield(mock_file)
73-
expect(AnnotateRoutes).to receive(:puts).with("Removed annotations from routes file.")
100+
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
101+
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file)
102+
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED)
74103
end
75104

76105
it "should remove trailing annotation and trim trailing newlines, but leave leading newlines alone" do
77-
expect(File).to receive(:read).with("config/routes.rb").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")
106+
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")
78107
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/)
79108
AnnotateRoutes.remove_annotations
80109
end
81110

82111
it "should remove prepended annotation and trim leading newlines, but leave trailing newlines alone" do
83-
expect(File).to receive(:read).with("config/routes.rb").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")
112+
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")
84113
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n/)
85114
AnnotateRoutes.remove_annotations
86115
end

0 commit comments

Comments
 (0)