Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Namespace impressionable methods #36

Merged
merged 4 commits into from

2 participants

@rossta

Thanks for impressionist, we're looking forward to using it here at ChallengePost.

I took at stab at renaming a few methods in the Impressionable module to improve clarity and reduce the likelihood of naming conflicts.

@sferik sferik merged commit 63ea4d5 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
23 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
@@ -32,8 +37,8 @@ def impressionist_count(options={})
imps.all.size
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)
@@ -56,4 +61,4 @@ def unique_impression_count_session(start_date=nil,end_date=Time.now)
impressionist_count({:start_date=>start_date, :end_date=>end_date, :filter=> :session_hash})
end
end
-end
+end
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  test_app/spec/intializers/initializers_spec.rb → test_app/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 {
Impression.create(:impressionable_type => @widget.class.name, :impressionable_id => @widget.id)
Something went wrong with that request. Please try again.