store supervoter scores in post vote score

This commit is contained in:
r888888888
2016-02-24 11:35:26 -08:00
parent 2deb3cc3b4
commit d27807c23a
2 changed files with 31 additions and 30 deletions

View File

@@ -956,25 +956,10 @@ class Post < ActiveRecord::Base
!PostVote.exists?(:user_id => user.id, :post_id => id)
end
def vote_magnitude
if CurrentUser.is_super_voter?
SuperVoter::MAGNITUDE
else
1
end
end
def vote!(score)
if can_be_voted_by?(CurrentUser.user)
if score == "up"
Post.where(:id => id).update_all("score = score + #{vote_magnitude}, up_score = up_score + #{vote_magnitude}")
self.score += vote_magnitude
elsif score == "down"
Post.where(:id => id).update_all("score = score - #{vote_magnitude}, down_score = down_score - #{vote_magnitude}")
self.score -= vote_magnitude
end
votes.create(:score => score)
vote = PostVote.create(:post_id => id, :score => score)
self.score += vote.score
else
raise PostVote::Error.new("You have already voted for this post")
end
@@ -984,17 +969,14 @@ class Post < ActiveRecord::Base
if can_be_voted_by?(CurrentUser.user)
raise PostVote::Error.new("You have not voted for this post")
else
vote = votes.where("user_id = ?", CurrentUser.user.id).first
if vote.score == 1
Post.where(:id => id).update_all("score = score - #{vote_magnitude}, up_score = up_score - #{vote_magnitude}")
self.score -= vote_magnitude
else
Post.where(:id => id).update_all("score = score + #{vote_magnitude}, down_score = down_score + #{vote_magnitude}")
self.score += vote_magnitude
end
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
end
end
end