1- # require 'bigdecimal'
1+ # frozen_string_literal: true
22
33module AnnotateRb
44 module ModelAnnotator
55 class Annotator
6- # Annotate Models plugin use this header
7- PREFIX = '== Schema Information' . freeze
8- PREFIX_MD = '## Schema Information' . freeze
9-
10- MAGIC_COMMENT_MATCHER = Regexp . new ( /(^#\s *encoding:.*(?:\n |r\n ))|(^# coding:.*(?:\n |\r \n ))|(^# -\* - coding:.*(?:\n |\r \n ))|(^# -\* - encoding\s ?:.*(?:\n |\r \n ))|(^#\s *frozen_string_literal:.+(?:\n |\r \n ))|(^# -\* - frozen_string_literal\s *:.+-\* -(?:\n |\r \n ))/ ) . freeze
11-
126 class << self
13- # We're passed a name of things that might be
14- # ActiveRecord models. If we can find the class, and
15- # if its a subclass of ActiveRecord::Base,
16- # then pass it to the associated block
177 def do_annotations ( options = { } )
18- header = options [ :format_markdown ] ? PREFIX_MD . dup : PREFIX . dup
19- version = ActiveRecord ::Migrator . current_version rescue 0
20- if options [ :include_version ] && version > 0
21- header << "\n # Schema version: #{ version } "
22- end
23-
248 annotated = [ ]
25- model_files_to_annotate = ModelFilesGetter . call ( options )
269
27- model_files_to_annotate . each do |path , filename |
28- ModelFileAnnotator . call ( annotated , File . join ( path , filename ) , header , options )
10+ model_files_to_consider = ModelFilesGetter . call ( options )
11+
12+ model_files_to_consider . each do |path , filename |
13+ file = File . join ( path , filename )
14+
15+ if AnnotationDecider . new ( file , options ) . annotate?
16+ ModelFileAnnotator . call ( annotated , file , options )
17+ end
2918 end
3019
3120 if annotated . empty?
@@ -37,34 +26,41 @@ def do_annotations(options = {})
3726
3827 def remove_annotations ( options = { } )
3928 deannotated = [ ]
40- deannotated_klass = false
41- ModelFilesGetter . call ( options ) . each do |file |
42- file = File . join ( file )
29+
30+ model_files_to_consider = ModelFilesGetter . call ( options )
31+
32+ model_files_to_consider . each do |path , filename |
33+ deannotated_klass = false
34+ file = File . join ( path , filename )
35+
4336 begin
4437 klass = ModelClassGetter . call ( file , options )
4538 if klass < ActiveRecord ::Base && !klass . abstract_class?
4639 model_name = klass . name . underscore
4740 table_name = klass . table_name
48- model_file_name = file
49- deannotated_klass = true if FileAnnotationRemover . call ( model_file_name , options )
5041
51- patterns = PatternGetter . call ( options )
42+ if FileAnnotationRemover . call ( file , options )
43+ deannotated_klass = true
44+ end
45+
46+ related_files = RelatedFilesListBuilder . new ( file , model_name , table_name , options ) . build
5247
53- patterns
54- . map { |f | FileNameResolver . call ( f , model_name , table_name ) }
55- . each do |f |
48+ related_files . each do |f , _position_key |
5649 if File . exist? ( f )
5750 FileAnnotationRemover . call ( f , options )
58- deannotated_klass = true
5951 end
6052 end
6153 end
62- deannotated << klass if deannotated_klass
54+
55+ if deannotated_klass
56+ deannotated << klass
57+ end
6358 rescue StandardError => e
6459 $stderr. puts "Unable to deannotate #{ File . join ( file ) } : #{ e . message } "
6560 $stderr. puts "\t " + e . backtrace . join ( "\n \t " ) if options [ :trace ]
6661 end
6762 end
63+
6864 puts "Removed annotations from: #{ deannotated . join ( ', ' ) } "
6965 end
7066 end
0 commit comments