Class: RuboCop::Cop::Rails::DuplicateAssociation
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::DuplicateAssociation
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp, ActiveRecordHelper, ClassSendNodeHelper
- Defined in:
- lib/rubocop/cop/rails/duplicate_association.rb
Overview
Looks for associations that have been defined multiple times in the same file.
When an association is defined multiple times on a model, Active Record overrides the previously defined association with the new one. Because of this, this cop’s autocorrection simply keeps the last of any duplicates and discards the rest.
Constant Summary collapse
- MSG =
"Association `%<name>s` is defined multiple times. Don't repeat associations."
Constants included from ActiveRecordHelper
ActiveRecordHelper::WHERE_METHODS
Instance Method Summary collapse
Methods included from ActiveRecordHelper
#external_dependency_checksum, #foreign_key_of, #in_where?, #inherit_active_record_base?, #polymorphic?, #resolve_relation_into_column, #schema, #table_name
Methods included from ClassSendNodeHelper
Instance Method Details
#on_class(class_node) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rubocop/cop/rails/duplicate_association.rb', line 35 def on_class(class_node) return unless active_record?(class_node.parent_class) offenses(class_node).each do |name, nodes| nodes.each do |node| add_offense(node, message: format(MSG, name: name)) do |corrector| next if same_line?(nodes.last, node) corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true)) end end end end |