Class: RuboCop::Cop::Rails::ReflectionClassName

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/reflection_class_name.rb

Overview

Checks if the value of the option ‘class_name`, in the definition of a reflection is a string.

Examples:

# bad
has_many :accounts, class_name: Account
has_many :accounts, class_name: Account.name

# good
has_many :accounts, class_name: 'Account'

Constant Summary collapse

MSG =
'Use a string value for `class_name`.'
RESTRICT_ON_SEND =
%i[has_many has_one belongs_to].freeze
ALLOWED_REFLECTION_CLASS_TYPES =
%i[dstr str sym].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/rails/reflection_class_name.rb', line 41

def on_send(node)
  association_with_reflection(node) do |reflection_class_name|
    return if reflection_class_name.value.send_type? && reflection_class_name.value.receiver.nil?
    return if reflection_class_name.value.lvar_type? && str_assigned?(reflection_class_name)

    add_offense(reflection_class_name) do |corrector|
      autocorrect(corrector, reflection_class_name)
    end
  end
end