Skip to content

Commit 31fe4c5

Browse files
committed
Do not annotate controllers, helpers and scaffolds by default (revert to behavior from previous version)
1 parent 64b1a34 commit 31fe4c5

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

lib/annotate.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
end
1515

1616
module Annotate
17+
TRUE_RE = /^(true|t|yes|y|1)$/i
18+
1719
##
1820
# The set of available options to customize the behavior of Annotate.
1921
#
@@ -60,7 +62,9 @@ def self.set_defaults(options = {})
6062
end
6163
end
6264

63-
TRUE_RE = /^(true|t|yes|y|1)$/i
65+
##
66+
# TODO: what is the difference between this and set_defaults?
67+
#
6468
def self.setup_options(options = {})
6569
POSITION_OPTIONS.each do |key|
6670
options[key] = fallback(ENV[key.to_s], ENV['position'], 'before')
@@ -86,6 +90,11 @@ def self.setup_options(options = {})
8690
options[:wrapper_open] ||= options[:wrapper]
8791
options[:wrapper_close] ||= options[:wrapper]
8892

93+
# These were added in 2.7.0 but so this is to revert to old behavior by default
94+
options[:exclude_scaffolds] = Annotate.true?(ENV.fetch('exclude_scaffolds', 'true'))
95+
options[:exclude_controllers] = Annotate.true?(ENV.fetch('exclude_controllers', 'true'))
96+
options[:exclude_helpers] = Annotate.true?(ENV.fetch('exclude_helpers', 'true'))
97+
8998
return options
9099
end
91100

lib/annotate/annotate_models.rb

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ def get_foreign_key_info(klass, options={})
320320
# a schema info block (a comment starting with "== Schema Information"), check if it
321321
# matches the block that is already there. If so, leave it be. If not, remove the old
322322
# info block and write a new one.
323-
# Returns true or false depending on whether the file was modified.
323+
#
324+
# == Returns:
325+
# true or false depending on whether the file was modified.
324326
#
325327
# === Options (opts)
326328
# :force<Symbol>:: whether to update the file even if it doesn't seem to need it.
@@ -394,8 +396,6 @@ def remove_annotation_of_file(file_name)
394396
# info block (basically a comment containing information
395397
# on the columns and their types) and put it at the front
396398
# of the model and fixture source files.
397-
# Returns true or false depending on whether the source
398-
# files were modified.
399399
#
400400
# === Options (opts)
401401
# :position_in_class<Symbol>:: where to place the annotated section in model file
@@ -411,35 +411,41 @@ def remove_annotation_of_file(file_name)
411411
# :exclude_controllers<Symbol>:: whether to skip modification of controller files
412412
# :exclude_helpers<Symbol>:: whether to skip modification of helper files
413413
#
414+
# == Returns:
415+
# an array of file names that were annotated.
416+
#
414417
def annotate(klass, file, header, options={})
415418
begin
416419
info = get_schema_info(klass, header, options)
417-
did_annotate = false
418420
model_name = klass.name.underscore
419421
table_name = klass.table_name
420422
model_file_name = File.join(file)
423+
annotated = []
421424

422425
if annotate_one_file(model_file_name, info, :position_in_class, options_with_position(options, :position_in_class))
423-
did_annotate = true
426+
annotated << model_file_name
424427
end
425428

426429
MATCHED_TYPES.each do |key|
427430
exclusion_key = "exclude_#{key.pluralize}".to_sym
428431
position_key = "position_in_#{key}".to_sym
429432

430433
unless options[exclusion_key]
431-
did_annotate = self.get_patterns(key).
434+
self.get_patterns(key).
432435
map { |f| resolve_filename(f, model_name, table_name) }.
433-
map { |f| annotate_one_file(f, info, position_key, options_with_position(options, position_key)) }.
434-
detect { |result| result } || did_annotate
436+
each { |f|
437+
if annotate_one_file(f, info, position_key, options_with_position(options, position_key))
438+
annotated << f
439+
end
440+
}
435441
end
436442
end
437-
438-
return did_annotate
439443
rescue Exception => e
440444
puts "Unable to annotate #{file}: #{e.message}"
441445
puts "\t" + e.backtrace.join("\n\t") if options[:trace]
442446
end
447+
448+
return annotated
443449
end
444450

445451
# position = :position_in_fixture or :position_in_class
@@ -537,9 +543,10 @@ def do_annotations(options={})
537543
self.root_dir = options[:root_dir] if options[:root_dir]
538544

539545
annotated = []
540-
get_model_files(options).each do |file|
541-
annotate_model_file(annotated, File.join(file), header, options)
546+
get_model_files(options).each do |path, filename|
547+
annotate_model_file(annotated, File.join(path, filename), header, options)
542548
end
549+
543550
if annotated.empty?
544551
puts "Model files unchanged."
545552
else
@@ -552,9 +559,7 @@ def annotate_model_file(annotated, file, header, options)
552559
return false if (/# -\*- SkipSchemaAnnotations.*/ =~ (File.exist?(file) ? File.read(file) : '') )
553560
klass = get_model_class(file)
554561
if klass && klass < ActiveRecord::Base && !klass.abstract_class? && klass.table_exists?
555-
if annotate(klass, file, header, options)
556-
annotated << file
557-
end
562+
annotated.concat(annotate(klass, file, header, options))
558563
end
559564
rescue BadModelFileError => e
560565
unless options[:ignore_unknown_models]

lib/generators/annotate/templates/auto_annotate_models.rake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ if Rails.env.development?
2424
'exclude_fixtures' => 'false',
2525
'exclude_factories' => 'false',
2626
'exclude_serializers' => 'false',
27-
'exclude_scaffolds' => 'false',
28-
'exclude_controllers' => 'false',
29-
'exclude_helpers' => 'false',
27+
'exclude_scaffolds' => 'true',
28+
'exclude_controllers' => 'true',
29+
'exclude_helpers' => 'true',
3030
'ignore_model_sub_dir' => 'false',
3131
'ignore_columns' => nil,
3232
'ignore_routes' => nil,

lib/tasks/annotate_models.rake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ task :annotate_models => :environment do
2929
options[:exclude_fixtures] = Annotate.true?(ENV['exclude_fixtures'])
3030
options[:exclude_serializers] = Annotate.true?(ENV['exclude_serializers'])
3131
options[:exclude_scaffolds] = Annotate.true?(ENV['exclude_scaffolds'])
32-
options[:exclude_controllers] = Annotate.true?(ENV['exclude_controllers'])
33-
options[:exclude_helpers] = Annotate.true?(ENV['exclude_helpers'])
32+
options[:exclude_controllers] = Annotate.true?(ENV.fetch('exclude_controllers', 'true'))
33+
options[:exclude_helpers] = Annotate.true?(ENV.fetch('exclude_helpers', 'true'))
3434
options[:ignore_model_sub_dir] = Annotate.true?(ENV['ignore_model_sub_dir'])
3535
options[:format_bare] = Annotate.true?(ENV['format_bare'])
3636
options[:format_rdoc] = Annotate.true?(ENV['format_rdoc'])

0 commit comments

Comments
 (0)