Class: RuboCop::Cop::Gusto::SuddenAssociations

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/gusto/sudden_associations.rb

Overview

Detects Rails association definitions called on other classes from outside their class bodies. This kind of sudden monkey-patching can lead to unexpected behavior.

Examples:

# bad
Company.has_one :onboarding_benefits_survey
User.belongs_to :company
Post.has_many :comments
Student.has_and_belongs_to_many :courses

# good
class Company
  has_one :onboarding_benefits_survey
end

class User
  belongs_to :company
end

Constant Summary collapse

MSG =
"Do not define Rails associations on another class. Associations should only be defined within the class body of their model."
RESTRICT_ON_SEND =
i(
  has_one
  has_many
  belongs_to
  has_and_belongs_to_many
).freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object

Matches association method calls on a class constant Examples matched:

Company.has_one :onboarding_benefits_survey
User.belongs_to :company
::Company.has_many :users


38
39
40
41
42
43
# File 'lib/rubocop/cop/gusto/sudden_associations.rb', line 38

def on_send(node)
  return unless node.receiver
  return unless node.receiver.const_type?

  add_offense(node)
end