class RuboCop::Cop::Rails::FindEach
This cop is used to identify usages of `all.each` and change them to use `all.find_each` instead.
@example
# bad User.all.each # good User.all.find_each
Constants
- IGNORED_METHODS
- MSG
- SCOPE_METHODS
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/rails/find_each.rb, line 30 def autocorrect(node) ->(corrector) { corrector.replace(node.loc.selector, 'find_each') } end
on_send(node)
click to toggle source
# File lib/rubocop/cop/rails/find_each.rb, line 21 def on_send(node) return unless node.receiver && node.method?(:each) return unless SCOPE_METHODS.include?(node.receiver.method_name) return if method_chain(node).any? { |m| ignored_by_find_each?(m) } add_offense(node, location: :selector) end
Private Instance Methods
ignored_by_find_each?(relation_method)
click to toggle source
# File lib/rubocop/cop/rails/find_each.rb, line 40 def ignored_by_find_each?(relation_method) # Active Record's #find_each ignores various extra parameters IGNORED_METHODS.include?(relation_method) end
method_chain(node)
click to toggle source
# File lib/rubocop/cop/rails/find_each.rb, line 36 def method_chain(node) [*node.ancestors, node].map(&:method_name) end