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

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

@@ -77,6 +77,7 @@ class User < ActiveRecord::Base
#after_create :notify_sock_puppets
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
has_many :posts, :foreign_key => "uploader_id"
has_many :post_votes
has_many :bans, lambda {order("bans.id desc")}
has_one :recent_ban, lambda {order("bans.id desc")}, :class_name => "Ban"
has_one :api_key