annotaterb 4.0.0 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ # Given a model file and options, this class will return a list of related files (e.g. fixture, controllers, etc)
6
+ # to also annotate
7
+ class RelatedFilesListBuilder
8
+ RELATED_TYPES = %w(test fixture factory serializer scaffold controller helper).freeze
9
+
10
+ def initialize(file, model_name, table_name, options)
11
+ @file = file
12
+ @model_name = model_name
13
+ @table_name = table_name
14
+ @options = options
15
+ end
16
+
17
+ def build
18
+ @list = []
19
+
20
+ add_related_test_files if !@options[:exclude_tests]
21
+ add_related_fixture_files if !@options[:exclude_fixtures]
22
+ add_related_factory_files if !@options[:exclude_factories]
23
+ add_related_serializer_files if !@options[:exclude_serializers]
24
+ add_related_scaffold_files if !@options[:exclude_scaffolds]
25
+ add_related_controller_files if !@options[:exclude_controllers]
26
+ add_related_helper_files if !@options[:exclude_helpers]
27
+ add_related_admin_files if !@options[:active_admin]
28
+ add_additional_file_patterns if !@options[:additional_file_patterns].present?
29
+
30
+ @list
31
+ end
32
+
33
+ private
34
+
35
+ def related_files_for_pattern(pattern_type)
36
+ patterns = PatternGetter.call(@options, pattern_type)
37
+
38
+ _related_files = patterns
39
+ .map { |f| FileNameResolver.call(f, @model_name, @table_name) }
40
+ .map { |f| Dir.glob(f) }
41
+ .flatten
42
+
43
+ _related_files
44
+ end
45
+
46
+ def add_related_test_files
47
+ position_key = :position_in_test
48
+ pattern_type = 'test'
49
+
50
+ related_files = related_files_for_pattern(pattern_type)
51
+ files_with_position_key = related_files.map { |f| [f, position_key] }
52
+
53
+ @list.concat(files_with_position_key)
54
+ end
55
+
56
+ def add_related_fixture_files
57
+ position_key = :position_in_fixture
58
+ pattern_type = 'fixture'
59
+
60
+ related_files = related_files_for_pattern(pattern_type)
61
+ files_with_position_key = related_files.map { |f| [f, position_key] }
62
+
63
+ @list.concat(files_with_position_key)
64
+ end
65
+
66
+ def add_related_factory_files
67
+ position_key = :position_in_factory
68
+ pattern_type = 'factory'
69
+
70
+ related_files = related_files_for_pattern(pattern_type)
71
+ files_with_position_key = related_files.map { |f| [f, position_key] }
72
+
73
+ @list.concat(files_with_position_key)
74
+ end
75
+
76
+ def add_related_serializer_files
77
+ position_key = :position_in_serializer
78
+ pattern_type = 'serializer'
79
+
80
+ related_files = related_files_for_pattern(pattern_type)
81
+ files_with_position_key = related_files.map { |f| [f, position_key] }
82
+
83
+ @list.concat(files_with_position_key)
84
+ end
85
+
86
+ def add_related_scaffold_files
87
+ position_key = :position_in_scaffold # Key does not exist
88
+ pattern_type = 'scaffold'
89
+
90
+ related_files = related_files_for_pattern(pattern_type)
91
+ files_with_position_key = related_files.map { |f| [f, position_key] }
92
+
93
+ @list.concat(files_with_position_key)
94
+ end
95
+
96
+ def add_related_controller_files
97
+ position_key = :position_in_controller # Key does not exist
98
+ pattern_type = 'controller'
99
+
100
+ related_files = related_files_for_pattern(pattern_type)
101
+ files_with_position_key = related_files.map { |f| [f, position_key] }
102
+
103
+ @list.concat(files_with_position_key)
104
+ end
105
+
106
+ def add_related_helper_files
107
+ position_key = :position_in_helper # Key does not exist
108
+ pattern_type = 'helper'
109
+
110
+ related_files = related_files_for_pattern(pattern_type)
111
+ files_with_position_key = related_files.map { |f| [f, position_key] }
112
+
113
+ @list.concat(files_with_position_key)
114
+ end
115
+
116
+ def add_related_admin_files
117
+ position_key = :position_in_admin # Key does not exist
118
+ pattern_type = 'admin'
119
+
120
+ related_files = related_files_for_pattern(pattern_type)
121
+ files_with_position_key = related_files.map { |f| [f, position_key] }
122
+
123
+ @list.concat(files_with_position_key)
124
+ end
125
+
126
+ def add_additional_file_patterns
127
+ position_key = :position_in_additional_file_patterns # Key does not exist
128
+ pattern_type = 'additional_file_patterns'
129
+
130
+ related_files = related_files_for_pattern(pattern_type)
131
+ files_with_position_key = related_files.map { |f| [f, position_key] }
132
+
133
+ @list.concat(files_with_position_key)
134
+ end
135
+ end
136
+ end
137
+ end
@@ -6,7 +6,6 @@ module AnnotateRb
6
6
  autoload :Helper, 'annotate_rb/model_annotator/helper'
7
7
  autoload :FilePatterns, 'annotate_rb/model_annotator/file_patterns'
8
8
  autoload :Constants, 'annotate_rb/model_annotator/constants'
9
- autoload :SchemaInfo, 'annotate_rb/model_annotator/schema_info'
10
9
  autoload :PatternGetter, 'annotate_rb/model_annotator/pattern_getter'
11
10
  autoload :BadModelFileError, 'annotate_rb/model_annotator/bad_model_file_error'
12
11
  autoload :FileNameResolver, 'annotate_rb/model_annotator/file_name_resolver'
@@ -16,5 +15,16 @@ module AnnotateRb
16
15
  autoload :ModelFilesGetter, 'annotate_rb/model_annotator/model_files_getter'
17
16
  autoload :FileAnnotator, 'annotate_rb/model_annotator/file_annotator'
18
17
  autoload :ModelFileAnnotator, 'annotate_rb/model_annotator/model_file_annotator'
18
+ autoload :ModelWrapper, 'annotate_rb/model_annotator/model_wrapper'
19
+ autoload :AnnotationGenerator, 'annotate_rb/model_annotator/annotation_generator'
20
+ autoload :ColumnAttributesBuilder, 'annotate_rb/model_annotator/column_attributes_builder'
21
+ autoload :ColumnTypeBuilder, 'annotate_rb/model_annotator/column_type_builder'
22
+ autoload :ColumnWrapper, 'annotate_rb/model_annotator/column_wrapper'
23
+ autoload :ColumnAnnotationBuilder, 'annotate_rb/model_annotator/column_annotation_builder'
24
+ autoload :IndexAnnotationBuilder, 'annotate_rb/model_annotator/index_annotation_builder'
25
+ autoload :ForeignKeyAnnotationBuilder, 'annotate_rb/model_annotator/foreign_key_annotation_builder'
26
+ autoload :RelatedFilesListBuilder, 'annotate_rb/model_annotator/related_files_list_builder'
27
+ autoload :AnnotationDecider, 'annotate_rb/model_annotator/annotation_decider'
28
+ autoload :FileAnnotatorInstruction, 'annotate_rb/model_annotator/file_annotator_instruction'
19
29
  end
20
30
  end
@@ -26,14 +26,14 @@ module AnnotateRb
26
26
 
27
27
  FLAG_OPTIONS = {
28
28
  classified_sort: true, # ModelAnnotator
29
- exclude_controllers: true, # Unused
30
- exclude_factories: false, # Unused
31
- exclude_fixtures: false, # Unused
32
- exclude_helpers: true, # Unused
33
- exclude_scaffolds: true, # Unused
34
- exclude_serializers: false, # Unused
29
+ exclude_controllers: true, # ModelAnnotator
30
+ exclude_factories: false, # ModelAnnotator
31
+ exclude_fixtures: false, # ModelAnnotator
32
+ exclude_helpers: true, # ModelAnnotator
33
+ exclude_scaffolds: true, # ModelAnnotator
34
+ exclude_serializers: false, # ModelAnnotator
35
35
  exclude_sti_subclasses: false, # ModelAnnotator
36
- exclude_tests: false, # Unused
36
+ exclude_tests: false, # ModelAnnotator
37
37
  force: false, # ModelAnnotator, but should be used by both
38
38
  format_bare: true, # Unused
39
39
  format_markdown: false, # ModelAnnotator, RouteAnnotator
@@ -59,10 +59,10 @@ module AnnotateRb
59
59
  debug: false, # Core
60
60
 
61
61
  # ModelAnnotator
62
- hide_default_column_types: '<%= ::AnnotateRb::ModelAnnotator::SchemaInfo::NO_DEFAULT_COL_TYPES.join(",") %>',
62
+ hide_default_column_types: '',
63
63
 
64
64
  # ModelAnnotator
65
- hide_limit_column_types: '<%= ::AnnotateRb::ModelAnnotator::SchemaInfo::NO_LIMIT_COL_TYPES.join(",") %>',
65
+ hide_limit_column_types: '',
66
66
 
67
67
  ignore_columns: nil, # ModelAnnotator
68
68
  ignore_routes: nil, # RouteAnnotator
@@ -95,7 +95,14 @@ module AnnotateRb
95
95
 
96
96
  FLAG_OPTION_KEYS = [
97
97
  :classified_sort,
98
+ :exclude_controllers,
99
+ :exclude_factories,
100
+ :exclude_fixtures,
101
+ :exclude_helpers,
102
+ :exclude_scaffolds,
103
+ :exclude_serializers,
98
104
  :exclude_sti_subclasses,
105
+ :exclude_tests,
99
106
  :force,
100
107
  :format_markdown,
101
108
  :format_rdoc,
@@ -156,13 +156,6 @@ module AnnotateRb
156
156
  option_parser.separator(' ' * 4 + 'Usage: annotaterb models [options]')
157
157
  option_parser.separator('')
158
158
 
159
- # option_parser.on('-m',
160
- # '--models',
161
- # "Annotate ActiveRecord models") do
162
- # @options[:models] = true
163
- # @options[:command] = Commands::AnnotateModels.new
164
- # end
165
-
166
159
  option_parser.on('-a',
167
160
  '--active-admin',
168
161
  'Annotate active_admin models') do
@@ -232,13 +225,6 @@ module AnnotateRb
232
225
  option_parser.separator(' ' * 4 + 'Usage: annotaterb routes [options]')
233
226
  option_parser.separator('')
234
227
 
235
- # option_parser.on('-r',
236
- # '--routes',
237
- # "Annotate routes.rb with the output of 'rails routes'") do
238
- # @options[:routes] = true
239
- # @options[:command] = Commands::AnnotateRoutes.new
240
- # end
241
-
242
228
  option_parser.on('--ignore-routes REGEX',
243
229
  "don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`") do |regex|
244
230
  @options[:ignore_routes] = regex
data/lib/annotate_rb.rb CHANGED
@@ -18,7 +18,6 @@ require_relative 'annotate_rb/parser'
18
18
  require_relative 'annotate_rb/runner'
19
19
  require_relative 'annotate_rb/route_annotator'
20
20
  require_relative 'annotate_rb/model_annotator'
21
- require_relative 'annotate_rb/env'
22
21
  require_relative 'annotate_rb/options'
23
22
  require_relative 'annotate_rb/eager_loader'
24
23
  require_relative 'annotate_rb/rake_bootstrapper'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: annotaterb
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew W. Lee
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-03 00:00:00.000000000 Z
11
+ date: 2023-05-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Annotates Rails/ActiveRecord Models, routes, fixtures, and others based
14
14
  on the database schema.
@@ -35,22 +35,31 @@ files:
35
35
  - lib/annotate_rb/config_loader.rb
36
36
  - lib/annotate_rb/core.rb
37
37
  - lib/annotate_rb/eager_loader.rb
38
- - lib/annotate_rb/env.rb
39
38
  - lib/annotate_rb/model_annotator.rb
39
+ - lib/annotate_rb/model_annotator/annotation_decider.rb
40
+ - lib/annotate_rb/model_annotator/annotation_generator.rb
40
41
  - lib/annotate_rb/model_annotator/annotation_pattern_generator.rb
41
42
  - lib/annotate_rb/model_annotator/annotator.rb
42
43
  - lib/annotate_rb/model_annotator/bad_model_file_error.rb
44
+ - lib/annotate_rb/model_annotator/column_annotation_builder.rb
45
+ - lib/annotate_rb/model_annotator/column_attributes_builder.rb
46
+ - lib/annotate_rb/model_annotator/column_type_builder.rb
47
+ - lib/annotate_rb/model_annotator/column_wrapper.rb
43
48
  - lib/annotate_rb/model_annotator/constants.rb
44
49
  - lib/annotate_rb/model_annotator/file_annotation_remover.rb
45
50
  - lib/annotate_rb/model_annotator/file_annotator.rb
51
+ - lib/annotate_rb/model_annotator/file_annotator_instruction.rb
46
52
  - lib/annotate_rb/model_annotator/file_name_resolver.rb
47
53
  - lib/annotate_rb/model_annotator/file_patterns.rb
54
+ - lib/annotate_rb/model_annotator/foreign_key_annotation_builder.rb
48
55
  - lib/annotate_rb/model_annotator/helper.rb
56
+ - lib/annotate_rb/model_annotator/index_annotation_builder.rb
49
57
  - lib/annotate_rb/model_annotator/model_class_getter.rb
50
58
  - lib/annotate_rb/model_annotator/model_file_annotator.rb
51
59
  - lib/annotate_rb/model_annotator/model_files_getter.rb
60
+ - lib/annotate_rb/model_annotator/model_wrapper.rb
52
61
  - lib/annotate_rb/model_annotator/pattern_getter.rb
53
- - lib/annotate_rb/model_annotator/schema_info.rb
62
+ - lib/annotate_rb/model_annotator/related_files_list_builder.rb
54
63
  - lib/annotate_rb/options.rb
55
64
  - lib/annotate_rb/parser.rb
56
65
  - lib/annotate_rb/rake_bootstrapper.rb
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module AnnotateRb
4
- class Env
5
- class << self
6
- def read(key)
7
- key = key.to_s unless key.is_a?(String)
8
-
9
- ENV[key]
10
- end
11
-
12
- def write(key, value)
13
- key = key.to_s unless key.is_a?(String)
14
-
15
- ENV[key] = value.to_s
16
- end
17
-
18
- def fetch(key, default_value)
19
- key = key.to_s unless key.is_a?(String)
20
- val = read(key)
21
-
22
- if val.nil?
23
- default_value
24
- else
25
- val
26
- end
27
- end
28
- end
29
- end
30
- end