Sha256: edfe1130fcb3a685bb28439b7e329b9a8667e3e7083a02e845350287e6444bd5

Contents?: true

Size: 1.29 KB

Versions: 47

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Checks for multiple scopes in a model that have the same `where` clause. This
      # often means you copy/pasted a scope, updated the name, and forgot to change the condition.
      #
      # @example
      #
      #   # bad
      #   scope :visible, -> { where(visible: true) }
      #   scope :hidden, -> { where(visible: true) }
      #
      #   # good
      #   scope :visible, -> { where(visible: true) }
      #   scope :hidden, -> { where(visible: false) }
      #
      class DuplicateScope < Base
        include ClassSendNodeHelper

        MSG = 'Multiple scopes share this same where clause.'

        def_node_matcher :scope, <<~PATTERN
          (send nil? :scope _ $...)
        PATTERN

        def on_class(class_node)
          offenses(class_node).each do |node|
            add_offense(node)
          end
        end

        private

        def offenses(class_node)
          class_send_nodes(class_node).select { |node| scope(node) }
                                      .group_by { |node| scope(node) }
                                      .select { |_, nodes| nodes.length > 1 }
                                      .values
                                      .flatten
        end
      end
    end
  end
end

Version data entries

47 entries across 44 versions & 7 rubygems

Version Path
rubocop-rails-2.29.0 lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.28.0 lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.27.0 lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/duplicate_scope.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/duplicate_scope.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/duplicate_scope.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/duplicate_scope.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/duplicate_scope.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/duplicate_scope.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/duplicate_scope.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/duplicate_scope.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/duplicate_scope.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/duplicate_scope.rb