Class: RuboCop::Cop::Rails::RedundantForeignKey
- Inherits:
-
RuboCop::Cop
- Object
- RuboCop::Cop
- RuboCop::Cop::Rails::RedundantForeignKey
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/rails/redundant_foreign_key.rb
Overview
This cop detects cases where the ‘:foreign_key` option on associations is redundant.
Constant Summary collapse
- MSG =
'Specifying the default value for `foreign_key` is redundant.'
Instance Method Summary collapse
Instance Method Details
#autocorrect(node) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/rubocop/cop/rails/redundant_foreign_key.rb', line 46 def autocorrect(node) _type, _name, , foreign_key_pair, _foreign_key = association_with_foreign_key(node) range = range_with_surrounding_space(range: foreign_key_pair.source_range, side: :left) range = range_with_surrounding_comma(range, :left) lambda do |corrector| corrector.remove(range) end end |
#on_send(node) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/rubocop/cop/rails/redundant_foreign_key.rb', line 38 def on_send(node) association_with_foreign_key(node) do |type, name, , foreign_key_pair, foreign_key| if redundant?(node, type, name, , foreign_key) add_offense(node, location: foreign_key_pair.loc.expression) end end end |