Merge pull request #36 from challengepost/namespace_impressionable_methods

Namespace impressionable methods
This commit is contained in:
Erik Michaels-Ober 2012-03-21 12:48:51 -07:00
commit 63ea4d5663
5 changed files with 30 additions and 12 deletions

View File

@ -3,19 +3,24 @@ module Impressionist
extend ActiveSupport::Concern extend ActiveSupport::Concern
module ClassMethods module ClassMethods
attr_accessor :cache_options attr_accessor :impressionist_cache_options
@cache_options = nil @impressionist_cache_options = nil
def counter_cache_options def impressionist_counter_cache_options
if @cache_options if @impressionist_cache_options
options = { :column_name => :impressions_count, :unique => false } options = { :column_name => :impressions_count, :unique => false }
options.merge!(@cache_options) if @cache_options.is_a?(Hash) options.merge!(@impressionist_cache_options) if @impressionist_cache_options.is_a?(Hash)
options options
end end
end end
def impressionist_counter_caching?
impressionist_counter_cache_options.present?
end
def counter_caching? def counter_caching?
counter_cache_options.present? ::ActiveSupport::Deprecation.warn("#counter_caching? is deprecated; please use #impressionist_counter_caching? instead")
impressionist_counter_caching?
end end
end end
@ -29,8 +34,8 @@ module Impressionist
options[:filter] == :all ? imps.count : imps.count(options[:filter], :distinct => true) options[:filter] == :all ? imps.count : imps.count(options[:filter], :distinct => true)
end end
def update_counter_cache def update_impressionist_counter_cache
cache_options = self.class.counter_cache_options cache_options = self.class.impressionist_counter_cache_options
column_name = cache_options[:column_name].to_sym column_name = cache_options[:column_name].to_sym
count = cache_options[:unique] ? impressionist_count(:filter => :ip_address) : impressionist_count count = cache_options[:unique] ? impressionist_count(:filter => :ip_address) : impressionist_count
update_attribute(column_name, count) update_attribute(column_name, count)

View File

@ -10,9 +10,9 @@ class Impression < ActiveRecord::Base
def update_impressions_counter_cache def update_impressions_counter_cache
impressionable_class = self.impressionable_type.constantize impressionable_class = self.impressionable_type.constantize
if impressionable_class.counter_cache_options if impressionable_class.impressionist_counter_cache_options
resouce = impressionable_class.find(self.impressionable_id) resouce = impressionable_class.find(self.impressionable_id)
resouce.try(:update_counter_cache) resouce.try(:update_impressionist_counter_cache)
end end
end end
end end

View File

@ -5,7 +5,7 @@ module Impressionist
module ClassMethods module ClassMethods
def is_impressionable(options={}) def is_impressionable(options={})
has_many :impressions, :as => :impressionable, :dependent => :destroy has_many :impressions, :as => :impressionable, :dependent => :destroy
@cache_options = options[:counter_cache] @impressionist_cache_options = options[:counter_cache]
end end
end end
end end

View File

@ -8,17 +8,30 @@ describe Impression do
Impression.destroy_all Impression.destroy_all
end end
describe "self#impressionist_counter_caching?" do
it "should know when counter caching is enabled" do
Widget.should be_impressionist_counter_caching
end
it "should know when counter caching is disabled" do
Article.should_not be_impressionist_counter_caching
end
end
describe "self#counter_caching?" do describe "self#counter_caching?" do
it "should know when counter caching is enabled" do it "should know when counter caching is enabled" do
ActiveSupport::Deprecation.should_receive(:warn)
Widget.should be_counter_caching Widget.should be_counter_caching
end end
it "should know when counter caching is disabled" do it "should know when counter caching is disabled" do
ActiveSupport::Deprecation.should_receive(:warn)
Article.should_not be_counter_caching Article.should_not be_counter_caching
end end
end end
describe "#update_counter_cache" do describe "#update_impressionist_counter_cache" do
it "should update the counter cache column to reflect the correct number of impressions" do it "should update the counter cache column to reflect the correct number of impressions" do
lambda { lambda {
@widget.impressions.create(:request_hash => 'abcd1234') @widget.impressions.create(:request_hash => 'abcd1234')