Merge pull request #2937 from evazion/fix-move-fav-upvotes

Upvote correctly when moving favorites (partial fix for #2936)
This commit is contained in:
Albert Yi
2017-03-27 15:24:13 -07:00
committed by GitHub
5 changed files with 111 additions and 61 deletions

View File

@@ -943,7 +943,7 @@ class Post < ActiveRecord::Base
def add_favorite!(user)
Favorite.add(self, user)
vote!("up") if CurrentUser.is_gold?
vote!("up", user) if user.is_voter?
rescue PostVote::Error
end
@@ -953,7 +953,7 @@ class Post < ActiveRecord::Base
def remove_favorite!(user)
Favorite.remove(self, user)
unvote! if CurrentUser.is_gold?
unvote!(user) if user.is_voter?
rescue PostVote::Error
end
@@ -1063,27 +1063,25 @@ class Post < ActiveRecord::Base
!PostVote.exists?(:user_id => user.id, :post_id => id)
end
def vote!(score)
unless CurrentUser.is_voter?
def vote!(vote, voter = CurrentUser.user)
unless voter.is_voter?
raise PostVote::Error.new("You do not have permission to vote")
end
unless can_be_voted_by?(CurrentUser.user)
unless can_be_voted_by?(voter)
raise PostVote::Error.new("You have already voted for this post")
end
PostVote.create!(:post_id => id, :score => score)
votes.create!(user: voter, vote: vote)
reload # PostVote.create modifies our score. Reload to get the new score.
end
def unvote!
if can_be_voted_by?(CurrentUser.user)
def unvote!(voter = CurrentUser.user)
if can_be_voted_by?(voter)
raise PostVote::Error.new("You have not voted for this post")
else
vote = PostVote.where("post_id = ? and user_id = ?", id, CurrentUser.user.id).first
vote.destroy
self.reload
votes.where(user: voter).destroy_all
reload
end
end
end