Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ if you want the counter to persist to database once every 100 views.

if you need to run callbacks for whatever reason after you update the counter just:

class Foo < ActiveRecord::Base
include VisitCounter
cached_counter :counter_name
self.persist_with_callbacks = true
end
class Foo < ActiveRecord::Base
include VisitCounter
cached_counter :counter_name
self.update_callback_method = :update_something
end

## Contributing

Expand Down
10 changes: 4 additions & 6 deletions lib/visit-counter/visit_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.included(base)
base.class_eval do
class << self
#defining class instance attributes
attr_accessor :visit_counter_threshold, :visit_counter_threshold_method, :persist_with_callbacks
attr_accessor :visit_counter_threshold, :visit_counter_threshold_method, :update_callback_method
end
end
base.send(:include, InstanceMethods)
Expand Down Expand Up @@ -73,11 +73,9 @@ def passed_limit?(object, staged_count, diff, name)

def persist(object, staged_count, diff, name)
VisitCounter::Store.engine.with_lock(object) do
if object.class.persist_with_callbacks
object.send("#{name}=", staged_count + diff )
object.save
else
object.update_attribute(name, staged_count + diff)
object.update_attribute(name, staged_count + diff)
if object.class.update_callback_method
object.send(object.class.update_callback_method)
end
object.nullify_counter_cache(name, diff)
end
Expand Down
10 changes: 5 additions & 5 deletions spec/lib/visit_counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
class DummyObject
include VisitCounter

attr_accessor :counter, :persist_with_callbacks
attr_accessor :counter, :update_callback_method

def update_attribute(attribute, value)
self.send("#{attribute}=", value)
end

def save
def update_something
nil
end

Expand Down Expand Up @@ -163,11 +163,11 @@ def read_attribute(name)
end

describe "persist with callbacks" do
it "should use save method" do
it "should use update_something method" do
@d = DummyObject.new
@d.class.persist_with_callbacks = true
@d.class.update_callback_method = :update_something
@d.stub!(:id).and_return(1)
@d.should_receive(:save)
@d.should_receive(:update_something)
@d.incr_counter :counter
end
end
Expand Down