Merge pull request #2710 from evazion/fix/2709
Post#unvote!: Return correct score (fixes #2709).
This commit is contained in:
@@ -1033,11 +1033,7 @@ class Post < ActiveRecord::Base
|
|||||||
vote = PostVote.where("post_id = ? and user_id = ?", id, CurrentUser.user.id).first
|
vote = PostVote.where("post_id = ? and user_id = ?", id, CurrentUser.user.id).first
|
||||||
vote.destroy
|
vote.destroy
|
||||||
|
|
||||||
if vote.score > 0
|
self.reload
|
||||||
self.score -= vote.score
|
|
||||||
else
|
|
||||||
self.score += vote.score
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1582,12 +1582,27 @@ class PostTest < ActiveSupport::TestCase
|
|||||||
should "allow undoing of votes" do
|
should "allow undoing of votes" do
|
||||||
user = FactoryGirl.create(:user)
|
user = FactoryGirl.create(:user)
|
||||||
post = FactoryGirl.create(:post)
|
post = FactoryGirl.create(:post)
|
||||||
|
|
||||||
|
# We deliberately don't call post.reload until the end to verify that
|
||||||
|
# post.unvote! returns the correct score even when not forcibly reloaded.
|
||||||
CurrentUser.scoped(user, "127.0.0.1") do
|
CurrentUser.scoped(user, "127.0.0.1") do
|
||||||
post.vote!("up")
|
post.vote!("up")
|
||||||
|
assert_equal(1, post.score)
|
||||||
|
|
||||||
post.unvote!
|
post.unvote!
|
||||||
post.reload
|
|
||||||
assert_equal(0, post.score)
|
assert_equal(0, post.score)
|
||||||
|
|
||||||
assert_nothing_raised {post.vote!("down")}
|
assert_nothing_raised {post.vote!("down")}
|
||||||
|
assert_equal(-1, post.score)
|
||||||
|
|
||||||
|
post.unvote!
|
||||||
|
assert_equal(0, post.score)
|
||||||
|
|
||||||
|
assert_nothing_raised {post.vote!("up")}
|
||||||
|
assert_equal(1, post.score)
|
||||||
|
|
||||||
|
post.reload
|
||||||
|
assert_equal(1, post.score)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user