Sha256: b676f4305d5f19edee453c4bf5798827d55c94f2d52d1131f8651ab47e0c1326

Contents?: true

Size: 1.48 KB

Versions: 21

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop 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

21 entries across 21 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/default_scope.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.14.2/lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.14.2 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.14.1 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.14.0 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.13.2 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.13.1 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.13.0 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.12.4 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.12.3 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.10.1 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.10.0 lib/rubocop/cop/rails/default_scope.rb
rubocop-rails-2.9.1 lib/rubocop/cop/rails/default_scope.rb