Skip to content

Commit

Permalink
Move some code to AnnotateModels::FilePatterns (ctran#794)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nard-tech authored and ocarta-l committed Jun 18, 2021
1 parent f6f3a07 commit 9ade67c
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 156 deletions.
23 changes: 15 additions & 8 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-04-05 20:42:06 +0900 using RuboCop version 0.68.1.
# on 2020-04-06 10:26:11 +0900 using RuboCop version 0.68.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -55,7 +55,7 @@ Layout/EmptyLineAfterMagicComment:
- 'annotate.gemspec'
- 'spec/lib/annotate/annotate_models_spec.rb'

# Offense count: 7
# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment.
Layout/ExtraSpacing:
Expand All @@ -64,6 +64,13 @@ Layout/ExtraSpacing:
- 'lib/annotate/annotate_models.rb'
- 'lib/tasks/annotate_routes.rake'

# Offense count: 16
# Cop supports --auto-correct.
# Configuration parameters: IndentationWidth.
# SupportedStyles: special_inside_parentheses, consistent, align_brackets
Layout/IndentFirstArrayElement:
EnforcedStyle: consistent

# Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, IndentationWidth.
Expand All @@ -80,7 +87,7 @@ Layout/SpaceAroundEqualsInParameterDefault:
Exclude:
- 'lib/annotate/annotate_routes.rb'

# Offense count: 6
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment.
Layout/SpaceAroundOperators:
Expand Down Expand Up @@ -212,7 +219,7 @@ Naming/AccessorMethodName:
Exclude:
- 'lib/annotate.rb'

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

# Offense count: 28
# Offense count: 30
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: when_needed, always, never
Expand Down Expand Up @@ -470,7 +477,7 @@ Style/StderrPuts:
- 'lib/annotate.rb'
- 'lib/annotate/annotate_models.rb'

# Offense count: 107
# Offense count: 55
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Expand All @@ -484,7 +491,7 @@ Style/StringLiterals:
- 'spec/lib/annotate/annotate_models_spec.rb'
- 'spec/lib/annotate/parser_spec.rb'

# Offense count: 1
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: single_quotes, double_quotes
Expand Down Expand Up @@ -519,7 +526,7 @@ Style/UnneededPercentQ:
Exclude:
- 'annotate.gemspec'

# Offense count: 377
# Offense count: 381
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Expand Down
118 changes: 2 additions & 116 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'bigdecimal'

require 'annotate/constants'
require_relative 'annotate_models/file_patterns'

module AnnotateModels
# Annotate Models plugin use this header
Expand All @@ -16,50 +17,6 @@ module AnnotateModels

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

# File.join for windows reverse bar compat?
# I dont use windows, can`t test
UNIT_TEST_DIR = File.join('test', "unit")
MODEL_TEST_DIR = File.join('test', "models") # since rails 4.0
SPEC_MODEL_DIR = File.join('spec', "models")
FIXTURE_TEST_DIR = File.join('test', "fixtures")
FIXTURE_SPEC_DIR = File.join('spec', "fixtures")

# Other test files
CONTROLLER_TEST_DIR = File.join('test', "controllers")
CONTROLLER_SPEC_DIR = File.join('spec', "controllers")
REQUEST_SPEC_DIR = File.join('spec', "requests")
ROUTING_SPEC_DIR = File.join('spec', "routing")

# Object Daddy http://github.com/flogic/object_daddy/tree/master
EXEMPLARS_TEST_DIR = File.join('test', "exemplars")
EXEMPLARS_SPEC_DIR = File.join('spec', "exemplars")

# Machinist http://github.com/notahat/machinist
BLUEPRINTS_TEST_DIR = File.join('test', "blueprints")
BLUEPRINTS_SPEC_DIR = File.join('spec', "blueprints")

# Factory Bot https://github.com/thoughtbot/factory_bot
FACTORY_BOT_TEST_DIR = File.join('test', "factories")
FACTORY_BOT_SPEC_DIR = File.join('spec', "factories")

# Fabrication https://github.com/paulelliott/fabrication.git
FABRICATORS_TEST_DIR = File.join('test', "fabricators")
FABRICATORS_SPEC_DIR = File.join('spec', "fabricators")

# Serializers https://github.com/rails-api/active_model_serializers
SERIALIZERS_DIR = File.join('app', "serializers")
SERIALIZERS_TEST_DIR = File.join('test', "serializers")
SERIALIZERS_SPEC_DIR = File.join('spec', "serializers")

# Controller files
CONTROLLER_DIR = File.join('app', "controllers")

# Active admin registry files
ACTIVEADMIN_DIR = File.join('app', "admin")

# Helper files
HELPER_DIR = File.join('app', "helpers")

# Don't show limit (#) on these column types
# Example: show "integer" instead of "integer(4)"
NO_LIMIT_COL_TYPES = %w(integer bigint boolean).freeze
Expand Down Expand Up @@ -110,82 +67,11 @@ def root_dir

attr_writer :root_dir

def test_files(root_directory)
[
File.join(root_directory, UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"),
File.join(root_directory, MODEL_TEST_DIR, "%MODEL_NAME%_test.rb"),
File.join(root_directory, SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb")
]
end

def fixture_files(root_directory)
[
File.join(root_directory, FIXTURE_TEST_DIR, "%TABLE_NAME%.yml"),
File.join(root_directory, FIXTURE_SPEC_DIR, "%TABLE_NAME%.yml"),
File.join(root_directory, FIXTURE_TEST_DIR, "%PLURALIZED_MODEL_NAME%.yml"),
File.join(root_directory, FIXTURE_SPEC_DIR, "%PLURALIZED_MODEL_NAME%.yml")
]
end

def scaffold_files(root_directory)
[
File.join(root_directory, CONTROLLER_TEST_DIR, "%PLURALIZED_MODEL_NAME%_controller_test.rb"),
File.join(root_directory, CONTROLLER_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_controller_spec.rb"),
File.join(root_directory, REQUEST_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_spec.rb"),
File.join(root_directory, ROUTING_SPEC_DIR, "%PLURALIZED_MODEL_NAME%_routing_spec.rb")
]
end

def factory_files(root_directory)
[
File.join(root_directory, EXEMPLARS_TEST_DIR, "%MODEL_NAME%_exemplar.rb"),
File.join(root_directory, EXEMPLARS_SPEC_DIR, "%MODEL_NAME%_exemplar.rb"),
File.join(root_directory, BLUEPRINTS_TEST_DIR, "%MODEL_NAME%_blueprint.rb"),
File.join(root_directory, BLUEPRINTS_SPEC_DIR, "%MODEL_NAME%_blueprint.rb"),
File.join(root_directory, FACTORY_BOT_TEST_DIR, "%MODEL_NAME%_factory.rb"), # (old style)
File.join(root_directory, FACTORY_BOT_SPEC_DIR, "%MODEL_NAME%_factory.rb"), # (old style)
File.join(root_directory, FACTORY_BOT_TEST_DIR, "%TABLE_NAME%.rb"), # (new style)
File.join(root_directory, FACTORY_BOT_SPEC_DIR, "%TABLE_NAME%.rb"), # (new style)
File.join(root_directory, FACTORY_BOT_TEST_DIR, "%PLURALIZED_MODEL_NAME%.rb"), # (new style)
File.join(root_directory, FACTORY_BOT_SPEC_DIR, "%PLURALIZED_MODEL_NAME%.rb"), # (new style)
File.join(root_directory, FABRICATORS_TEST_DIR, "%MODEL_NAME%_fabricator.rb"),
File.join(root_directory, FABRICATORS_SPEC_DIR, "%MODEL_NAME%_fabricator.rb")
]
end

def serialize_files(root_directory)
[
File.join(root_directory, SERIALIZERS_DIR, "%MODEL_NAME%_serializer.rb"),
File.join(root_directory, SERIALIZERS_TEST_DIR, "%MODEL_NAME%_serializer_test.rb"),
File.join(root_directory, SERIALIZERS_SPEC_DIR, "%MODEL_NAME%_serializer_spec.rb")
]
end

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'
[File.join(root_directory, HELPER_DIR, "%PLURALIZED_MODEL_NAME%_helper.rb")]
else
[]
end
end

def get_patterns(options, pattern_types = [])
current_patterns = []
root_dir.each do |root_directory|
Array(pattern_types).each do |pattern_type|
patterns = files_by_pattern(root_directory, pattern_type, options)
patterns = FilePatterns.generate(root_directory, pattern_type, options)

current_patterns += if pattern_type.to_sym == :additional_file_patterns
patterns
Expand Down
124 changes: 124 additions & 0 deletions lib/annotate/annotate_models/file_patterns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
module AnnotateModels
# This module provides module method to get file paths.
module FilePatterns
# Controller files
CONTROLLER_DIR = File.join('app', 'controllers')

# Active admin registry files
ACTIVEADMIN_DIR = File.join('app', 'admin')

# Helper files
HELPER_DIR = File.join('app', 'helpers')

# File.join for windows reverse bar compat?
# I dont use windows, can`t test
UNIT_TEST_DIR = File.join('test', 'unit')
MODEL_TEST_DIR = File.join('test', 'models') # since rails 4.0
SPEC_MODEL_DIR = File.join('spec', 'models')

FIXTURE_TEST_DIR = File.join('test', 'fixtures')
FIXTURE_SPEC_DIR = File.join('spec', 'fixtures')

# Other test files
CONTROLLER_TEST_DIR = File.join('test', 'controllers')
CONTROLLER_SPEC_DIR = File.join('spec', 'controllers')
REQUEST_SPEC_DIR = File.join('spec', 'requests')
ROUTING_SPEC_DIR = File.join('spec', 'routing')

# Object Daddy http://github.com/flogic/object_daddy/tree/master
EXEMPLARS_TEST_DIR = File.join('test', 'exemplars')
EXEMPLARS_SPEC_DIR = File.join('spec', 'exemplars')

# Machinist http://github.com/notahat/machinist
BLUEPRINTS_TEST_DIR = File.join('test', 'blueprints')
BLUEPRINTS_SPEC_DIR = File.join('spec', 'blueprints')

# Factory Bot https://github.com/thoughtbot/factory_bot
FACTORY_BOT_TEST_DIR = File.join('test', 'factories')
FACTORY_BOT_SPEC_DIR = File.join('spec', 'factories')

# Fabrication https://github.com/paulelliott/fabrication.git
FABRICATORS_TEST_DIR = File.join('test', 'fabricators')
FABRICATORS_SPEC_DIR = File.join('spec', 'fabricators')

# Serializers https://github.com/rails-api/active_model_serializers
SERIALIZERS_DIR = File.join('app', 'serializers')
SERIALIZERS_TEST_DIR = File.join('test', 'serializers')
SERIALIZERS_SPEC_DIR = File.join('spec', 'serializers')

class << self
def generate(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'
[File.join(root_directory, HELPER_DIR, '%PLURALIZED_MODEL_NAME%_helper.rb')]
else
[]
end
end

private

def test_files(root_directory)
[
File.join(root_directory, UNIT_TEST_DIR, '%MODEL_NAME%_test.rb'),
File.join(root_directory, MODEL_TEST_DIR, '%MODEL_NAME%_test.rb'),
File.join(root_directory, SPEC_MODEL_DIR, '%MODEL_NAME%_spec.rb')
]
end

def fixture_files(root_directory)
[
File.join(root_directory, FIXTURE_TEST_DIR, '%TABLE_NAME%.yml'),
File.join(root_directory, FIXTURE_SPEC_DIR, '%TABLE_NAME%.yml'),
File.join(root_directory, FIXTURE_TEST_DIR, '%PLURALIZED_MODEL_NAME%.yml'),
File.join(root_directory, FIXTURE_SPEC_DIR, '%PLURALIZED_MODEL_NAME%.yml')
]
end

def scaffold_files(root_directory)
[
File.join(root_directory, CONTROLLER_TEST_DIR, '%PLURALIZED_MODEL_NAME%_controller_test.rb'),
File.join(root_directory, CONTROLLER_SPEC_DIR, '%PLURALIZED_MODEL_NAME%_controller_spec.rb'),
File.join(root_directory, REQUEST_SPEC_DIR, '%PLURALIZED_MODEL_NAME%_spec.rb'),
File.join(root_directory, ROUTING_SPEC_DIR, '%PLURALIZED_MODEL_NAME%_routing_spec.rb')
]
end

def factory_files(root_directory)
[
File.join(root_directory, EXEMPLARS_TEST_DIR, '%MODEL_NAME%_exemplar.rb'),
File.join(root_directory, EXEMPLARS_SPEC_DIR, '%MODEL_NAME%_exemplar.rb'),
File.join(root_directory, BLUEPRINTS_TEST_DIR, '%MODEL_NAME%_blueprint.rb'),
File.join(root_directory, BLUEPRINTS_SPEC_DIR, '%MODEL_NAME%_blueprint.rb'),
File.join(root_directory, FACTORY_BOT_TEST_DIR, '%MODEL_NAME%_factory.rb'), # (old style)
File.join(root_directory, FACTORY_BOT_SPEC_DIR, '%MODEL_NAME%_factory.rb'), # (old style)
File.join(root_directory, FACTORY_BOT_TEST_DIR, '%TABLE_NAME%.rb'), # (new style)
File.join(root_directory, FACTORY_BOT_SPEC_DIR, '%TABLE_NAME%.rb'), # (new style)
File.join(root_directory, FACTORY_BOT_TEST_DIR, '%PLURALIZED_MODEL_NAME%.rb'), # (new style)
File.join(root_directory, FACTORY_BOT_SPEC_DIR, '%PLURALIZED_MODEL_NAME%.rb'), # (new style)
File.join(root_directory, FABRICATORS_TEST_DIR, '%MODEL_NAME%_fabricator.rb'),
File.join(root_directory, FABRICATORS_SPEC_DIR, '%MODEL_NAME%_fabricator.rb')
]
end

def serialize_files(root_directory)
[
File.join(root_directory, SERIALIZERS_DIR, '%MODEL_NAME%_serializer.rb'),
File.join(root_directory, SERIALIZERS_TEST_DIR, '%MODEL_NAME%_serializer_test.rb'),
File.join(root_directory, SERIALIZERS_SPEC_DIR, '%MODEL_NAME%_serializer_spec.rb')
]
end
end
end
end
Loading

0 comments on commit 9ade67c

Please sign in to comment.