Merge pull request #37 from challengepost/optimize_impressionist_count
Optimize impressionist_count query via arel count with distinct option
This commit is contained in:
commit
95125f4627
|
@ -26,10 +26,7 @@ module Impressionist
|
||||||
def impressionist_count(options={})
|
def impressionist_count(options={})
|
||||||
options.reverse_merge!(:filter=>:request_hash, :start_date=>nil, :end_date=>Time.now)
|
options.reverse_merge!(:filter=>:request_hash, :start_date=>nil, :end_date=>Time.now)
|
||||||
imps = options[:start_date].blank? ? impressions : impressions.where("created_at>=? and created_at<=?",options[:start_date],options[:end_date])
|
imps = options[:start_date].blank? ? impressions : impressions.where("created_at>=? and created_at<=?",options[:start_date],options[:end_date])
|
||||||
if options[:filter]!=:all
|
options[:filter] == :all ? imps.count : imps.count(options[:filter], :distinct => true)
|
||||||
imps = imps.select(options[:filter]).group(options[:filter])
|
|
||||||
end
|
|
||||||
imps.all.size
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_counter_cache
|
def update_counter_cache
|
||||||
|
|
|
@ -21,7 +21,7 @@ describe Impression do
|
||||||
describe "#update_counter_cache" do
|
describe "#update_counter_cache" do
|
||||||
it "should update the counter cache column to reflect the correct number of impressions" do
|
it "should update the counter cache column to reflect the correct number of impressions" do
|
||||||
lambda {
|
lambda {
|
||||||
Impression.create(:impressionable_type => @widget.class.name, :impressionable_id => @widget.id)
|
@widget.impressions.create(:request_hash => 'abcd1234')
|
||||||
@widget.reload
|
@widget.reload
|
||||||
}.should change(@widget, :impressions_count).from(0).to(1)
|
}.should change(@widget, :impressions_count).from(0).to(1)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue