Sha256: 6a068bb5c076d428bcac011593f50085dc419c86c662e272775aeb4ed3196632

Contents?: true

Size: 1.63 KB

Versions: 6484

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop is used to identify usages of `where.first` and
      # change them to use `find_by` instead.
      #
      # @example
      #   # bad
      #   User.where(name: 'Bruce').first
      #   User.where(name: 'Bruce').take
      #
      #   # good
      #   User.find_by(name: 'Bruce')
      class FindBy < Cop
        include RangeHelp

        MSG = 'Use `find_by` instead of `where.%<method>s`.'.freeze
        TARGET_SELECTORS = %i[first take].freeze

        def_node_matcher :where_first?, <<-PATTERN
          (send ({send csend} _ :where ...) {:first :take})
        PATTERN

        def on_send(node)
          return unless where_first?(node)

          range = range_between(node.receiver.loc.selector.begin_pos,
                                node.loc.selector.end_pos)

          add_offense(node, location: range,
                            message: format(MSG, method: node.method_name))
        end
        alias on_csend on_send

        def autocorrect(node)
          # Don't autocorrect where(...).first, because it can return different
          # results from find_by. (They order records differently, so the
          # 'first' record can be different.)
          return if node.method?(:first)

          where_loc = node.receiver.loc.selector
          first_loc = range_between(node.loc.dot.begin_pos,
                                    node.loc.selector.end_pos)

          lambda do |corrector|
            corrector.replace(where_loc, 'find_by')
            corrector.replace(first_loc, '')
          end
        end
      end
    end
  end
end

Version data entries

6,484 entries across 6,480 versions & 23 rubygems

Version Path
affixapi-1.1.102 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
mux_ruby-3.20.0 vendor/bundle/ruby/3.2.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_bank_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_organization_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_id_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_bank_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_organization_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_id_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_id_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_bank_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_organization_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_organization_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_id_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_bank_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
ory-client-1.15.12 vendor/bundle/ruby/3.1.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_bank_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_id_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_organization_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_bank_ruby-0.123.3 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb
cybrid_api_organization_ruby-0.123.3 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/rails/find_by.rb