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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user