diff --git a/app/models/impressionist/impressionable.rb b/app/models/impressionist/impressionable.rb index 9385646..4b23b4b 100644 --- a/app/models/impressionist/impressionable.rb +++ b/app/models/impressionist/impressionable.rb @@ -32,10 +32,10 @@ module Impressionist options[:filter] == :all ? imps.count : imps.count(options[:filter], :distinct => true) end - def update_impressionist_counter_cache + def update_impressionist_counter_cache slave = Impressionist::UpdateCounters.new(self) slave.update - end + end def impressionable? true diff --git a/lib/impressionist/models/mongoid/impression.rb b/lib/impressionist/models/mongoid/impression.rb index 55330c4..d3b1c15 100644 --- a/lib/impressionist/models/mongoid/impression.rb +++ b/lib/impressionist/models/mongoid/impression.rb @@ -1,11 +1,15 @@ +## +# see active_record/impression.rb +# same doc applies to here class Impression include Mongoid::Document include Mongoid::Timestamps - include Impressionist::SetUpAssociation - - belongs_to :impressionable, polymorphic: true + include Impressionist::CounterCache + Impressionist::SetupAssociation.new(self).set + field :impressionable_id + field :impressionable_type field :user_id field :controller_name field :action_name @@ -16,15 +20,6 @@ class Impression field :message field :referrer - set_callback(:create, :after) do |doc| - unless impressionable_id.nil? - impressionable_class = doc.impressionable_type.constantize + after_save :impressionable_counter_cache_updatable? - if impressionable_class.impressionist_counter_cache_options - resource = impressionable_class.find(doc.impressionable_id) - resource.try(:update_impressionist_counter_cache) - end - end - end - end diff --git a/lib/impressionist/models/mongoid/impressionist/impressionable.rb b/lib/impressionist/models/mongoid/impressionist/impressionable.rb index d97f986..4802e0a 100644 --- a/lib/impressionist/models/mongoid/impressionist/impressionable.rb +++ b/lib/impressionist/models/mongoid/impressionist/impressionable.rb @@ -7,37 +7,25 @@ module Impressionist extend ActiveSupport::Concern module ClassMethods + def is_impressionable(options={}) - has_many :impressions, as: :impressionable, dependent: :destroy - @impressionist_cache_options = options[:counter_cache] - if !@impressionist_cache_options.nil? - opts = impressionist_counter_cache_options - field opts[:column_name], type: Integer - end + define_association + @impressionist_cache_options = options + + true end - def impressionist_counter_cache_options - if @impressionist_cache_options - options = { :column_name => :impressions_count, :unique => false } - options.merge!(@impressionist_cache_options) if @impressionist_cache_options.is_a?(Hash) - options - end - end + private + def define_association + has_many(:impressions, + :as => :impressionable, + :dependent => :destroy) + end - def impressionist_counter_caching? - impressionist_counter_cache_options.present? - end - - def counter_caching? - ::ActiveSupport::Deprecation.warn("#counter_caching? is deprecated; please use #impressionist_counter_caching? instead") - impressionist_counter_caching? - end - end - - def impressionable? - true end + ## + # Overides active_record impressionist_count 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]) @@ -45,13 +33,6 @@ module Impressionist filter == :all ? imps.count : imps.where(filter.ne => nil).distinct(filter).count end - def update_impressionist_counter_cache - cache_options = self.class.impressionist_counter_cache_options - column_name = cache_options[:column_name].to_sym - count = cache_options[:unique] ? impressionist_count(:filter => :ip_address) : impressionist_count - old_count = send(column_name) || 0 - self.inc(column_name, (count - old_count)) - end - end + end