From b8719206ff881cd29e89c456b266dbaf51f290a9 Mon Sep 17 00:00:00 2001 From: jd pagano Date: Thu, 10 Oct 2013 19:31:57 -0400 Subject: [PATCH] Refactor override of impressionist_count for mongoid compatibility to fit the same style as the default AR version --- .../mongoid/impressionist/impressionable.rb | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/lib/impressionist/models/mongoid/impressionist/impressionable.rb b/lib/impressionist/models/mongoid/impressionist/impressionable.rb index 8ceb912..d770a79 100644 --- a/lib/impressionist/models/mongoid/impressionist/impressionable.rb +++ b/lib/impressionist/models/mongoid/impressionist/impressionable.rb @@ -4,28 +4,21 @@ module Impressionist # extends AS::Concern include Impressionist::IsImpressionable - ## TODO: Make it readable - - # Overides impressionist_count in order to provied - # mongoid compability + # Overides impressionist_count in order to provide mongoid compability def impressionist_count(options={}) - options. - reverse_merge!( - :filter=>:request_hash, - :start_date=>nil, - :end_date=>Time.now) - imps = options[:start_date].blank? ? - impressions : - impressions. - between(created_at: options[:start_date]..options[:end_date]) + # Uses these options as defaults unless overridden in options hash + options.reverse_merge!(:filter => :request_hash, :start_date => nil, :end_date => Time.now) - 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 : - imps.where(filter.ne => nil). - distinct(filter).count + + # Count all distinct impressions unless the :all filter is provided + distinct = options[:filter] != :all + distinct ? imps.where(filter.ne => nil).distinct(filter).count : imps.count end end