Class: Rubocop::Cop::PolymorphicAssociations

Inherits:
RuboCop::Cop::Base
  • Object
show all
Defined in:
lib/rubocop/cop/polymorphic_associations.rb

Overview

Cop that prevents the use of polymorphic associations

Examples:

# bad
class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
end

# good
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :article
end

Constant Summary collapse

MSG =
'Do not use polymorphic associations, use separate tables instead'
RESTRICT_ON_SEND =
i[belongs_to].to_set.freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



27
28
29
30
31
# File 'lib/rubocop/cop/polymorphic_associations.rb', line 27

def on_send(node)
  polymorphic_pair(node) do |pair|
    add_offense(pair)
  end
end

#polymorphic_pair(node) ⇒ Object



23
24
25
# File 'lib/rubocop/cop/polymorphic_associations.rb', line 23

def_node_matcher :polymorphic_pair, "(send _ %RESTRICT_ON_SEND ... (hash <$(pair (sym :polymorphic) _) ...>))\n"