Added support for counter cache column.
This commit is contained in:
parent
e291457b47
commit
9d0b22515b
23
README.md
23
README.md
|
@ -5,15 +5,9 @@ impressionist
|
||||||
|
|
||||||
A lightweight plugin that logs impressions per action or manually per model
|
A lightweight plugin that logs impressions per action or manually per model
|
||||||
|
|
||||||
|
I would not call this a stable plugin yet, although I have been running it in prod with no problems. Use at your own risk ;-)
|
||||||
------------------------------------------------------------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
NOTE: If you are upgrading from a version prior to 0.4.0, you will need to run this migration after the upgrade:
|
|
||||||
|
|
||||||
https://github.com/charlotte-ruby/impressionist/blob/master/upgrade_migrations/version_0_4_0.rb
|
|
||||||
|
|
||||||
If you don't run this migration you will receive this error: Unknown attribute : referrer
|
|
||||||
|
|
||||||
|
|
||||||
What does this thing do?
|
What does this thing do?
|
||||||
------------------------
|
------------------------
|
||||||
Logs an impression... and I use that term loosely. It can log page impressions (technically action impressions), but it is not limited to that.
|
Logs an impression... and I use that term loosely. It can log page impressions (technically action impressions), but it is not limited to that.
|
||||||
|
@ -28,7 +22,7 @@ http://www.user-agents.org/allagents.xml
|
||||||
|
|
||||||
Which versions of Rails and Ruby is this compatible with?
|
Which versions of Rails and Ruby is this compatible with?
|
||||||
---------------------------------------------------------
|
---------------------------------------------------------
|
||||||
Rails 3.0.x and Ruby 1.9.2 (also tested on REE 1.8.7) - Sorry, but you need to upgrade if you are using Rails 2. You know you want to anyways.. all the cool kids are doing it ;-)
|
Rails 3.0.4 and Ruby 1.9.2 (also tested on REE 1.8.7) - Sorry, but you need to upgrade if you are using Rails 2. You know you want to anyways.. all the cool kids are doing it ;-)
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
@ -112,6 +106,19 @@ Usage
|
||||||
|
|
||||||
Logging impressions for authenticated users happens automatically. If you have a current_user helper or use @current_user in your before_filter to set your authenticated user, current_user.id will be written to the user_id field in the impressions table.
|
Logging impressions for authenticated users happens automatically. If you have a current_user helper or use @current_user in your before_filter to set your authenticated user, current_user.id will be written to the user_id field in the impressions table.
|
||||||
|
|
||||||
|
Adding a counter cache
|
||||||
|
----------------------
|
||||||
|
Impressionist makes it easy to add a `counter_cache` column to your model. The most basic configuration looks like:
|
||||||
|
|
||||||
|
is_impressionable :counter_cache => true
|
||||||
|
|
||||||
|
This will automatically increment the `impressions_count` column in the included model. Note: You'll need to add that column to your model. If you'd like specific a different column name, you can:
|
||||||
|
|
||||||
|
is_impressionable :counter_cache => { :column_name => :my_column }
|
||||||
|
|
||||||
|
If you'd like to include only unique impressions in your count:
|
||||||
|
|
||||||
|
is_impressionable :counter_cache => { :column_name => :my_column, :unique => true }
|
||||||
|
|
||||||
Development Roadmap
|
Development Roadmap
|
||||||
-------------------
|
-------------------
|
||||||
|
|
|
@ -1,3 +1,12 @@
|
||||||
class Impression < ActiveRecord::Base
|
class Impression < ActiveRecord::Base
|
||||||
belongs_to :impressionable, :polymorphic=>true
|
belongs_to :impressionable, :polymorphic=>true
|
||||||
|
|
||||||
|
after_save :update_impressions_counter_cache
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def update_impressions_counter_cache
|
||||||
|
resouce = self.impressionable_type.constantize.find(self.impressionable_id)
|
||||||
|
resouce.update_counter_cache if resouce.try(:cache_impression_count?)
|
||||||
|
end
|
||||||
end
|
end
|
|
@ -1,7 +1,8 @@
|
||||||
module Impressionist
|
module Impressionist
|
||||||
module Impressionable
|
module Impressionable
|
||||||
def is_impressionable
|
def is_impressionable(options={})
|
||||||
has_many :impressions, :as=>:impressionable
|
has_many :impressions, :as=>:impressionable
|
||||||
|
@counter_cache_options = options[:counter_cache] ? options[:counter_cache] : nil
|
||||||
include InstanceMethods
|
include InstanceMethods
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -19,6 +20,16 @@ module Impressionist
|
||||||
imps.all.size
|
imps.all.size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cache_impression_count?
|
||||||
|
! @counter_cache_options.nil?
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_counter_cache
|
||||||
|
column_name = @counter_cache_options[:column_name] || :impressions_count
|
||||||
|
impression_count = @counter_cache_options[:unique] ? impressionist_count(filter: :ip_address) : impressionist_count
|
||||||
|
self.class.update_attribute(column_name.to_sym, impression_count)
|
||||||
|
end
|
||||||
|
|
||||||
# OLD METHODS - DEPRECATE IN V0.5
|
# OLD METHODS - DEPRECATE IN V0.5
|
||||||
def impression_count(start_date=nil,end_date=Time.now)
|
def impression_count(start_date=nil,end_date=Time.now)
|
||||||
impressionist_count({:start_date=>start_date, :end_date=>end_date, :filter=>:all})
|
impressionist_count({:start_date=>start_date, :end_date=>end_date, :filter=>:all})
|
||||||
|
|
Loading…
Reference in New Issue