FIX #96
This commit is contained in:
parent
b05aa79ad3
commit
d737ee37af
|
@ -1,11 +1,15 @@
|
||||||
|
##
|
||||||
|
# see active_record/impression.rb
|
||||||
|
# same doc applies to here
|
||||||
class Impression
|
class Impression
|
||||||
include Mongoid::Document
|
include Mongoid::Document
|
||||||
include Mongoid::Timestamps
|
include Mongoid::Timestamps
|
||||||
|
|
||||||
include Impressionist::SetUpAssociation
|
include Impressionist::CounterCache
|
||||||
|
Impressionist::SetupAssociation.new(self).set
|
||||||
belongs_to :impressionable, polymorphic: true
|
|
||||||
|
|
||||||
|
field :impressionable_id
|
||||||
|
field :impressionable_type
|
||||||
field :user_id
|
field :user_id
|
||||||
field :controller_name
|
field :controller_name
|
||||||
field :action_name
|
field :action_name
|
||||||
|
@ -16,15 +20,6 @@ class Impression
|
||||||
field :message
|
field :message
|
||||||
field :referrer
|
field :referrer
|
||||||
|
|
||||||
set_callback(:create, :after) do |doc|
|
after_save :impressionable_counter_cache_updatable?
|
||||||
unless impressionable_id.nil?
|
|
||||||
impressionable_class = doc.impressionable_type.constantize
|
|
||||||
|
|
||||||
if impressionable_class.impressionist_counter_cache_options
|
|
||||||
resource = impressionable_class.find(doc.impressionable_id)
|
|
||||||
resource.try(:update_impressionist_counter_cache)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,37 +7,25 @@ module Impressionist
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
|
||||||
def is_impressionable(options={})
|
def is_impressionable(options={})
|
||||||
has_many :impressions, as: :impressionable, dependent: :destroy
|
define_association
|
||||||
@impressionist_cache_options = options[:counter_cache]
|
@impressionist_cache_options = options
|
||||||
if !@impressionist_cache_options.nil?
|
|
||||||
opts = impressionist_counter_cache_options
|
true
|
||||||
field opts[:column_name], type: Integer
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def impressionist_counter_cache_options
|
private
|
||||||
if @impressionist_cache_options
|
def define_association
|
||||||
options = { :column_name => :impressions_count, :unique => false }
|
has_many(:impressions,
|
||||||
options.merge!(@impressionist_cache_options) if @impressionist_cache_options.is_a?(Hash)
|
:as => :impressionable,
|
||||||
options
|
:dependent => :destroy)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Overides active_record impressionist_count
|
||||||
def impressionist_count(options={})
|
def impressionist_count(options={})
|
||||||
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)
|
||||||
imps = options[:start_date].blank? ? impressions : impressions.between(created_at: options[:start_date]..options[:end_date])
|
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
|
filter == :all ? imps.count : imps.where(filter.ne => nil).distinct(filter).count
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue