Class: RuboCop::Cop::Rails::MandatoryInverseOf
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::MandatoryInverseOf
- Defined in:
- lib/rubocop/cop/rails/mandatory_inverse_of.rb
Overview
Ensures that every ActiveRecord association (belongs_to, has_many, has_one) has an ‘inverse_of` option.
Stricter than the stock ‘Rails/InverseOf`, which only flags associations where Active Record cannot auto-detect the inverse. This cop mandates `inverse_of` on every association. Disable the stock cop when enabling this one.
Constant Summary collapse
- MSG =
'All associations must declare `inverse_of`.'
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.default_configuration ⇒ Object
26 27 28 29 30 31 |
# File 'lib/rubocop/cop/rails/mandatory_inverse_of.rb', line 26 def self.default_configuration super.merge( 'Include' => ['app/models/**/*.rb'], 'Exclude' => ['app/serializers/**/*.rb'] ) end |
Instance Method Details
#on_send(node) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rubocop/cop/rails/mandatory_inverse_of.rb', line 33 def on_send(node) return unless in_model_file?(processed_source.file_path) return unless association?(node) return if polymorphic_or_through?(node) kwargs = node.last_argument missing_inverse_of = !kwargs || !kwargs.hash_type? || kwargs.keys.none? { |k| k.value == :inverse_of } add_offense(node.loc.selector) if missing_inverse_of rescue StandardError => e source_name = if processed_source && processed_source.buffer processed_source.file_path else 'unknown' end warn "Rails/MandatoryInverseOf failed on #{source_name}: #{e.}" end |