Skip to content

Commit 9d36391

Browse files
drwlvfonic
authored andcommitted
Make it possible to annotate models and routes together (ctran#647)
Prior to this change, `Annotate.include_models?` returned the inverse of `Annotate.include_routes?`. This made it so annotating models and routes was not possible to do together. This PR adds an explicit `--models` flag and also adds it the option to `lib/generators/annotate/templates/auto_annotate_models.rake` with the default being set to `false`. Fixes ctran#563 and undoes the bug introduced in ctran#485.
1 parent 3d5c268 commit 9d36391

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

README.rdoc

+3-2
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ To annotate all your models, tests, fixtures, and factories:
8989

9090
To annotate just your models, tests, and factories:
9191

92-
annotate --exclude fixtures
92+
annotate --models --exclude fixtures
9393

9494
To annotate just your models:
9595

96-
annotate --exclude tests,fixtures,factories,serializers
96+
annotate --models
9797

9898
To annotate routes.rb:
9999

@@ -184,6 +184,7 @@ you can do so with a simple environment variable, instead of editing the
184184
--wo, --wrapper-open STR Annotation wrapper opening.
185185
--wc, --wrapper-close STR Annotation wrapper closing
186186
-r, --routes Annotate routes.rb with the output of 'rake routes'
187+
--models Annotate ActiveRecord models
187188
-a, --active-admin Annotate active_admin models
188189
-v, --version Show the current version of this gem
189190
-m, --show-migration Include the migration version number in the annotation

lib/annotate.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module Annotate
3737
].freeze
3838
OTHER_OPTIONS = [
3939
:additional_file_patterns, :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close,
40-
:wrapper, :routes, :hide_limit_column_types, :hide_default_column_types,
40+
:wrapper, :routes, :models, :hide_limit_column_types, :hide_default_column_types,
4141
:ignore_routes, :active_admin
4242
].freeze
4343
PATH_OPTIONS = [
@@ -115,7 +115,7 @@ def self.include_routes?
115115
end
116116

117117
def self.include_models?
118-
ENV['routes'] !~ TRUE_RE
118+
ENV['models'] =~ TRUE_RE
119119
end
120120

121121
def self.loaded_tasks=(val)

lib/annotate/parser.rb

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
123123
env['routes'] = 'true'
124124
end
125125

126+
option_parser.on('--models', "Annotate routes.rb with the output of 'rake routes'") do
127+
env['models'] = 'true'
128+
end
129+
126130
option_parser.on('-a', '--active-admin', 'Annotate active_admin models') do
127131
env['active_admin'] = 'true'
128132
end

lib/generators/annotate/templates/auto_annotate_models.rake

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ if Rails.env.development?
99
Annotate.set_defaults(
1010
'additional_file_patterns' => [],
1111
'routes' => 'false',
12+
'models' => 'false',
1213
'position_in_routes' => 'before',
1314
'position_in_class' => 'before',
1415
'position_in_test' => 'before',

spec/lib/annotate/parser_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,17 @@ module Annotate # rubocop:disable Metrics/ModuleLength
222222
end
223223
end
224224

225+
%w[--models].each do |option|
226+
describe option do
227+
let(:env_key) { 'models' }
228+
let(:set_value) { 'true' }
229+
it 'sets the ENV variable' do
230+
expect(ENV).to receive(:[]=).with(env_key, set_value)
231+
Parser.parse([option])
232+
end
233+
end
234+
end
235+
225236
%w[-a --active-admin].each do |option|
226237
describe option do
227238
let(:env_key) { 'active_admin' }

0 commit comments

Comments
 (0)