diff --git a/app/models/post.rb b/app/models/post.rb index e64e17aaa..8e2c3cf74 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/models/post_vote.rb b/app/models/post_vote.rb index d444d2c32..bf4c79e94 100644 --- a/app/models/post_vote.rb +++ b/app/models/post_vote.rb @@ -4,8 +4,9 @@ class PostVote < ActiveRecord::Base belongs_to :post before_validation :initialize_user, :on => :create validates_presence_of :post_id, :user_id, :score - validates_inclusion_of :score, :in => [1, -1] + validates_inclusion_of :score, :in => [SuperVoter::MAGNITUDE, 1, -1, -SuperVoter::MAGNITUDE] attr_accessible :post_id, :user_id, :score + after_destroy :update_post_on_destroy def self.prune! where("created_at < ?", 30.days.ago).delete_all @@ -13,13 +14,31 @@ class PostVote < ActiveRecord::Base def score=(x) if x == "up" - write_attribute(:score, 1) + Post.where(:id => post_id).update_all("score = score + #{magnitude}, up_score = up_score + #{magnitude}") + write_attribute(:score, magnitude) elsif x == "down" - write_attribute(:score, -1) + Post.where(:id => post_id).update_all("score = score - #{magnitude}, down_score = down_score - #{magnitude}") + write_attribute(:score, -magnitude) end end def initialize_user self.user_id = CurrentUser.user.id end + + def update_post_on_destroy + 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 magnitude + if CurrentUser.is_super_voter? + SuperVoter::MAGNITUDE + else + 1 + end + end end