Class: RuboCop::Cop::Rails::WhereEquals
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::WhereEquals
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/rails/where_equals.rb
Overview
Identifies places where manually constructed SQL in ‘where` can be replaced with `where(attribute: value)`.
Constant Summary collapse
- MSG =
'Use `%<good_method>s` instead of manually constructing SQL.'- RESTRICT_ON_SEND =
%i[where].freeze
- EQ_ANONYMOUS_RE =
column = ?
/\A([\w.]+)\s+=\s+\?\z/.freeze
- IN_ANONYMOUS_RE =
column IN (?)
/\A([\w.]+)\s+IN\s+\(\?\)\z/i.freeze
- EQ_NAMED_RE =
column = :column
/\A([\w.]+)\s+=\s+:(\w+)\z/.freeze
- IN_NAMED_RE =
column IN (:column)
/\A([\w.]+)\s+IN\s+\(:(\w+)\)\z/i.freeze
- IS_NULL_RE =
column IS NULL
/\A([\w.]+)\s+IS\s+NULL\z/i.freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rubocop/cop/rails/where_equals.rb', line 41 def on_send(node) where_method_call?(node) do |template_node, value_node| value_node = value_node.first range = offense_range(node) column_and_value = extract_column_and_value(template_node, value_node) return unless column_and_value good_method = build_good_method(*column_and_value) = format(MSG, good_method: good_method) add_offense(range, message: ) do |corrector| corrector.replace(range, good_method) end end end |