lib/rubocop/cop/rails/inverse_of.rb in rubocop-rails-2.13.1 vs lib/rubocop/cop/rails/inverse_of.rb in rubocop-rails-2.13.2
- old
+ new
@@ -124,10 +124,22 @@
# class Patient < ApplicationRecord
# has_many :appointments
# has_many :physicians, through: :appointments
# end
#
+ # @example IgnoreScopes: false (default)
+ # # bad
+ # class Blog < ApplicationRecord
+ # has_many :posts, -> { order(published_at: :desc) }
+ # end
+ #
+ # @example IgnoreScopes: true
+ # # good
+ # class Blog < ApplicationRecord
+ # has_many :posts, -> { order(published_at: :desc) }
+ # end
+ #
# @see https://guides.rubyonrails.org/association_basics.html#bi-directional-associations
# @see https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses
class InverseOf < Base
SPECIFY_MSG = 'Specify an `:inverse_of` option.'
NIL_MSG = 'You specified `inverse_of: nil`, you probably meant to use `inverse_of: false`.'
@@ -187,11 +199,11 @@
add_offense(node.loc.selector, message: message(options))
end
def scope?(arguments)
- arguments.any?(&:block_type?)
+ !ignore_scopes? && arguments.any?(&:block_type?)
end
def options_requiring_inverse_of?(options)
required = options.any? do |opt|
conditions_option?(opt) ||
@@ -233,9 +245,13 @@
if options.any? { |opt| inverse_of_nil_option?(opt) }
NIL_MSG
else
SPECIFY_MSG
end
+ end
+
+ def ignore_scopes?
+ cop_config['IgnoreScopes'] == true
end
end
end
end
end