Class: RuboCop::Cop::Captive::StringWhereInScope

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/captive/string_where_in_scope.rb

Overview

Prevents to use ‘where` with string that contains SQL apart from model. Like `where(’date > ?‘, Date.current)` there, you writing a part of SQL. That’s why it’s important to write this, into a model, in order to respect the MVC architecture

Examples:

# bad
where('date > ?', Date.current)

# good
date_after(Date.current)

scope :date_after, ->(date) { where('date > ?', date) }

Constant Summary collapse

MSG =
"The `where` method should be used in a scope in a model."

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rubocop/cop/captive/string_where_in_scope.rb', line 27

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

  options_node = node.arguments[0]
  return if options_node&.hash_type?

  add_offense(options_node, message: MSG)
end