Refactor impressionist_count method in impressionable.rb method to be more human readable

This commit is contained in:
jd pagano 2013-10-10 16:29:46 -04:00 committed by jonathanpagano@gmail.com
parent 13cda5c508
commit c054fe6b02
1 changed files with 10 additions and 6 deletions

View File

@ -24,15 +24,19 @@ module Impressionist
end # end of ClassMethods end # end of ClassMethods
# ------------------------------------------
# TODO: CLEAN UP, make it HUMAN readable
def impressionist_count(options={}) def impressionist_count(options={})
# Uses these options as defaults unless overridden in options hash
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)
# 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.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])
# Count all distinct impressions unless the :all filter is provided.
distinct = options[:filter] != :all
if Rails::VERSION::MAJOR == 4 if Rails::VERSION::MAJOR == 4
options[:filter] == :all ? imps.count : imps.select(options[:filter]).distinct.count distinct ? imps.select(options[:filter]).distinct.count : imps.count
else else
options[:filter] == :all ? imps.count : imps.count(options[:filter], :distinct => true) distinct ? imps.count(options[:filter], :distinct => true) : imps.count
end end
end end