Merge pull request #120 from joofsh/Refactor_impressionable_impressionist_count_method

Refactor impressionist_count method in impressionable.rb
This commit is contained in:
Antonio C Nalesso Moreira 2013-10-10 15:00:35 -07:00
commit cbece83ac9
1 changed files with 10 additions and 6 deletions

View File

@ -24,15 +24,19 @@ module Impressionist
end # end of ClassMethods
# ------------------------------------------
# TODO: CLEAN UP, make it HUMAN readable
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)
# 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])
# Count all distinct impressions unless the :all filter is provided.
distinct = options[:filter] != :all
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
options[:filter] == :all ? imps.count : imps.count(options[:filter], :distinct => true)
distinct ? imps.count(options[:filter], :distinct => true) : imps.count
end
end