Skip to content

Commit

Permalink
[Fix rubocop#1282] Fix WhereRange to correctly handle template stri…
Browse files Browse the repository at this point in the history
…ngs with extra spaces
  • Loading branch information
fatkodima committed Jun 7, 2024
1 parent 07bec34 commit 027a695
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/fix_where_range_spaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1282](https://github.com/rubocop/rubocop-rails/issues/1282): Fix `WhereRange` to correctly handle template strings with extra spaces. ([@fatkodima][])
12 changes: 6 additions & 6 deletions lib/rubocop/cop/rails/where_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ class WhereRange < Base
RESTRICT_ON_SEND = %i[where not].freeze

# column >= ?
GTEQ_ANONYMOUS_RE = /\A([\w.]+)\s+>=\s+\?\z/.freeze
GTEQ_ANONYMOUS_RE = /\A\s*([\w.]+)\s+>=\s+\?\s*\z/.freeze
# column <[=] ?
LTEQ_ANONYMOUS_RE = /\A([\w.]+)\s+(<=?)\s+\?\z/.freeze
LTEQ_ANONYMOUS_RE = /\A\s*([\w.]+)\s+(<=?)\s+\?\s*\z/.freeze
# column >= ? AND column <[=] ?
RANGE_ANONYMOUS_RE = /\A([\w.]+)\s+>=\s+\?\s+AND\s+\1\s+(<=?)\s+\?\z/i.freeze
RANGE_ANONYMOUS_RE = /\A\s*([\w.]+)\s+>=\s+\?\s+AND\s+\1\s+(<=?)\s+\?\s*\z/i.freeze
# column >= :value
GTEQ_NAMED_RE = /\A([\w.]+)\s+>=\s+:(\w+)\z/.freeze
GTEQ_NAMED_RE = /\A\s*([\w.]+)\s+>=\s+:(\w+)\s*\z/.freeze
# column <[=] :value
LTEQ_NAMED_RE = /\A([\w.]+)\s+(<=?)\s+:(\w+)\z/.freeze
LTEQ_NAMED_RE = /\A\s*([\w.]+)\s+(<=?)\s+:(\w+)\s*\z/.freeze
# column >= :value1 AND column <[=] :value2
RANGE_NAMED_RE = /\A([\w.]+)\s+>=\s+:(\w+)\s+AND\s+\1\s+(<=?)\s+:(\w+)\z/i.freeze
RANGE_NAMED_RE = /\A\s*([\w.]+)\s+>=\s+:(\w+)\s+AND\s+\1\s+(<=?)\s+:(\w+)\s*\z/i.freeze

minimum_target_ruby_version 2.6
minimum_target_rails_version 6.0
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/rails/where_range_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@
RUBY
end

it 'correctly handles spaces in the template string' do
expect_offense(<<~RUBY)
Model.where(' column >= ? ', value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value..)` instead of manually constructing SQL.
Model.where(' column >= :min ', min: value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value..)` instead of manually constructing SQL.
Model.where(' column >= ? AND column < ? ', value1, value2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value1...value2)` instead of manually constructing SQL.
Model.where(' column >= :min AND column < :max ', min: value1, max: value2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value1...value2)` instead of manually constructing SQL.
RUBY
end

it 'does not register an offense when using ranges' do
expect_no_offenses(<<~RUBY)
Model.where(column: value..)
Expand Down

0 comments on commit 027a695

Please sign in to comment.