lib/annotate/annotate_models.rb in annotate-2.7.5 vs lib/annotate/annotate_models.rb in annotate-3.0.0
- old
+ new
@@ -157,17 +157,19 @@
File.join(root_directory, SERIALIZERS_TEST_DIR, "%MODEL_NAME%_serializer_spec.rb"),
File.join(root_directory, SERIALIZERS_SPEC_DIR, "%MODEL_NAME%_serializer_spec.rb")
]
end
- def files_by_pattern(root_directory, pattern_type)
+ def files_by_pattern(root_directory, pattern_type, options)
case pattern_type
when 'test' then test_files(root_directory)
when 'fixture' then fixture_files(root_directory)
when 'scaffold' then scaffold_files(root_directory)
when 'factory' then factory_files(root_directory)
when 'serializer' then serialize_files(root_directory)
+ when 'additional_file_patterns'
+ [options[:additional_file_patterns] || []].flatten
when 'controller'
[File.join(root_directory, CONTROLLER_DIR, "%PLURALIZED_MODEL_NAME%_controller.rb")]
when 'admin'
[File.join(root_directory, ACTIVEADMIN_DIR, "%MODEL_NAME%.rb")]
when 'helper'
@@ -175,18 +177,24 @@
else
[]
end
end
- def get_patterns(pattern_types = [])
+ def get_patterns(options, pattern_types = [])
current_patterns = []
root_dir.each do |root_directory|
Array(pattern_types).each do |pattern_type|
- current_patterns += files_by_pattern(root_directory, pattern_type)
+ patterns = files_by_pattern(root_directory, pattern_type, options)
+
+ current_patterns += if pattern_type.to_sym == :additional_file_patterns
+ patterns
+ else
+ patterns.map { |p| p.sub(/^[\/]*/, '') }
+ end
end
end
- current_patterns.map { |p| p.sub(/^[\/]*/, '') }
+ current_patterns
end
# Simple quoting for the default column value
def quote(value)
case value
@@ -579,12 +587,13 @@
false
end
end
def matched_types(options)
- types = MATCHED_TYPES
+ types = MATCHED_TYPES.dup
types << 'admin' if options[:active_admin] =~ TRUE_RE && !types.include?('admin')
+ types << 'additional_file_patterns' if options[:additional_file_patterns].present?
types
end
# Given the name of an ActiveRecord class, create a schema
@@ -632,12 +641,15 @@
exclusion_key = 'exclude_class'.to_sym
position_key = 'position_in_class'.to_sym
end
next if options[exclusion_key]
- get_patterns(key)
+
+ get_patterns(options, key)
.map { |f| resolve_filename(f, model_name, table_name) }
+ .map { |f| expand_glob_into_files(f) }
+ .flatten
.each do |f|
if annotate_one_file(f, info, position_key, options_with_position(options, position_key))
annotated << f
end
end
@@ -791,10 +803,14 @@
else
puts "Annotated (#{annotated.length}): #{annotated.join(', ')}"
end
end
+ def expand_glob_into_files(glob)
+ Dir.glob(glob)
+ end
+
def annotate_model_file(annotated, file, header, options)
begin
return false if /#{SKIP_ANNOTATION_PREFIX}.*/ =~ (File.exist?(file) ? File.read(file) : '')
klass = get_model_class(file)
do_annotate = klass &&
@@ -828,11 +844,11 @@
model_name = klass.name.underscore
table_name = klass.table_name
model_file_name = file
deannotated_klass = true if remove_annotation_of_file(model_file_name, options)
- get_patterns(matched_types(options))
+ get_patterns(options, matched_types(options))
.map { |f| resolve_filename(f, model_name, table_name) }
.each do |f|
if File.exist?(f)
remove_annotation_of_file(f, options)
deannotated_klass = true
@@ -908,10 +924,14 @@
end
def mb_chars_ljust(string, length)
string = string.to_s
padding = length - width(string)
- string + (' ' * padding)
+ if padding > 0
+ string + (' ' * padding)
+ else
+ string[0..length-1]
+ end
end
end
class BadModelFileError < LoadError
def to_s