class RuboCop::Cop::Highlands::ClassName
Cop
to prevent cross-model references, which result in a cascade of autoloads. E.g., belongs_to :user, :class_name => User.name
Constants
- MSG
Public Instance Methods
on_send(node)
click to toggle source
Is this a has_many, has_one, or belongs_to with a :class_name arg? Make sure the class name is a hardcoded string. If not, add an offense and return true.
# File lib/rubocop/cop/highlands/class_name.rb, line 13 def on_send(node) association_statement = node.command?(:has_many) || node.command?(:has_one) || node.command?(:belongs_to) return unless association_statement class_pair = class_name_node(node) if class_pair && !string_class_name?(class_pair) add_offense(class_pair) end end
Private Instance Methods
class_name_node(node)
click to toggle source
Return the descendant node that is a hash pair (:key => value) whose key is :class_name.
# File lib/rubocop/cop/highlands/class_name.rb, line 32 def class_name_node(node) node.descendants.detect do |e| e.is_a?(Parser::AST::Node) && e.pair_type? && e.children[0].children[0] == :class_name end end
string_class_name?(class_pair)
click to toggle source
Given a hash pair :class_name => value, is the value a hardcoded string?
# File lib/rubocop/cop/highlands/class_name.rb, line 41 def string_class_name?(class_pair) class_pair.children[1].str_type? end