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

@@ -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

View File

@@ -2,10 +2,14 @@ class PostVote < ActiveRecord::Base
class Error < Exception ; end
belongs_to :post
before_validation :initialize_user, :on => :create
belongs_to :user
attr_accessor :vote
attr_accessible :post, :post_id, :user, :user_id, :score, :vote
after_initialize :initialize_attributes, if: :new_record?
validates_presence_of :post_id, :user_id, :score
validates_inclusion_of :score, :in => [SuperVoter::MAGNITUDE, 1, -1, -SuperVoter::MAGNITUDE]
attr_accessible :post_id, :user_id, :score
after_create :update_post_on_create
after_destroy :update_post_on_destroy
def self.prune!
@@ -24,18 +28,22 @@ class PostVote < ActiveRecord::Base
select_values_sql("select post_id from post_votes where score > 0 and user_id = ?", user_id)
end
def score=(x)
if x == "up"
Post.where(:id => post_id).update_all("score = score + #{magnitude}, up_score = up_score + #{magnitude}")
write_attribute(:score, magnitude)
elsif x == "down"
Post.where(:id => post_id).update_all("score = score - #{magnitude}, down_score = down_score - #{magnitude}")
write_attribute(:score, -magnitude)
def initialize_attributes
self.user_id ||= CurrentUser.user.id
if vote == "up"
self.score = magnitude
elsif vote == "down"
self.score = -magnitude
end
end
def initialize_user
self.user_id ||= CurrentUser.user.id
def update_post_on_create
if score > 0
Post.where(:id => post_id).update_all("score = score + #{score}, up_score = up_score + #{score}")
else
Post.where(:id => post_id).update_all("score = score + #{score}, down_score = down_score + #{score}")
end
end
def update_post_on_destroy
@@ -47,7 +55,7 @@ class PostVote < ActiveRecord::Base
end
def magnitude
if CurrentUser.is_super_voter?
if user.is_super_voter?
SuperVoter::MAGNITUDE
else
1

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