From 380f49cf62cf07e2b7c174d628d8cc299a53c24e Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Mon, 19 Mar 2012 14:31:05 -0400 Subject: [PATCH 1/4] namespace impressionable model module class methods with 'impressionist_' --- app/models/impressionist/impressionable.rb | 18 +++++++++--------- .../models/active_record/impression.rb | 2 +- .../impressionist/impressionable.rb | 2 +- .../initializers_spec.rb | 0 test_app/spec/models/counter_caching_spec.rb | 6 +++--- 5 files changed, 14 insertions(+), 14 deletions(-) rename test_app/spec/{intializers => initializers}/initializers_spec.rb (100%) diff --git a/app/models/impressionist/impressionable.rb b/app/models/impressionist/impressionable.rb index e09addd..57d5d86 100644 --- a/app/models/impressionist/impressionable.rb +++ b/app/models/impressionist/impressionable.rb @@ -3,19 +3,19 @@ module Impressionist 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 counter_caching? - counter_cache_options.present? + def impressionist_counter_caching? + impressionist_counter_cache_options.present? end end @@ -33,7 +33,7 @@ module Impressionist end 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 count = cache_options[:unique] ? impressionist_count(:filter => :ip_address) : impressionist_count update_attribute(column_name, count) @@ -56,4 +56,4 @@ module Impressionist impressionist_count({:start_date=>start_date, :end_date=>end_date, :filter=> :session_hash}) end end -end +end \ No newline at end of file diff --git a/lib/impressionist/models/active_record/impression.rb b/lib/impressionist/models/active_record/impression.rb index 4d13373..707151d 100644 --- a/lib/impressionist/models/active_record/impression.rb +++ b/lib/impressionist/models/active_record/impression.rb @@ -10,7 +10,7 @@ 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) end diff --git a/lib/impressionist/models/active_record/impressionist/impressionable.rb b/lib/impressionist/models/active_record/impressionist/impressionable.rb index d9339fb..6100b88 100644 --- a/lib/impressionist/models/active_record/impressionist/impressionable.rb +++ b/lib/impressionist/models/active_record/impressionist/impressionable.rb @@ -5,7 +5,7 @@ module Impressionist 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 diff --git a/test_app/spec/intializers/initializers_spec.rb b/test_app/spec/initializers/initializers_spec.rb similarity index 100% rename from test_app/spec/intializers/initializers_spec.rb rename to test_app/spec/initializers/initializers_spec.rb diff --git a/test_app/spec/models/counter_caching_spec.rb b/test_app/spec/models/counter_caching_spec.rb index 88cbd73..609b88e 100644 --- a/test_app/spec/models/counter_caching_spec.rb +++ b/test_app/spec/models/counter_caching_spec.rb @@ -8,13 +8,13 @@ describe Impression do Impression.destroy_all end - describe "self#counter_caching?" do + describe "self#impressionist_counter_caching?" do it "should know when counter caching is enabled" do - Widget.should be_counter_caching + Widget.should be_impressionist_counter_caching end it "should know when counter caching is disabled" do - Article.should_not be_counter_caching + Article.should_not be_impressionist_counter_caching end end From 4c2c09ca69ca779ff630f187e8e12ec3c8a96a1e Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Mon, 19 Mar 2012 14:33:19 -0400 Subject: [PATCH 2/4] Rename instance method update_counter_cache => update_impressionist_counter_cache --- app/models/impressionist/impressionable.rb | 2 +- lib/impressionist/models/active_record/impression.rb | 2 +- test_app/spec/models/counter_caching_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/impressionist/impressionable.rb b/app/models/impressionist/impressionable.rb index 57d5d86..6143538 100644 --- a/app/models/impressionist/impressionable.rb +++ b/app/models/impressionist/impressionable.rb @@ -32,7 +32,7 @@ module Impressionist imps.all.size end - def update_counter_cache + 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 diff --git a/lib/impressionist/models/active_record/impression.rb b/lib/impressionist/models/active_record/impression.rb index 707151d..94c2710 100644 --- a/lib/impressionist/models/active_record/impression.rb +++ b/lib/impressionist/models/active_record/impression.rb @@ -12,7 +12,7 @@ class Impression < ActiveRecord::Base 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 diff --git a/test_app/spec/models/counter_caching_spec.rb b/test_app/spec/models/counter_caching_spec.rb index 609b88e..ac7b615 100644 --- a/test_app/spec/models/counter_caching_spec.rb +++ b/test_app/spec/models/counter_caching_spec.rb @@ -18,7 +18,7 @@ describe Impression do 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 { Impression.create(:impressionable_type => @widget.class.name, :impressionable_id => @widget.id) From f59f897d25d2cf96b29d28360789ad3ce9f0fbc1 Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Mon, 19 Mar 2012 15:56:42 -0400 Subject: [PATCH 3/4] Deprecation warning for counter_caching? --- app/models/impressionist/impressionable.rb | 5 +++++ test_app/spec/models/counter_caching_spec.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/app/models/impressionist/impressionable.rb b/app/models/impressionist/impressionable.rb index 6143538..5e7508f 100644 --- a/app/models/impressionist/impressionable.rb +++ b/app/models/impressionist/impressionable.rb @@ -17,6 +17,11 @@ module Impressionist 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? diff --git a/test_app/spec/models/counter_caching_spec.rb b/test_app/spec/models/counter_caching_spec.rb index ac7b615..486a90b 100644 --- a/test_app/spec/models/counter_caching_spec.rb +++ b/test_app/spec/models/counter_caching_spec.rb @@ -18,6 +18,19 @@ describe Impression do 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 enabled" do + ActiveSupport::Deprecation.should_receive(:warn) + Article.should_not be_counter_caching + end + + end + describe "#update_impressionist_counter_cache" do it "should update the counter cache column to reflect the correct number of impressions" do lambda { From 51144d4c8607fc3c93528a033dcc9e19934ce99e Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Mon, 19 Mar 2012 16:04:43 -0400 Subject: [PATCH 4/4] Typo in test --- test_app/spec/models/counter_caching_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_app/spec/models/counter_caching_spec.rb b/test_app/spec/models/counter_caching_spec.rb index 486a90b..d1f59a1 100644 --- a/test_app/spec/models/counter_caching_spec.rb +++ b/test_app/spec/models/counter_caching_spec.rb @@ -24,7 +24,7 @@ describe Impression do Widget.should be_counter_caching end - it "should know when counter caching is enabled" do + it "should know when counter caching is disabled" do ActiveSupport::Deprecation.should_receive(:warn) Article.should_not be_counter_caching end