post_vote.rb: determine vote magnitude from voter, not CurrentUser.

Bug: when moving favorites, the parent is given an upvote for each gold+
favoriter, but the magnitude of these upvotes was based on the
CurrentUser, rather than the upvoter. This meant that upvotes for
supervoter favorites weren't given correctly.

To fix this, `magnitude` is changed to use the voting `user` instead of
CurrentUser.

New problem: when setting the score, `score=` calls `magnitude`, but
`user` isn't initialized yet. So we also refactor so that

    1. `initialize_attributes` initializes `user` before setting `score`
    2. the vote direction is given by `vote`, so it's separate from `score`
    3. updating the score on the post happens in a callback instead of
       directly in `score=`.
This commit is contained in:
evazion
2017-03-24 14:55:37 -05:00
parent 07707b257a
commit 3dc854c0c8
3 changed files with 25 additions and 17 deletions

View File

@@ -14,17 +14,17 @@ class PostVoteTest < ActiveSupport::TestCase
context "Voting for a post" do
should "interpret up as +1 score" do
vote = PostVote.create(:post_id => @post.id, :score => "up")
vote = PostVote.create(:post_id => @post.id, :vote => "up")
assert_equal(1, vote.score)
end
should "interpret down as -1 score" do
vote = PostVote.create(:post_id => @post.id, :score => "down")
vote = PostVote.create(:post_id => @post.id, :vote => "down")
assert_equal(-1, vote.score)
end
should "not accept any other scores" do
vote = PostVote.create(:post_id => @post.id, :score => "xxx")
vote = PostVote.create(:post_id => @post.id, :vote => "xxx")
assert(vote.errors.any?)
end
end