posts.rb: vote on behalf of correct user when moving favorites.
Bug: when an approver moves the favorites of a post, each favorite is removed from the child post and added to the parent post. For gold+ users, this triggers an upvote, but these upvotes were performed by the approver rather than the favoriter.
This commit is contained in:
@@ -943,7 +943,7 @@ class Post < ActiveRecord::Base
|
|||||||
|
|
||||||
def add_favorite!(user)
|
def add_favorite!(user)
|
||||||
Favorite.add(self, user)
|
Favorite.add(self, user)
|
||||||
vote!("up") if CurrentUser.is_gold?
|
vote!("up", user) if user.is_gold?
|
||||||
rescue PostVote::Error
|
rescue PostVote::Error
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -953,7 +953,7 @@ class Post < ActiveRecord::Base
|
|||||||
|
|
||||||
def remove_favorite!(user)
|
def remove_favorite!(user)
|
||||||
Favorite.remove(self, user)
|
Favorite.remove(self, user)
|
||||||
unvote! if CurrentUser.is_gold?
|
unvote!(user) if user.is_gold?
|
||||||
rescue PostVote::Error
|
rescue PostVote::Error
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1063,27 +1063,25 @@ class Post < ActiveRecord::Base
|
|||||||
!PostVote.exists?(:user_id => user.id, :post_id => id)
|
!PostVote.exists?(:user_id => user.id, :post_id => id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def vote!(score)
|
def vote!(score, voter = CurrentUser.user)
|
||||||
unless CurrentUser.is_voter?
|
unless voter.is_voter?
|
||||||
raise PostVote::Error.new("You do not have permission to vote")
|
raise PostVote::Error.new("You do not have permission to vote")
|
||||||
end
|
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")
|
raise PostVote::Error.new("You have already voted for this post")
|
||||||
end
|
end
|
||||||
|
|
||||||
PostVote.create!(:post_id => id, :score => score)
|
votes.create!(user: voter, score: vote)
|
||||||
reload # PostVote.create modifies our score. Reload to get the new score.
|
reload # PostVote.create modifies our score. Reload to get the new score.
|
||||||
end
|
end
|
||||||
|
|
||||||
def unvote!
|
def unvote!(voter = CurrentUser.user)
|
||||||
if can_be_voted_by?(CurrentUser.user)
|
if can_be_voted_by?(voter)
|
||||||
raise PostVote::Error.new("You have not voted for this post")
|
raise PostVote::Error.new("You have not voted for this post")
|
||||||
else
|
else
|
||||||
vote = PostVote.where("post_id = ? and user_id = ?", id, CurrentUser.user.id).first
|
votes.where(user: voter).destroy_all
|
||||||
vote.destroy
|
reload
|
||||||
|
|
||||||
self.reload
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class PostVote < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize_user
|
def initialize_user
|
||||||
self.user_id = CurrentUser.user.id
|
self.user_id ||= CurrentUser.user.id
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_post_on_destroy
|
def update_post_on_destroy
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ class User < ActiveRecord::Base
|
|||||||
#after_create :notify_sock_puppets
|
#after_create :notify_sock_puppets
|
||||||
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
|
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
|
||||||
has_many :posts, :foreign_key => "uploader_id"
|
has_many :posts, :foreign_key => "uploader_id"
|
||||||
|
has_many :post_votes
|
||||||
has_many :bans, lambda {order("bans.id desc")}
|
has_many :bans, lambda {order("bans.id desc")}
|
||||||
has_one :recent_ban, lambda {order("bans.id desc")}, :class_name => "Ban"
|
has_one :recent_ban, lambda {order("bans.id desc")}, :class_name => "Ban"
|
||||||
has_one :api_key
|
has_one :api_key
|
||||||
|
|||||||
Reference in New Issue
Block a user