Class: RuboCop::Cop::Rails::InverseOf

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rails/inverse_of.rb

Overview

Looks for has_(one|many) and belongs_to associations where Active Record can’t automatically determine the inverse association because of a scope or the options used. Using the blog with order scope example below, traversing the a Blog’s association in both directions with ‘blog.posts.first.blog` would cause the `blog` to be loaded from the database twice.

‘:inverse_of` must be manually specified for Active Record to use the associated object in memory, or set to `false` to opt-out. Note that setting `nil` does not stop Active Record from trying to determine the inverse automatically, and is not considered a valid value for this.

Examples:

# good
class Blog < ApplicationRecord
  has_many :posts
end

class Post < ApplicationRecord
  belongs_to :blog
end
# bad
class Blog < ApplicationRecord
  has_many :posts, -> { order(published_at: :desc) }
end

class Post < ApplicationRecord
  belongs_to :blog
end

# good
class Blog < ApplicationRecord
  has_many(:posts,
           -> { order(published_at: :desc) },
           inverse_of: :blog)
end

class Post < ApplicationRecord
  belongs_to :blog
end

# good
class Blog < ApplicationRecord
  with_options inverse_of: :blog do
    has_many :posts, -> { order(published_at: :desc) }
  end
end

class Post < ApplicationRecord
  belongs_to :blog
end

# good
# When you don't want to use the inverse association.
class Blog < ApplicationRecord
  has_many(:posts,
           -> { order(published_at: :desc) },
           inverse_of: false)
end
# bad
class Picture < ApplicationRecord
  belongs_to :imageable, polymorphic: true
end

class Employee < ApplicationRecord
  has_many :pictures, as: :imageable
end

class Product < ApplicationRecord
  has_many :pictures, as: :imageable
end

# good
class Picture < ApplicationRecord
  belongs_to :imageable, polymorphic: true
end

class Employee < ApplicationRecord
  has_many :pictures, as: :imageable, inverse_of: :imageable
end

class Product < ApplicationRecord
  has_many :pictures, as: :imageable, inverse_of: :imageable
end
# bad
# However, RuboCop can not detect this pattern...
class Physician < ApplicationRecord
  has_many :appointments
  has_many :patients, through: :appointments
end

class Appointment < ApplicationRecord
  belongs_to :physician
  belongs_to :patient
end

class Patient < ApplicationRecord
  has_many :appointments
  has_many :physicians, through: :appointments
end

# good
class Physician < ApplicationRecord
  has_many :appointments
  has_many :patients, through: :appointments
end

class Appointment < ApplicationRecord
  belongs_to :physician, inverse_of: :appointments
  belongs_to :patient, inverse_of: :appointments
end

class Patient < ApplicationRecord
  has_many :appointments
  has_many :physicians, through: :appointments
end

IgnoreScopes: false (default)

# bad
class Blog < ApplicationRecord
  has_many :posts, -> { order(published_at: :desc) }
end

IgnoreScopes: true

# good
class Blog < ApplicationRecord
  has_many :posts, -> { order(published_at: :desc) }
end

Constant Summary collapse

SPECIFY_MSG =
'Specify an `:inverse_of` option.'
NIL_MSG =
'You specified `inverse_of: nil`, you probably meant to use `inverse_of: false`.'
RESTRICT_ON_SEND =
%i[has_many has_one belongs_to].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rubocop/cop/rails/inverse_of.rb', line 181

def on_send(node)
  recv, arguments = association_recv_arguments(node)
  return unless arguments

  with_options = with_options_arguments(recv, node)

  options = arguments.concat(with_options).flat_map do |arg|
    options_from_argument(arg)
  end
  return if options_ignoring_inverse_of?(options)

  return unless scope?(arguments) || options_requiring_inverse_of?(options)

  return if options_contain_inverse_of?(options)

  add_offense(node.loc.selector, message: message(options))
end

#options_contain_inverse_of?(options) ⇒ Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/rubocop/cop/rails/inverse_of.rb', line 219

def options_contain_inverse_of?(options)
  options.any? { |opt| inverse_of_option?(opt) }
end

#options_ignoring_inverse_of?(options) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
217
# File 'lib/rubocop/cop/rails/inverse_of.rb', line 213

def options_ignoring_inverse_of?(options)
  options.any? do |opt|
    through_option?(opt) || polymorphic_option?(opt)
  end
end

#options_requiring_inverse_of?(options) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
206
207
208
209
210
211
# File 'lib/rubocop/cop/rails/inverse_of.rb', line 203

def options_requiring_inverse_of?(options)
  required = options.any? do |opt|
    conditions_option?(opt) || foreign_key_option?(opt)
  end

  return required if target_rails_version >= 5.2

  required || options.any? { |opt| as_option?(opt) }
end

#same_context_in_with_options?(arg, recv) ⇒ Boolean

Returns:

  • (Boolean)


230
231
232
233
234
# File 'lib/rubocop/cop/rails/inverse_of.rb', line 230

def same_context_in_with_options?(arg, recv)
  return true if arg.nil? && recv.nil?

  arg && recv && arg.children[0] == recv.children[0]
end

#scope?(arguments) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/rubocop/cop/rails/inverse_of.rb', line 199

def scope?(arguments)
  !ignore_scopes? && arguments.any?(&:block_type?)
end

#with_options_arguments(recv, node) ⇒ Object



223
224
225
226
227
228
# File 'lib/rubocop/cop/rails/inverse_of.rb', line 223

def with_options_arguments(recv, node)
  blocks = node.each_ancestor(:block).select do |block|
    block.send_node.command?(:with_options) && same_context_in_with_options?(block.first_argument, recv)
  end
  blocks.flat_map { |n| n.send_node.arguments }
end