class RuboCop::Cop::Rails::ReflectionClassName

This cop checks if the value of the option `class_name`, in the definition of a reflection is a string. It is marked as unsafe because it cannot be determined whether constant or method return value specified to `class_name` is a string.

@example

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

# good
has_many :accounts, class_name: 'Account'

Constants

ALLOWED_REFLECTION_CLASS_TYPES
MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rails/reflection_class_name.rb, line 33
def on_send(node)
  association_with_reflection(node) do |reflection_class_name|
    add_offense(reflection_class_name.loc.expression)
  end
end

Private Instance Methods

reflection_class_value?(class_value) click to toggle source
# File lib/rubocop/cop/rails/reflection_class_name.rb, line 41
def reflection_class_value?(class_value)
  if class_value.send_type?
    !class_value.method?(:to_s) || class_value.receiver&.const_type?
  else
    !ALLOWED_REFLECTION_CLASS_TYPES.include?(class_value.type)
  end
end