Skip to content

Commit

Permalink
Doing counter model to reload if has changed when asking for count
Browse files Browse the repository at this point in the history
  • Loading branch information
ritxi committed May 28, 2011
1 parent a77b7c2 commit d3642be
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
25 changes: 21 additions & 4 deletions lib/activity_counter/model/counter.rb
Expand Up @@ -19,8 +19,7 @@ def generate(*options)

def split_source(options)
@source = options.delete(:source)
p @source
{ :source_class => @source.class.to_s, :source_id => @source[:id] }.merge(options)
@source ? { :source_class => @source.class.to_s, :source_id => @source[:id] }.merge(options) : options
end

def find_reflection_name(options)
Expand Down Expand Up @@ -85,17 +84,35 @@ def validate_option(options, option)
end
end
module InstanceMethods
def counter_changed?
@counter_changed
end
def counter_changed!
@counter_changed = true
end
def counter_unchanged!
@counter_changed = false
end
def source
eval("#{source_class}.find(#{source_id})")
end
def cached_items
source.send(source_relation)
end
def increase
self.class.increment_counter('count', self[:id])
self.class.increment_counter(:count, self[:id])
counter_changed!
end
def decrease
self.class.decrement_counter('count', self[:id])
self.class.decrement_counter(:count, self[:id])
counter_changed!
end
def count
if counter_changed?
counter_unchanged!
self.reload
end
self[:count]
end
end
end
Expand Down
45 changes: 27 additions & 18 deletions test/unit/counter_test.rb
Expand Up @@ -3,43 +3,52 @@

class CounterTest < ActiveSupport::TestCase
def setup
@user = User.create
@user2 = User.create
@event = Event.create

end
def new_counter(source, counter_name, *options)
options = options.first.blank? ? {} : options.first
counter_options = {:source => source, :name => counter_name}.merge(options)
Counter.generate(counter_options)



end
def new_event_counter(user)
new_counter(user, 'total', :source_relation => 'events')
def new_event_invitation_counter(event)
new_counter(event, :pending, :source_relation => :invitees)
end
test "acts_as_counter method presence" do
assert User.public_methods.include?('acts_as_activity_counter')
assert User.private_methods.include?("add_counter_cache_callbacks")
assert Event.public_methods.include?('acts_as_activity_counter')
assert Event.private_methods.include?("add_counter_cache_callbacks")
end
test "counter uniqueness" do
assert new_event_counter(@user).save
assert !new_event_counter(@user).save
assert new_event_invitation_counter(@event).save
assert !new_event_invitation_counter(@event).save
end

test "have all attributes set" do
counter = new_counter(@user, nil, :source_relation => 'events')
assert(!counter.save)
assert_equal(counter.errors.keys, [:name])
counter.name = 'total'
assert(counter.save)
counter = new_counter(@event, nil, :source_relation => :invitees)
assert !counter.save
assert_equal counter.errors.keys, [:name]
counter.name = 'invitees'
assert counter.save
end

test 'increase/decrease counter by 1' do
counter = new_event_counter(@user)
counter = new_event_invitation_counter(@event)
counter.save
assert_equal(0, counter.count)
assert_equal 0, counter.count

counter.increase
assert_equal(1, counter.count)
assert_equal 1, counter.count

counter.increase
assert_equal 2, counter.count

counter.decrease
assert_equal(0, counter.count)
assert_equal 1, counter.count

counter.decrease
assert_equal(0, counter.count)
assert_equal 0, counter.count
end
end

0 comments on commit d3642be

Please sign in to comment.