Skip to content
Browse files

Merge pull request #36 from challengepost/namespace_impressionable_me…

…thods

Namespace impressionable methods
  • Loading branch information...
2 parents 95125f4 + 51144d4 commit 63ea4d5663e966fa8093e69b35090aa610978968 @sferik sferik committed
View
21 app/models/impressionist/impressionable.rb
@@ -3,19 +3,24 @@ module Impressionable
extend ActiveSupport::Concern
module ClassMethods
- attr_accessor :cache_options
- @cache_options = nil
+ attr_accessor :impressionist_cache_options
+ @impressionist_cache_options = nil
- def counter_cache_options
- if @cache_options
+ def impressionist_counter_cache_options
+ if @impressionist_cache_options
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
end
end
+ def impressionist_counter_caching?
+ impressionist_counter_cache_options.present?
+ end
+
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
@@ -29,8 +34,8 @@ def impressionist_count(options={})
options[:filter] == :all ? imps.count : imps.count(options[:filter], :distinct => true)
end
- def update_counter_cache
- cache_options = self.class.counter_cache_options
+ 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
update_attribute(column_name, count)
View
4 lib/impressionist/models/active_record/impression.rb
@@ -10,9 +10,9 @@ class Impression < ActiveRecord::Base
def update_impressions_counter_cache
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.try(:update_counter_cache)
+ resouce.try(:update_impressionist_counter_cache)
end
end
end
View
2 lib/impressionist/models/active_record/impressionist/impressionable.rb
@@ -5,7 +5,7 @@ module Impressionable
module ClassMethods
def is_impressionable(options={})
has_many :impressions, :as => :impressionable, :dependent => :destroy
- @cache_options = options[:counter_cache]
+ @impressionist_cache_options = options[:counter_cache]
end
end
end
View
0 ...app/spec/intializers/initializers_spec.rb → ...pp/spec/initializers/initializers_spec.rb
File renamed without changes.
View
15 test_app/spec/models/counter_caching_spec.rb
@@ -8,17 +8,30 @@
Impression.destroy_all
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
it "should know when counter caching is enabled" do
+ ActiveSupport::Deprecation.should_receive(:warn)
Widget.should be_counter_caching
end
it "should know when counter caching is disabled" do
+ ActiveSupport::Deprecation.should_receive(:warn)
Article.should_not be_counter_caching
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
lambda {
@widget.impressions.create(:request_hash => 'abcd1234')

0 comments on commit 63ea4d5

Please sign in to comment.
Something went wrong with that request. Please try again.