Sha256: cba364cc421a6f48fac65f32271d827c7b6cbc815d811f1af1a4d71b6a59108b

Contents?: true

Size: 1.47 KB

Versions: 51

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Looks for uses of `default_scope`.
      #
      # @example
      #   # bad
      #   default_scope -> { where(hidden: false) }
      #
      #   # good
      #   scope :published, -> { where(hidden: false) }
      #
      #   # bad
      #   def self.default_scope
      #     where(hidden: false)
      #   end
      #
      #   # good
      #   def self.published
      #     where(hidden: false)
      #   end
      #
      class DefaultScope < Base
        MSG = 'Avoid use of `default_scope`. It is better to use explicitly named scopes.'
        RESTRICT_ON_SEND = %i[default_scope].freeze

        def_node_matcher :method_call?, <<~PATTERN
          (send nil? :default_scope ...)
        PATTERN

        def_node_matcher :class_method_definition?, <<~PATTERN
          (defs _ :default_scope args ...)
        PATTERN

        def_node_matcher :eigenclass_method_definition?, <<~PATTERN
          (sclass (self) $(def :default_scope args ...))
        PATTERN

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

          add_offense(node.loc.selector)
        end

        def on_defs(node)
          return unless class_method_definition?(node)

          add_offense(node.loc.name)
        end

        def on_sclass(node)
          eigenclass_method_definition?(node) do |default_scope|
            add_offense(default_scope.loc.name)
          end
        end
      end
    end
  end
end

Version data entries

51 entries across 48 versions & 8 rubygems

Version Path
rubocop-rails-2.30.1 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.30.0 lib/rubocop/cop/rails/default_scope.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-rails-2.29.1/lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.29.1 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.29.0 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.28.0 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.27.0 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/default_scope.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/default_scope.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/default_scope.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/default_scope.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/default_scope.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/default_scope.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/default_scope.rb