Skip to content

Commit 8666d89

Browse files
authored
Move some code to AnnotateModels::FilePatterns (ctran#794)
AnnotateModels is fat and so hard to maintain. In order to reduce code lines of AnnotateModels, move constants and methods defined in AnnotateModels to another namespace AnnotateModels::FilePatterns.
1 parent fdfc340 commit 8666d89

File tree

5 files changed

+280
-156
lines changed

5 files changed

+280
-156
lines changed

.rubocop_todo.yml

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-04-05 20:42:06 +0900 using RuboCop version 0.68.1.
3+
# on 2020-04-06 10:26:11 +0900 using RuboCop version 0.68.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -55,7 +55,7 @@ Layout/EmptyLineAfterMagicComment:
5555
- 'annotate.gemspec'
5656
- 'spec/lib/annotate/annotate_models_spec.rb'
5757

58-
# Offense count: 7
58+
# Offense count: 3
5959
# Cop supports --auto-correct.
6060
# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment.
6161
Layout/ExtraSpacing:
@@ -64,6 +64,13 @@ Layout/ExtraSpacing:
6464
- 'lib/annotate/annotate_models.rb'
6565
- 'lib/tasks/annotate_routes.rake'
6666

67+
# Offense count: 16
68+
# Cop supports --auto-correct.
69+
# Configuration parameters: IndentationWidth.
70+
# SupportedStyles: special_inside_parentheses, consistent, align_brackets
71+
Layout/IndentFirstArrayElement:
72+
EnforcedStyle: consistent
73+
6774
# Offense count: 5
6875
# Cop supports --auto-correct.
6976
# Configuration parameters: EnforcedStyle, IndentationWidth.
@@ -80,7 +87,7 @@ Layout/SpaceAroundEqualsInParameterDefault:
8087
Exclude:
8188
- 'lib/annotate/annotate_routes.rb'
8289

83-
# Offense count: 6
90+
# Offense count: 4
8491
# Cop supports --auto-correct.
8592
# Configuration parameters: AllowForAlignment.
8693
Layout/SpaceAroundOperators:
@@ -212,7 +219,7 @@ Naming/AccessorMethodName:
212219
Exclude:
213220
- 'lib/annotate.rb'
214221

215-
# Offense count: 102
222+
# Offense count: 103
216223
# Configuration parameters: Blacklist.
217224
# Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
218225
Naming/HeredocDelimiterNaming:
@@ -309,7 +316,7 @@ Style/FormatStringToken:
309316
Exclude:
310317
- 'lib/annotate/annotate_models.rb'
311318

312-
# Offense count: 28
319+
# Offense count: 30
313320
# Cop supports --auto-correct.
314321
# Configuration parameters: EnforcedStyle.
315322
# SupportedStyles: when_needed, always, never
@@ -470,7 +477,7 @@ Style/StderrPuts:
470477
- 'lib/annotate.rb'
471478
- 'lib/annotate/annotate_models.rb'
472479

473-
# Offense count: 107
480+
# Offense count: 55
474481
# Cop supports --auto-correct.
475482
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
476483
# SupportedStyles: single_quotes, double_quotes
@@ -484,7 +491,7 @@ Style/StringLiterals:
484491
- 'spec/lib/annotate/annotate_models_spec.rb'
485492
- 'spec/lib/annotate/parser_spec.rb'
486493

487-
# Offense count: 1
494+
# Offense count: 2
488495
# Cop supports --auto-correct.
489496
# Configuration parameters: EnforcedStyle.
490497
# SupportedStyles: single_quotes, double_quotes
@@ -519,7 +526,7 @@ Style/UnneededPercentQ:
519526
Exclude:
520527
- 'annotate.gemspec'
521528

522-
# Offense count: 377
529+
# Offense count: 381
523530
# Cop supports --auto-correct.
524531
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
525532
# URISchemes: http, https

lib/annotate/annotate_models.rb

+2-116
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'bigdecimal'
44

55
require 'annotate/constants'
6+
require_relative 'annotate_models/file_patterns'
67

78
module AnnotateModels
89
# Annotate Models plugin use this header
@@ -16,50 +17,6 @@ module AnnotateModels
1617

1718
MATCHED_TYPES = %w(test fixture factory serializer scaffold controller helper).freeze
1819

19-
# File.join for windows reverse bar compat?
20-
# I dont use windows, can`t test
21-
UNIT_TEST_DIR = File.join('test', "unit")
22-
MODEL_TEST_DIR = File.join('test', "models") # since rails 4.0
23-
SPEC_MODEL_DIR = File.join('spec', "models")
24-
FIXTURE_TEST_DIR = File.join('test', "fixtures")
25-
FIXTURE_SPEC_DIR = File.join('spec', "fixtures")
26-
27-
# Other test files
28-
CONTROLLER_TEST_DIR = File.join('test', "controllers")
29-
CONTROLLER_SPEC_DIR = File.join('spec', "controllers")
30-
REQUEST_SPEC_DIR = File.join('spec', "requests")
31-
ROUTING_SPEC_DIR = File.join('spec', "routing")
32-
33-
# Object Daddy http://github.com/flogic/object_daddy/tree/master
34-
EXEMPLARS_TEST_DIR = File.join('test', "exemplars")
35-
EXEMPLARS_SPEC_DIR = File.join('spec', "exemplars")
36-
37-
# Machinist http://github.com/notahat/machinist
38-
BLUEPRINTS_TEST_DIR = File.join('test', "blueprints")
39-
BLUEPRINTS_SPEC_DIR = File.join('spec', "blueprints")
40-
41-
# Factory Bot https://github.com/thoughtbot/factory_bot
42-
FACTORY_BOT_TEST_DIR = File.join('test', "factories")
43-
FACTORY_BOT_SPEC_DIR = File.join('spec', "factories")
44-
45-
# Fabrication https://github.com/paulelliott/fabrication.git
46-
FABRICATORS_TEST_DIR = File.join('test', "fabricators")
47-
FABRICATORS_SPEC_DIR = File.join('spec', "fabricators")
48-
49-
# Serializers https://github.com/rails-api/active_model_serializers
50-
SERIALIZERS_DIR = File.join('app', "serializers")
51-
SERIALIZERS_TEST_DIR = File.join('test', "serializers")
52-
SERIALIZERS_SPEC_DIR = File.join('spec', "serializers")
53-
54-
# Controller files
55-
CONTROLLER_DIR = File.join('app', "controllers")
56-
57-
# Active admin registry files
58-
ACTIVEADMIN_DIR = File.join('app', "admin")
59-
60-
# Helper files
61-
HELPER_DIR = File.join('app', "helpers")
62-
6320
# Don't show limit (#) on these column types
6421
# Example: show "integer" instead of "integer(4)"
6522
NO_LIMIT_COL_TYPES = %w(integer bigint boolean).freeze
@@ -110,82 +67,11 @@ def root_dir
11067

11168
attr_writer :root_dir
11269

113-
def test_files(root_directory)
114-
[
115-
File.join(root_directory, UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"),
116-
File.join(root_directory, MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"),
117-
File.join(root_directory, SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb")
118-
]
119-
end
120-
121-
def fixture_files(root_directory)
122-
[
123-
File.join(root_directory, FIXTURE_TEST_DIR, "%TABLE_NAME%.yml"),
124-
File.join(root_directory, FIXTURE_SPEC_DIR, "%TABLE_NAME%.yml"),
125-
File.join(root_directory, FIXTURE_TEST_DIR, "%PLURALIZED_MODEL_NAME%.yml"),
126-
File.join(root_directory, FIXTURE_SPEC_DIR, "%PLURALIZED_MODEL_NAME%.yml")
127-
]
128-
end
129-
130-
def scaffold_files(root_directory)
131-
[
132-
File.join(root_directory, CONTROLLER_TEST_DIR, "%PLURALIZED_MODEL_NAME%_controller_test.rb"),
133-
File.join(root_directory, CONTROLLER_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_controller_spec.rb"),
134-
File.join(root_directory, REQUEST_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_spec.rb"),
135-
File.join(root_directory, ROUTING_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_routing_spec.rb")
136-
]
137-
end
138-
139-
def factory_files(root_directory)
140-
[
141-
File.join(root_directory, EXEMPLARS_TEST_DIR, "%MODEL_NAME%_exemplar.rb"),
142-
File.join(root_directory, EXEMPLARS_SPEC_DIR, "%MODEL_NAME%_exemplar.rb"),
143-
File.join(root_directory, BLUEPRINTS_TEST_DIR, "%MODEL_NAME%_blueprint.rb"),
144-
File.join(root_directory, BLUEPRINTS_SPEC_DIR, "%MODEL_NAME%_blueprint.rb"),
145-
File.join(root_directory, FACTORY_BOT_TEST_DIR, "%MODEL_NAME%_factory.rb"), # (old style)
146-
File.join(root_directory, FACTORY_BOT_SPEC_DIR, "%MODEL_NAME%_factory.rb"), # (old style)
147-
File.join(root_directory, FACTORY_BOT_TEST_DIR, "%TABLE_NAME%.rb"), # (new style)
148-
File.join(root_directory, FACTORY_BOT_SPEC_DIR, "%TABLE_NAME%.rb"), # (new style)
149-
File.join(root_directory, FACTORY_BOT_TEST_DIR, "%PLURALIZED_MODEL_NAME%.rb"), # (new style)
150-
File.join(root_directory, FACTORY_BOT_SPEC_DIR, "%PLURALIZED_MODEL_NAME%.rb"), # (new style)
151-
File.join(root_directory, FABRICATORS_TEST_DIR, "%MODEL_NAME%_fabricator.rb"),
152-
File.join(root_directory, FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb")
153-
]
154-
end
155-
156-
def serialize_files(root_directory)
157-
[
158-
File.join(root_directory, SERIALIZERS_DIR, "%MODEL_NAME%_serializer.rb"),
159-
File.join(root_directory, SERIALIZERS_TEST_DIR, "%MODEL_NAME%_serializer_test.rb"),
160-
File.join(root_directory, SERIALIZERS_SPEC_DIR, "%MODEL_NAME%_serializer_spec.rb")
161-
]
162-
end
163-
164-
def files_by_pattern(root_directory, pattern_type, options)
165-
case pattern_type
166-
when 'test' then test_files(root_directory)
167-
when 'fixture' then fixture_files(root_directory)
168-
when 'scaffold' then scaffold_files(root_directory)
169-
when 'factory' then factory_files(root_directory)
170-
when 'serializer' then serialize_files(root_directory)
171-
when 'additional_file_patterns'
172-
[options[:additional_file_patterns] || []].flatten
173-
when 'controller'
174-
[File.join(root_directory, CONTROLLER_DIR, "%PLURALIZED_MODEL_NAME%_controller.rb")]
175-
when 'admin'
176-
[File.join(root_directory, ACTIVEADMIN_DIR, "%MODEL_NAME%.rb")]
177-
when 'helper'
178-
[File.join(root_directory, HELPER_DIR, "%PLURALIZED_MODEL_NAME%_helper.rb")]
179-
else
180-
[]
181-
end
182-
end
183-
18470
def get_patterns(options, pattern_types = [])
18571
current_patterns = []
18672
root_dir.each do |root_directory|
18773
Array(pattern_types).each do |pattern_type|
188-
patterns = files_by_pattern(root_directory, pattern_type, options)
74+
patterns = FilePatterns.generate(root_directory, pattern_type, options)
18975

19076
current_patterns += if pattern_type.to_sym == :additional_file_patterns
19177
patterns
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
module AnnotateModels
2+
# This module provides module method to get file paths.
3+
module FilePatterns
4+
# Controller files
5+
CONTROLLER_DIR = File.join('app', 'controllers')
6+
7+
# Active admin registry files
8+
ACTIVEADMIN_DIR = File.join('app', 'admin')
9+
10+
# Helper files
11+
HELPER_DIR = File.join('app', 'helpers')
12+
13+
# File.join for windows reverse bar compat?
14+
# I dont use windows, can`t test
15+
UNIT_TEST_DIR = File.join('test', 'unit')
16+
MODEL_TEST_DIR = File.join('test', 'models') # since rails 4.0
17+
SPEC_MODEL_DIR = File.join('spec', 'models')
18+
19+
FIXTURE_TEST_DIR = File.join('test', 'fixtures')
20+
FIXTURE_SPEC_DIR = File.join('spec', 'fixtures')
21+
22+
# Other test files
23+
CONTROLLER_TEST_DIR = File.join('test', 'controllers')
24+
CONTROLLER_SPEC_DIR = File.join('spec', 'controllers')
25+
REQUEST_SPEC_DIR = File.join('spec', 'requests')
26+
ROUTING_SPEC_DIR = File.join('spec', 'routing')
27+
28+
# Object Daddy http://github.com/flogic/object_daddy/tree/master
29+
EXEMPLARS_TEST_DIR = File.join('test', 'exemplars')
30+
EXEMPLARS_SPEC_DIR = File.join('spec', 'exemplars')
31+
32+
# Machinist http://github.com/notahat/machinist
33+
BLUEPRINTS_TEST_DIR = File.join('test', 'blueprints')
34+
BLUEPRINTS_SPEC_DIR = File.join('spec', 'blueprints')
35+
36+
# Factory Bot https://github.com/thoughtbot/factory_bot
37+
FACTORY_BOT_TEST_DIR = File.join('test', 'factories')
38+
FACTORY_BOT_SPEC_DIR = File.join('spec', 'factories')
39+
40+
# Fabrication https://github.com/paulelliott/fabrication.git
41+
FABRICATORS_TEST_DIR = File.join('test', 'fabricators')
42+
FABRICATORS_SPEC_DIR = File.join('spec', 'fabricators')
43+
44+
# Serializers https://github.com/rails-api/active_model_serializers
45+
SERIALIZERS_DIR = File.join('app', 'serializers')
46+
SERIALIZERS_TEST_DIR = File.join('test', 'serializers')
47+
SERIALIZERS_SPEC_DIR = File.join('spec', 'serializers')
48+
49+
class << self
50+
def generate(root_directory, pattern_type, options)
51+
case pattern_type
52+
when 'test' then test_files(root_directory)
53+
when 'fixture' then fixture_files(root_directory)
54+
when 'scaffold' then scaffold_files(root_directory)
55+
when 'factory' then factory_files(root_directory)
56+
when 'serializer' then serialize_files(root_directory)
57+
when 'additional_file_patterns'
58+
[options[:additional_file_patterns] || []].flatten
59+
when 'controller'
60+
[File.join(root_directory, CONTROLLER_DIR, '%PLURALIZED_MODEL_NAME%_controller.rb')]
61+
when 'admin'
62+
[File.join(root_directory, ACTIVEADMIN_DIR, '%MODEL_NAME%.rb')]
63+
when 'helper'
64+
[File.join(root_directory, HELPER_DIR, '%PLURALIZED_MODEL_NAME%_helper.rb')]
65+
else
66+
[]
67+
end
68+
end
69+
70+
private
71+
72+
def test_files(root_directory)
73+
[
74+
File.join(root_directory, UNIT_TEST_DIR, '%MODEL_NAME%_test.rb'),
75+
File.join(root_directory, MODEL_TEST_DIR, '%MODEL_NAME%_test.rb'),
76+
File.join(root_directory, SPEC_MODEL_DIR, '%MODEL_NAME%_spec.rb')
77+
]
78+
end
79+
80+
def fixture_files(root_directory)
81+
[
82+
File.join(root_directory, FIXTURE_TEST_DIR, '%TABLE_NAME%.yml'),
83+
File.join(root_directory, FIXTURE_SPEC_DIR, '%TABLE_NAME%.yml'),
84+
File.join(root_directory, FIXTURE_TEST_DIR, '%PLURALIZED_MODEL_NAME%.yml'),
85+
File.join(root_directory, FIXTURE_SPEC_DIR, '%PLURALIZED_MODEL_NAME%.yml')
86+
]
87+
end
88+
89+
def scaffold_files(root_directory)
90+
[
91+
File.join(root_directory, CONTROLLER_TEST_DIR, '%PLURALIZED_MODEL_NAME%_controller_test.rb'),
92+
File.join(root_directory, CONTROLLER_SPEC_DIR, '%PLURALIZED_MODEL_NAME%_controller_spec.rb'),
93+
File.join(root_directory, REQUEST_SPEC_DIR, '%PLURALIZED_MODEL_NAME%_spec.rb'),
94+
File.join(root_directory, ROUTING_SPEC_DIR, '%PLURALIZED_MODEL_NAME%_routing_spec.rb')
95+
]
96+
end
97+
98+
def factory_files(root_directory)
99+
[
100+
File.join(root_directory, EXEMPLARS_TEST_DIR, '%MODEL_NAME%_exemplar.rb'),
101+
File.join(root_directory, EXEMPLARS_SPEC_DIR, '%MODEL_NAME%_exemplar.rb'),
102+
File.join(root_directory, BLUEPRINTS_TEST_DIR, '%MODEL_NAME%_blueprint.rb'),
103+
File.join(root_directory, BLUEPRINTS_SPEC_DIR, '%MODEL_NAME%_blueprint.rb'),
104+
File.join(root_directory, FACTORY_BOT_TEST_DIR, '%MODEL_NAME%_factory.rb'), # (old style)
105+
File.join(root_directory, FACTORY_BOT_SPEC_DIR, '%MODEL_NAME%_factory.rb'), # (old style)
106+
File.join(root_directory, FACTORY_BOT_TEST_DIR, '%TABLE_NAME%.rb'), # (new style)
107+
File.join(root_directory, FACTORY_BOT_SPEC_DIR, '%TABLE_NAME%.rb'), # (new style)
108+
File.join(root_directory, FACTORY_BOT_TEST_DIR, '%PLURALIZED_MODEL_NAME%.rb'), # (new style)
109+
File.join(root_directory, FACTORY_BOT_SPEC_DIR, '%PLURALIZED_MODEL_NAME%.rb'), # (new style)
110+
File.join(root_directory, FABRICATORS_TEST_DIR, '%MODEL_NAME%_fabricator.rb'),
111+
File.join(root_directory, FABRICATORS_SPEC_DIR, '%MODEL_NAME%_fabricator.rb')
112+
]
113+
end
114+
115+
def serialize_files(root_directory)
116+
[
117+
File.join(root_directory, SERIALIZERS_DIR, '%MODEL_NAME%_serializer.rb'),
118+
File.join(root_directory, SERIALIZERS_TEST_DIR, '%MODEL_NAME%_serializer_test.rb'),
119+
File.join(root_directory, SERIALIZERS_SPEC_DIR, '%MODEL_NAME%_serializer_spec.rb')
120+
]
121+
end
122+
end
123+
end
124+
end

0 commit comments

Comments
 (0)