Skip to content
Snippets Groups Projects
Unverified Commit cdec4a94 authored by Alper Akgun's avatar Alper Akgun
Browse files

DistinctCountByLargeForeignKey should only work for batch counts

parent fe61831f
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -22,6 +22,7 @@ module RuboCop
def on_send(node)
distinct_count?(node) do |method_name, method_arguments|
next unless method_arguments && method_arguments.length >= 2
next if batch_set_to_false?(method_arguments[2])
next if allowed_foreign_key?(method_arguments[1])
 
add_offense(node, location: :selector, message: format(MSG, method_name))
Loading
Loading
@@ -37,6 +38,21 @@ module RuboCop
def allowed_foreign_keys
(cop_config['AllowedForeignKeys'] || []).map(&:to_s)
end
def batch_set_to_false?(options)
return false unless options.is_a?(RuboCop::AST::HashNode)
batch_set_to_false = false
options.each_pair do |key, value|
next unless value.boolean_type? && value.falsey_literal?
next unless key.type == :sym && key.value == :batch
batch_set_to_false = true
break
end
batch_set_to_false
end
end
end
end
Loading
Loading
Loading
Loading
@@ -21,11 +21,23 @@ RSpec.describe RuboCop::Cop::UsageData::DistinctCountByLargeForeignKey, type: :r
subject(:cop) { described_class.new(config) }
 
context 'when counting by disallowed key' do
it 'register an offence' do
it 'registers an offence' do
inspect_source('distinct_count(Issue, :creator_id)')
 
expect(cop.offenses.size).to eq(1)
end
it 'does not register an offence when batch is false' do
inspect_source('distinct_count(Issue, :creator_id, batch: false)')
expect(cop.offenses).to be_empty
end
it 'register an offence when batch is true' do
inspect_source('distinct_count(Issue, :creator_id, batch: true)')
expect(cop.offenses.size).to eq(1)
end
end
 
context 'when calling by allowed key' do
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment