lib/annotate/annotate_models.rb in annotate-2.6.1 vs lib/annotate/annotate_models.rb in annotate-2.6.2

- old
+ new

@@ -29,12 +29,12 @@ # Fabrication https://github.com/paulelliott/fabrication.git FABRICATORS_TEST_DIR = File.join("test", "fabricators") FABRICATORS_SPEC_DIR = File.join("spec", "fabricators") TEST_PATTERNS = [ - [UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"], - [SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb"], + File.join(UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"), + File.join(SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb"), ] FIXTURE_PATTERNS = [ File.join(FIXTURE_TEST_DIR, "%TABLE_NAME%.yml"), File.join(FIXTURE_SPEC_DIR, "%TABLE_NAME%.yml"), @@ -129,10 +129,15 @@ col_type << "(#{col.limit})" unless NO_LIMIT_COL_TYPES.include?(col_type) end end end + # Check out if we got an array column + if col.respond_to?(:array) && col.array + attrs << "is an Array" + end + # Check out if we got a geometric column # and print the type and SRID if col.respond_to?(:geometry_type) attrs << "#{col.geometry_type}, #{col.srid}" elsif col.respond_to?(:geometric_type) and col.geometric_type.present? @@ -225,32 +230,28 @@ encoding_header = old_content.match(encoding).to_s if old_columns == new_columns && !options[:force] return false else + # Replace inline the old schema info with the new schema info + new_content = old_content.sub(PATTERN, info_block + "\n") -# todo: figure out if we need to extract any logic from this merge chunk -# <<<<<<< HEAD -# # Replace the old schema info with the new schema info -# new_content = old_content.sub(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n*/, info_block) -# # But, if there *was* no old schema info, we simply need to insert it -# if new_content == old_content -# old_content.sub!(encoding, '') -# new_content = options[:position] == 'after' ? -# (encoding_header + (old_content =~ /\n$/ ? old_content : old_content + "\n") + info_block) : -# (encoding_header + info_block + old_content) -# end -# ======= + if new_content.end_with? (info_block + "\n") + new_content = old_content.sub(PATTERN, "\n" + info_block) + end + + # if there *was* no old schema info (no substitution happened) or :force was passed, + # we simply need to insert it in correct position + if new_content == old_content || options[:force] + old_content.sub!(encoding, '') + old_content.sub!(PATTERN, '') - # Strip the old schema info, and insert new schema info. - old_content.sub!(encoding, '') - old_content.sub!(PATTERN, '') + new_content = options[position].to_s == 'after' ? + (encoding_header + (old_content.rstrip + "\n\n" + info_block)) : + (encoding_header + info_block + "\n" + old_content) + end - new_content = options[position].to_s == 'after' ? - (encoding_header + (old_content.rstrip + "\n\n" + info_block)) : - (encoding_header + info_block + "\n" + old_content) - File.open(file_name, "wb") { |f| f.puts new_content } return true end else return false @@ -299,12 +300,11 @@ did_annotate = true end unless options[:exclude_tests] did_annotate = TEST_PATTERNS. - map { |pat| [pat[0], resolve_filename(pat[1], model_name, table_name)] }. - map { |pat| find_test_file(*pat) }. + map { |file| resolve_filename(file, model_name, table_name) }. map { |file| annotate_one_file(file, info, :position_in_test, options_with_position(options, :position_in_test)) }. detect { |result| result } || did_annotate end unless options[:exclude_fixtures] @@ -368,11 +368,11 @@ # Retrieve the classes belonging to the model names we're asked to process # Check for namespaced models in subdirectories as well as models # in subdirectories without namespacing. def get_model_class(file) # this is for non-rails projects, which don't get Rails auto-require magic - require File.expand_path("#{model_dir}/#{file}") unless Module.const_defined?(:Rails) + require File.expand_path("#{model_dir}/#{file}") model_path = file.gsub(/\.rb$/, '') get_loaded_model(model_path) || get_loaded_model(model_path.split('/').last) end # Retrieve loaded model class by path to the file where it's supposed to be defined. @@ -439,20 +439,11 @@ model_name = klass.name.underscore table_name = klass.table_name model_file_name = File.join(model_dir, file) deannotated_klass = true if(remove_annotation_of_file(model_file_name)) - TEST_PATTERNS. - map { |pat| [pat[0], resolve_filename(pat[1], model_name, table_name)]}. - map { |pat| find_test_file(*pat) }.each do |file| - if(File.exist?(file)) - remove_annotation_of_file(file) - deannotated_klass = true - end - end - - (FIXTURE_PATTERNS + FACTORY_PATTERNS). + (TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS). map { |file| resolve_filename(file, model_name, table_name) }. each do |file| if File.exist?(file) remove_annotation_of_file(file) deannotated_klass = true @@ -464,13 +455,9 @@ puts "Unable to deannotate #{file}: #{e.message}" puts "\t" + e.backtrace.join("\n\t") if options[:trace] end end puts "Removed annotations from: #{deannotated.join(', ')}" - end - - def find_test_file(dir, file_name) - Dir.glob(File.join(dir, "**", file_name)).first || File.join(dir, file_name) end def resolve_filename(filename_template, model_name, table_name) return filename_template. gsub('%MODEL_NAME%', model_name).