class RuboCop::Cop::Codeur::RailsAppPatterns
This cop makes sure that Rails app only uses patterns (app subdirectories) defined in Cop
config.
@example ForbiddenPatterns: [presenters, view_objects, uploaders, modules]
# bad app/presenters/order_presenter.rb # bad app/modules/stuffy.rb # good app/view_components/order_component.rb # good app/models/concerns/stuffy.rb
@example AllowedPatterns: [assets, controllers, javascript, jobs, mailers, models, views]
# bad app/controller/some_controller.rb # good app/controllers/some_controller.rb
Constants
- MSG_FORBIDDEN
- MSG_NOT_ALLOWED
Public Instance Methods
on_new_investigation()
click to toggle source
# File lib/rubocop/cop/codeur/rails_app_patterns.rb, line 33 def on_new_investigation file_path = processed_source.file_path return if config.file_to_exclude?(file_path) for_bad_patterns(file_path) { |msg| add_global_offense(msg, severity: :warning) } end
Private Instance Methods
allowed_patterns()
click to toggle source
# File lib/rubocop/cop/codeur/rails_app_patterns.rb, line 71 def allowed_patterns cop_config['AllowedPatterns'] || [] end
for_bad_patterns(file_path) { |msg| ... }
click to toggle source
# File lib/rubocop/cop/codeur/rails_app_patterns.rb, line 42 def for_bad_patterns(file_path) pattern = pattern_from_path(file_path) return if pattern.nil? if pattern_forbidden?(pattern) msg = format(MSG_FORBIDDEN, pattern: pattern) elsif pattern_not_allowed?(pattern) msg = format(MSG_NOT_ALLOWED, pattern: pattern, allowed_patterns: allowed_patterns.map { |p| "`#{p}`" }.join(', ')) end yield msg if msg end
forbidden_patterns()
click to toggle source
# File lib/rubocop/cop/codeur/rails_app_patterns.rb, line 75 def forbidden_patterns cop_config['ForbiddenPatterns'] || [] end
pattern_forbidden?(pattern)
click to toggle source
# File lib/rubocop/cop/codeur/rails_app_patterns.rb, line 63 def pattern_forbidden?(pattern) forbidden_patterns.any? && forbidden_patterns.include?(pattern) end
pattern_from_path(path)
click to toggle source
# File lib/rubocop/cop/codeur/rails_app_patterns.rb, line 57 def pattern_from_path(path) return nil unless path.match(%r{/(?<folder>app|test)/(?<pattern>.+)/.+}) Regexp.last_match(:pattern) end
pattern_not_allowed?(pattern)
click to toggle source
# File lib/rubocop/cop/codeur/rails_app_patterns.rb, line 67 def pattern_not_allowed?(pattern) allowed_patterns.any? && !allowed_patterns.include?(pattern) end