Skip to content

Commit 523fed1

Browse files
authored
Merge pull request #1291 from fatkodima/fix-where_range-spaces
[Fix #1282] Fix `WhereRange` to correctly handle template strings with extra spaces
2 parents 07bec34 + 027a695 commit 523fed1

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

changelog/fix_where_range_spaces.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1282](https://github.com/rubocop/rubocop-rails/issues/1282): Fix `WhereRange` to correctly handle template strings with extra spaces. ([@fatkodima][])

lib/rubocop/cop/rails/where_range.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ class WhereRange < Base
3737
RESTRICT_ON_SEND = %i[where not].freeze
3838

3939
# column >= ?
40-
GTEQ_ANONYMOUS_RE = /\A([\w.]+)\s+>=\s+\?\z/.freeze
40+
GTEQ_ANONYMOUS_RE = /\A\s*([\w.]+)\s+>=\s+\?\s*\z/.freeze
4141
# column <[=] ?
42-
LTEQ_ANONYMOUS_RE = /\A([\w.]+)\s+(<=?)\s+\?\z/.freeze
42+
LTEQ_ANONYMOUS_RE = /\A\s*([\w.]+)\s+(<=?)\s+\?\s*\z/.freeze
4343
# column >= ? AND column <[=] ?
44-
RANGE_ANONYMOUS_RE = /\A([\w.]+)\s+>=\s+\?\s+AND\s+\1\s+(<=?)\s+\?\z/i.freeze
44+
RANGE_ANONYMOUS_RE = /\A\s*([\w.]+)\s+>=\s+\?\s+AND\s+\1\s+(<=?)\s+\?\s*\z/i.freeze
4545
# column >= :value
46-
GTEQ_NAMED_RE = /\A([\w.]+)\s+>=\s+:(\w+)\z/.freeze
46+
GTEQ_NAMED_RE = /\A\s*([\w.]+)\s+>=\s+:(\w+)\s*\z/.freeze
4747
# column <[=] :value
48-
LTEQ_NAMED_RE = /\A([\w.]+)\s+(<=?)\s+:(\w+)\z/.freeze
48+
LTEQ_NAMED_RE = /\A\s*([\w.]+)\s+(<=?)\s+:(\w+)\s*\z/.freeze
4949
# column >= :value1 AND column <[=] :value2
50-
RANGE_NAMED_RE = /\A([\w.]+)\s+>=\s+:(\w+)\s+AND\s+\1\s+(<=?)\s+:(\w+)\z/i.freeze
50+
RANGE_NAMED_RE = /\A\s*([\w.]+)\s+>=\s+:(\w+)\s+AND\s+\1\s+(<=?)\s+:(\w+)\s*\z/i.freeze
5151

5252
minimum_target_ruby_version 2.6
5353
minimum_target_rails_version 6.0

spec/rubocop/cop/rails/where_range_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@
142142
RUBY
143143
end
144144

145+
it 'correctly handles spaces in the template string' do
146+
expect_offense(<<~RUBY)
147+
Model.where(' column >= ? ', value)
148+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value..)` instead of manually constructing SQL.
149+
Model.where(' column >= :min ', min: value)
150+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value..)` instead of manually constructing SQL.
151+
Model.where(' column >= ? AND column < ? ', value1, value2)
152+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value1...value2)` instead of manually constructing SQL.
153+
Model.where(' column >= :min AND column < :max ', min: value1, max: value2)
154+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value1...value2)` instead of manually constructing SQL.
155+
RUBY
156+
end
157+
145158
it 'does not register an offense when using ranges' do
146159
expect_no_offenses(<<~RUBY)
147160
Model.where(column: value..)

0 commit comments

Comments
 (0)