Merge pull request #2710 from evazion/fix/2709

Post#unvote!: Return correct score (fixes #2709).
This commit is contained in:
Albert Yi
2016-10-10 12:20:51 -07:00
committed by GitHub
2 changed files with 17 additions and 6 deletions

View File

@@ -1033,11 +1033,7 @@ class Post < ActiveRecord::Base
vote = PostVote.where("post_id = ? and user_id = ?", id, CurrentUser.user.id).first
vote.destroy
if vote.score > 0
self.score -= vote.score
else
self.score += vote.score
end
self.reload
end
end
end

View File

@@ -1582,12 +1582,27 @@ class PostTest < ActiveSupport::TestCase
should "allow undoing of votes" do
user = FactoryGirl.create(:user)
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
post.vote!("up")
assert_equal(1, post.score)
post.unvote!
post.reload
assert_equal(0, post.score)
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