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:
@@ -1063,7 +1063,7 @@ class Post < ActiveRecord::Base
|
||||
!PostVote.exists?(:user_id => user.id, :post_id => id)
|
||||
end
|
||||
|
||||
def vote!(score, voter = CurrentUser.user)
|
||||
def vote!(vote, voter = CurrentUser.user)
|
||||
unless voter.is_voter?
|
||||
raise PostVote::Error.new("You do not have permission to vote")
|
||||
end
|
||||
@@ -1072,7 +1072,7 @@ class Post < ActiveRecord::Base
|
||||
raise PostVote::Error.new("You have already voted for this post")
|
||||
end
|
||||
|
||||
votes.create!(user: voter, score: vote)
|
||||
votes.create!(user: voter, vote: vote)
|
||||
reload # PostVote.create modifies our score. Reload to get the new score.
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user