diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5863ac72e..246a23ea6 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -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: @@ -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. @@ -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: @@ -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: @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 8351e1162..0aeb72889 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -3,6 +3,7 @@ require 'bigdecimal' require 'annotate/constants' +require_relative 'annotate_models/file_patterns' module AnnotateModels # Annotate Models plugin use this header @@ -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 @@ -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 diff --git a/lib/annotate/annotate_models/file_patterns.rb b/lib/annotate/annotate_models/file_patterns.rb new file mode 100644 index 000000000..262d3e0a2 --- /dev/null +++ b/lib/annotate/annotate_models/file_patterns.rb @@ -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 diff --git a/spec/lib/annotate/annotate_models/file_patterns_spec.rb b/spec/lib/annotate/annotate_models/file_patterns_spec.rb new file mode 100644 index 000000000..9c3138625 --- /dev/null +++ b/spec/lib/annotate/annotate_models/file_patterns_spec.rb @@ -0,0 +1,139 @@ +require_relative '../../../spec_helper' +require 'annotate/annotate_models' + +describe AnnotateModels::FilePatterns do + describe '.by_pattern' do + subject { AnnotateModels::FilePatterns.generate(root_directory, pattern_type, options) } + + let(:root_directory) { '/root' } + let(:options) { {} } + + context 'when pattern_type is "test"' do + let(:pattern_type) { 'test' } + + it 'returns patterns of test files' do + is_expected.to eq([ + '/root/test/unit/%MODEL_NAME%_test.rb', + '/root/test/models/%MODEL_NAME%_test.rb', + '/root/spec/models/%MODEL_NAME%_spec.rb' + ]) + end + end + + context 'when pattern_type is "fixture"' do + let(:pattern_type) { 'fixture' } + + it 'returns patterns of fixture files' do + is_expected.to eq([ + '/root/test/fixtures/%TABLE_NAME%.yml', + '/root/spec/fixtures/%TABLE_NAME%.yml', + '/root/test/fixtures/%PLURALIZED_MODEL_NAME%.yml', + '/root/spec/fixtures/%PLURALIZED_MODEL_NAME%.yml' + ]) + end + end + + context 'when pattern_type is "scaffold"' do + let(:pattern_type) { 'scaffold' } + + it 'returns patterns of scaffold files' do + is_expected.to eq([ + '/root/test/controllers/%PLURALIZED_MODEL_NAME%_controller_test.rb', + '/root/spec/controllers/%PLURALIZED_MODEL_NAME%_controller_spec.rb', + '/root/spec/requests/%PLURALIZED_MODEL_NAME%_spec.rb', + '/root/spec/routing/%PLURALIZED_MODEL_NAME%_routing_spec.rb' + ]) + end + end + + context 'when pattern_type is "factory"' do + let(:pattern_type) { 'factory' } + + it 'returns patterns of factory files' do + is_expected.to eq([ + '/root/test/exemplars/%MODEL_NAME%_exemplar.rb', + '/root/spec/exemplars/%MODEL_NAME%_exemplar.rb', + '/root/test/blueprints/%MODEL_NAME%_blueprint.rb', + '/root/spec/blueprints/%MODEL_NAME%_blueprint.rb', + '/root/test/factories/%MODEL_NAME%_factory.rb', + '/root/spec/factories/%MODEL_NAME%_factory.rb', + '/root/test/factories/%TABLE_NAME%.rb', + '/root/spec/factories/%TABLE_NAME%.rb', + '/root/test/factories/%PLURALIZED_MODEL_NAME%.rb', + '/root/spec/factories/%PLURALIZED_MODEL_NAME%.rb', + '/root/test/fabricators/%MODEL_NAME%_fabricator.rb', + '/root/spec/fabricators/%MODEL_NAME%_fabricator.rb' + ]) + end + end + + context 'when pattern_type is "serializer"' do + let(:pattern_type) { 'serializer' } + + it 'returns patterns of serializer files' do + is_expected.to eq([ + '/root/app/serializers/%MODEL_NAME%_serializer.rb', + '/root/test/serializers/%MODEL_NAME%_serializer_test.rb', + '/root/spec/serializers/%MODEL_NAME%_serializer_spec.rb' + ]) + end + end + + context 'when pattern_type is "additional_file_patterns"' do + let(:pattern_type) { 'additional_file_patterns' } + + context 'when additional_file_patterns is specified in the options' do + let(:additional_file_patterns) do + [ + '%PLURALIZED_MODEL_NAME%/**/*.rb', + '%PLURALIZED_MODEL_NAME%/*_form' + ] + end + + let(:options) { { additional_file_patterns: additional_file_patterns } } + + it 'returns additional_file_patterns in the argument "options"' do + is_expected.to eq(additional_file_patterns) + end + end + + context 'when additional_file_patterns is not specified in the options' do + let(:options) { {} } + + it 'returns an empty array' do + is_expected.to eq([]) + end + end + end + + context 'when pattern_type is "controller"' do + let(:pattern_type) { 'controller' } + + it 'returns patterns of controller files' do + is_expected.to eq([ + '/root/app/controllers/%PLURALIZED_MODEL_NAME%_controller.rb' + ]) + end + end + + context 'when pattern_type is "admin"' do + let(:pattern_type) { 'admin' } + + it 'returns patterns of admin files' do + is_expected.to eq([ + '/root/app/admin/%MODEL_NAME%.rb' + ]) + end + end + + context 'when pattern_type is "helper"' do + let(:pattern_type) { 'helper' } + + it 'returns patterns of helper files' do + is_expected.to eq([ + '/root/app/helpers/%PLURALIZED_MODEL_NAME%_helper.rb' + ]) + end + end + end +end diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 3f0a977bc..d9f74cf9d 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -1629,38 +1629,6 @@ def mock_column(name, type, options = {}) end end - describe '.files_by_pattern' do - subject { AnnotateModels.files_by_pattern(root_directory, pattern_type, options) } - - context 'when pattern_type is "additional_file_patterns"' do - let(:root_directory) { nil } - let(:pattern_type) { 'additional_file_patterns' } - - context 'when additional_file_patterns is specified in the options' do - let(:additional_file_patterns) do - [ - '%PLURALIZED_MODEL_NAME%/**/*.rb', - '%PLURALIZED_MODEL_NAME%/*_form' - ] - end - - let(:options) { { additional_file_patterns: additional_file_patterns } } - - it 'returns additional_file_patterns in the argument "options"' do - is_expected.to eq(additional_file_patterns) - end - end - - context 'when additional_file_patterns is not specified in the options' do - let(:options) { {} } - - it 'returns an empty array' do - is_expected.to eq([]) - end - end - end - end - describe '.get_patterns' do subject { AnnotateModels.get_patterns(options, pattern_type) }