Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #1340] Fix a false positive for Rails/WhereEquals when qualifying the database name #1341

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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