Skip to content

Commit 40c44a8

Browse files
ethnextctran
authored andcommitted
add option to exclude STI subclasses from annotation (#365)
* add option to exclude STI subclasses from annotation
1 parent 3b41216 commit 40c44a8

File tree

6 files changed

+13
-4
lines changed

6 files changed

+13
-4
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Metrics/MethodLength:
105105
# Offense count: 2
106106
# Configuration parameters: CountComments.
107107
Metrics/ModuleLength:
108-
Max: 517
108+
Max: 522
109109

110110
# Offense count: 7
111111
Metrics/PerceivedComplexity:

lib/annotate.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module Annotate
2929
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
3030
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :trace,
3131
:timestamp, :exclude_serializers, :classified_sort, :show_foreign_keys,
32-
:exclude_scaffolds, :exclude_controllers, :exclude_helpers, :ignore_unknown_models
32+
:exclude_scaffolds, :exclude_controllers, :exclude_helpers,
33+
:exclude_sti_subclasses, :ignore_unknown_models
3334
].freeze
3435
OTHER_OPTIONS = [
3536
:ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, :wrapper, :routes,

lib/annotate/annotate_models.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ def matched_types(options)
453453
# :exclude_scaffolds<Symbol>:: whether to skip modification of scaffold files
454454
# :exclude_controllers<Symbol>:: whether to skip modification of controller files
455455
# :exclude_helpers<Symbol>:: whether to skip modification of helper files
456+
# :exclude_sti_subclasses<Symbol>:: whether to skip modification of files for STI subclasses
456457
#
457458
# == Returns:
458459
# an array of file names that were annotated.
@@ -609,7 +610,12 @@ def annotate_model_file(annotated, file, header, options)
609610
begin
610611
return false if /# -\*- SkipSchemaAnnotations.*/ =~ (File.exist?(file) ? File.read(file) : '')
611612
klass = get_model_class(file)
612-
if klass && klass < ActiveRecord::Base && !klass.abstract_class? && klass.table_exists?
613+
do_annotate = klass &&
614+
klass < ActiveRecord::Base &&
615+
(!options[:exclude_sti_subclasses] || !(klass.superclass < ActiveRecord::Base && klass.table_name == klass.superclass.table_name)) &&
616+
!klass.abstract_class? &&
617+
klass.table_exists?
618+
if do_annotate
613619
annotated.concat(annotate(klass, file, header, options))
614620
end
615621
rescue BadModelFileError => e

lib/generators/annotate/templates/auto_annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ if Rails.env.development?
2727
'exclude_scaffolds' => 'true',
2828
'exclude_controllers' => 'true',
2929
'exclude_helpers' => 'true',
30+
'exclude_sti_subclasses' => 'false',
3031
'ignore_model_sub_dir' => 'false',
3132
'ignore_columns' => nil,
3233
'ignore_routes' => nil,

lib/tasks/annotate_models.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ task annotate_models: :environment do
3131
options[:exclude_scaffolds] = Annotate.true?(ENV['exclude_scaffolds'])
3232
options[:exclude_controllers] = Annotate.true?(ENV.fetch('exclude_controllers', 'true'))
3333
options[:exclude_helpers] = Annotate.true?(ENV.fetch('exclude_helpers', 'true'))
34+
options[:exclude_sti_subclasses] = Annotate.true?(ENV['exclude_sti_subclasses'])
3435
options[:ignore_model_sub_dir] = Annotate.true?(ENV['ignore_model_sub_dir'])
3536
options[:format_bare] = Annotate.true?(ENV['format_bare'])
3637
options[:format_rdoc] = Annotate.true?(ENV['format_rdoc'])

spec/annotate/annotate_models_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ class Foo < ActiveRecord::Base; end;
905905
after { Object.send :remove_const, 'Foo' }
906906

907907
it 'skips attempt to annotate if no table exists for model' do
908-
annotate_model_file = AnnotateModels.annotate_model_file([], 'foo.rb', nil, nil)
908+
annotate_model_file = AnnotateModels.annotate_model_file([], 'foo.rb', nil, {})
909909

910910
expect(annotate_model_file).to eq nil
911911
end

0 commit comments

Comments
 (0)