Skip to content

Commit

Permalink
Merge pull request #1341 from Earlopain/where-equals-database-qualifier
Browse files Browse the repository at this point in the history
[Fix #1340] Fix a false positive for `Rails/WhereEquals` when qualifying the database name
  • Loading branch information
koic authored Aug 27, 2024
2 parents da11209 + 5c975e5 commit 4c1b7d7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_false_positive_database_qualified.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1340](https://github.com/rubocop/rubocop-rails/issues/1340): Fix a false positive for `Rails/WhereEquals`, `Rails/WhereNot`, and `Rails/WhereRange` when qualifying the database name. ([@earlopain][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/rails/where_equals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def offense_range(node)
range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
end

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
def extract_column_and_value(template_node, value_node)
value =
case template_node.value
Expand All @@ -90,8 +91,12 @@ def extract_column_and_value(template_node, value_node)
return
end

[Regexp.last_match(1), value]
column_qualifier = Regexp.last_match(1)
return if column_qualifier.count('.') > 1

[column_qualifier, value]
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

def build_good_method(method_name, column, value)
if column.include?('.')
Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/rails/where_not.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def offense_range(node)
range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
end

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
def extract_column_and_value(template_node, value_node)
value =
case template_node.value
Expand All @@ -84,8 +85,12 @@ def extract_column_and_value(template_node, value_node)
return
end

[Regexp.last_match(1), value]
column_qualifier = Regexp.last_match(1)
return if column_qualifier.count('.') > 1

[column_qualifier, value]
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

def build_good_method(dot, column, value)
dot ||= '.'
Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/rails/where_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def extract_column_and_value(template_node, values_node)
rhs = pair2.value
end
end
else
return
end

if lhs
Expand All @@ -150,7 +152,10 @@ def extract_column_and_value(template_node, values_node)
rhs_source = parentheses_needed?(rhs) ? "(#{rhs.source})" : rhs.source
end

[Regexp.last_match(1), "#{lhs_source}#{operator}#{rhs_source}"] if operator
column_qualifier = Regexp.last_match(1)
return if column_qualifier.count('.') > 1

[column_qualifier, "#{lhs_source}#{operator}#{rhs_source}"] if operator
end
# rubocop:enable Metrics

Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/where_equals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,10 @@
users.not('name = ?', 'Gabe')
RUBY
end

it 'does not register an offense when qualifying the database' do
expect_no_offenses(<<~RUBY)
User.where('database.users.name = ?', 'Gabe')
RUBY
end
end
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/where_not_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,10 @@
User.where('name <> ? AND age <> ?', 'john', 19)
RUBY
end

it 'does not register an offense when qualifying the database' do
expect_no_offenses(<<~RUBY)
User.where('database.users.name != ?', 'Gabe')
RUBY
end
end
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/where_range_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@
Model.where(column: ...value)
RUBY
end

it 'does not register an offense when qualifying the database' do
expect_no_offenses(<<~RUBY)
Model.where('database.table.column >= ?', value)
RUBY
end
end
end
end

0 comments on commit 4c1b7d7

Please sign in to comment.