class RuboCop::Cop::DarkFinger::MigrationConstants

Constants

DEFAULT_ALLOWED_CONSTANTS

Attributes

allowed_constants[R]

Public Class Methods

new(*args, options) click to toggle source
Calls superclass method
# File lib/rubocop/cop/dark_finger/migration_constants.rb, line 15
def initialize(*args, options)
  super(*args)
  @whitelisted_constants = options[:whitelisted_constants] || cop_config['whitelisted_constants'] || []
  @allowed_constants =
    DEFAULT_ALLOWED_CONSTANTS +
    allowed_top_level_constants +
    @whitelisted_constants
end

Public Instance Methods

on_casgn(node) click to toggle source
# File lib/rubocop/cop/dark_finger/migration_constants.rb, line 29
def on_casgn(node)
  add_allowed_constant(node.children[1])
end
on_class(node) click to toggle source
# File lib/rubocop/cop/dark_finger/migration_constants.rb, line 33
def on_class(node)
  add_allowed_constant(node.children.first.const_name)
  add_module_parent_chain_for(node)
end
on_const(node) click to toggle source
# File lib/rubocop/cop/dark_finger/migration_constants.rb, line 24
def on_const(node)
  return if allowed_constants.include?(node.const_name)
  add_offense(node, message: %Q(Undeclared constant: "#{node.const_name}"))
end
on_module(node) click to toggle source
# File lib/rubocop/cop/dark_finger/migration_constants.rb, line 38
def on_module(node)
  add_allowed_constant(node.children.first.const_name)
  add_module_parent_chain_for(node)
end

Private Instance Methods

add_allowed_constant(constant) click to toggle source
# File lib/rubocop/cop/dark_finger/migration_constants.rb, line 57
def add_allowed_constant(constant)
  @allowed_constants << constant.to_s
  @allowed_constants.uniq!
end
add_module_parent_chain_for(node) click to toggle source
# File lib/rubocop/cop/dark_finger/migration_constants.rb, line 62
def add_module_parent_chain_for(node)
  chain = ModuleAncestorChainExtractor.new(node).perform
  add_allowed_constant(chain)
end
allowed_top_level_constants() click to toggle source
# File lib/rubocop/cop/dark_finger/migration_constants.rb, line 45
def allowed_top_level_constants
  Module.constants.map(&:to_s) - top_level_model_classes_and_containing_modules
end
top_level_model_classes_and_containing_modules() click to toggle source
# File lib/rubocop/cop/dark_finger/migration_constants.rb, line 49
def top_level_model_classes_and_containing_modules
  return [] unless Object.const_defined?('ActiveRecord::Base')

  ::ActiveRecord::Base.descendants.map do |klass|
    klass.name.sub(/::.*/, '').to_s
  end.uniq
end