Class: RuboCop::Cop::Rails::DefaultScope

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rails/default_scope.rb

Overview

Looks for uses of ‘default_scope`.

Examples:

# 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

Constant Summary collapse

MSG =
'Avoid use of `default_scope`. It is better to use explicitly named scopes.'
RESTRICT_ON_SEND =
%i[default_scope].freeze

Instance Method Summary collapse

Instance Method Details

#on_defs(node) ⇒ Object



47
48
49
50
51
# File 'lib/rubocop/cop/rails/default_scope.rb', line 47

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

  add_offense(node.loc.name)
end

#on_sclass(node) ⇒ Object



53
54
55
56
57
# File 'lib/rubocop/cop/rails/default_scope.rb', line 53

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

#on_send(node) ⇒ Object



41
42
43
44
45
# File 'lib/rubocop/cop/rails/default_scope.rb', line 41

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

  add_offense(node.loc.selector)
end