Skip to content
This repository has been archived by the owner on Sep 18, 2020. It is now read-only.

Commit

Permalink
Only call hdel and after_unlike when like exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Christof Dorner committed Jan 30, 2012
1 parent cb9ee23 commit 8f6c2e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/likeable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ def after_like(like)

# removes a like
def remove_like_from(user)
Likeable.redis.hdel(like_key, user.id)
Likeable.redis.hdel(user.like_key(self.class.to_s.downcase), self.id)
after_unlike(user)
clear_memoized_methods(:like_count, :like_user_ids, :liked_user_ids, :liked_users)
if Likeable.redis.hexists(like_key, user.id)
Likeable.redis.hdel(like_key, user.id)
Likeable.redis.hdel(user.like_key(self.class.to_s.downcase), self.id)
after_unlike(user)
clear_memoized_methods(:like_count, :like_user_ids, :liked_user_ids, :liked_users)
end
end

def after_unlike(user)
Expand Down
9 changes: 9 additions & 0 deletions spec/likeable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ def id
it "removes a like" do
target_class = @target.class.to_s.downcase
user_like_key = "users:like:#{@user.id}:#{target_class}"
Likeable.redis.should_receive(:hexists).with("like_key", @user.id).and_return(true)
@user.should_receive(:like_key).with(target_class).and_return(user_like_key)
Likeable.redis.should_receive(:hdel).with("like_key", @user.id).once
Likeable.redis.should_receive(:hdel).with(user_like_key, @target.id)
@target.remove_like_from(@user)
end

it "doesn't call after_unlike if like didn't exist" do
CleanTestClassForLikeable.after_unlike(:foo)
@target = CleanTestClassForLikeable.new
@target.should_not_receive(:foo)
@target.remove_like_from(@user)
end
end

describe "#liked_users" do
Expand Down Expand Up @@ -130,6 +138,7 @@ def id
end
it 'is called after a like is destroyed' do
CleanTestClassForLikeable.after_unlike(:foo)
Likeable.redis.should_receive(:hexists).and_return(true)
@target.should_receive(:foo)
@target.remove_like_from(@user)
end
Expand Down

0 comments on commit 8f6c2e3

Please sign in to comment.