Class: RuboCop::Cop::DarkFinger::MigrationConstants

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/dark_finger/migration_constants.rb

Constant Summary collapse

DEFAULT_ALLOWED_CONSTANTS =
[
  'Migration',
  'ActiveRecord',
  'ActiveRecord::Migration',
  'ActiveRecord::Base',
  'ActiveRecord::IrreversibleMigration'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, options) ⇒ MigrationConstants

Returns a new instance of MigrationConstants.



15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#allowed_constantsObject (readonly)

Returns the value of attribute allowed_constants.



13
14
15
# File 'lib/rubocop/cop/dark_finger/migration_constants.rb', line 13

def allowed_constants
  @allowed_constants
end

Instance Method Details

#on_casgn(node) ⇒ Object



29
30
31
# 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) ⇒ Object



33
34
35
36
# 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) ⇒ Object



24
25
26
27
# 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) ⇒ Object



38
39
40
41
# 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