namespace impressionable model module class methods with 'impressionist_'

This commit is contained in:
Ross Kaffenberger 2012-03-19 14:31:05 -04:00
parent 594889a3db
commit 380f49cf62
5 changed files with 14 additions and 14 deletions

View File

@ -3,19 +3,19 @@ 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 counter_caching? def impressionist_counter_caching?
counter_cache_options.present? impressionist_counter_cache_options.present?
end end
end end
@ -33,7 +33,7 @@ module Impressionist
end end
def update_counter_cache def update_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,7 +10,7 @@ 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_counter_cache)
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,13 +8,13 @@ describe Impression do
Impression.destroy_all Impression.destroy_all
end end
describe "self#counter_caching?" do describe "self#impressionist_counter_caching?" do
it "should know when counter caching is enabled" do it "should know when counter caching is enabled" do
Widget.should be_counter_caching Widget.should be_impressionist_counter_caching
end end
it "should know when counter caching is disabled" do it "should know when counter caching is disabled" do
Article.should_not be_counter_caching Article.should_not be_impressionist_counter_caching
end end
end end