Skip to content

Commit

Permalink
Make it possible to annotate models and routes together (ctran#647)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
drwl authored and vfonic committed May 8, 2020
1 parent 3d5c268 commit 9d36391
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ To annotate all your models, tests, fixtures, and factories:

To annotate just your models, tests, and factories:

annotate --exclude fixtures
annotate --models --exclude fixtures

To annotate just your models:

annotate --exclude tests,fixtures,factories,serializers
annotate --models

To annotate routes.rb:

Expand Down Expand Up @@ -184,6 +184,7 @@ you can do so with a simple environment variable, instead of editing the
--wo, --wrapper-open STR Annotation wrapper opening.
--wc, --wrapper-close STR Annotation wrapper closing
-r, --routes Annotate routes.rb with the output of 'rake routes'
--models Annotate ActiveRecord models
-a, --active-admin Annotate active_admin models
-v, --version Show the current version of this gem
-m, --show-migration Include the migration version number in the annotation
Expand Down
4 changes: 2 additions & 2 deletions lib/annotate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Annotate
].freeze
OTHER_OPTIONS = [
:additional_file_patterns, :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close,
:wrapper, :routes, :hide_limit_column_types, :hide_default_column_types,
:wrapper, :routes, :models, :hide_limit_column_types, :hide_default_column_types,
:ignore_routes, :active_admin
].freeze
PATH_OPTIONS = [
Expand Down Expand Up @@ -115,7 +115,7 @@ def self.include_routes?
end

def self.include_models?
ENV['routes'] !~ TRUE_RE
ENV['models'] =~ TRUE_RE
end

def self.loaded_tasks=(val)
Expand Down
4 changes: 4 additions & 0 deletions lib/annotate/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
env['routes'] = 'true'
end

option_parser.on('--models', "Annotate routes.rb with the output of 'rake routes'") do
env['models'] = 'true'
end

option_parser.on('-a', '--active-admin', 'Annotate active_admin models') do
env['active_admin'] = 'true'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if Rails.env.development?
Annotate.set_defaults(
'additional_file_patterns' => [],
'routes' => 'false',
'models' => 'false',
'position_in_routes' => 'before',
'position_in_class' => 'before',
'position_in_test' => 'before',
Expand Down
11 changes: 11 additions & 0 deletions spec/lib/annotate/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ module Annotate # rubocop:disable Metrics/ModuleLength
end
end

%w[--models].each do |option|
describe option do
let(:env_key) { 'models' }
let(:set_value) { 'true' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
end

%w[-a --active-admin].each do |option|
describe option do
let(:env_key) { 'active_admin' }
Expand Down

0 comments on commit 9d36391

Please sign in to comment.