Refactor override of impressionist_count for mongoid compatibility to fit the same style as the default AR version

This commit is contained in:
jd pagano 2013-10-10 19:31:57 -04:00
parent 2b0fc9e572
commit b8719206ff
1 changed files with 11 additions and 18 deletions

View File

@ -4,28 +4,21 @@ module Impressionist
# extends AS::Concern # extends AS::Concern
include Impressionist::IsImpressionable include Impressionist::IsImpressionable
## TODO: Make it readable # Overides impressionist_count in order to provide mongoid compability
# Overides impressionist_count in order to provied
# mongoid compability
def impressionist_count(options={}) def impressionist_count(options={})
options.
reverse_merge!(
:filter=>:request_hash,
:start_date=>nil,
:end_date=>Time.now)
imps = options[:start_date].blank? ? # Uses these options as defaults unless overridden in options hash
impressions : options.reverse_merge!(:filter => :request_hash, :start_date => nil, :end_date => Time.now)
impressions.
between(created_at: options[:start_date]..options[:end_date])
filter = options[:filter] # If a start_date is provided, finds impressions between then and the end_date.
# Otherwise returns all impressions
imps = options[:start_date].blank? ? impressions :
impressions.between(created_at: options[:start_date]..options[:end_date])
filter == :all ?
imps.count : # Count all distinct impressions unless the :all filter is provided
imps.where(filter.ne => nil). distinct = options[:filter] != :all
distinct(filter).count distinct ? imps.where(filter.ne => nil).distinct(filter).count : imps.count
end end
end end