diff --git a/app/models/post.rb b/app/models/post.rb index 0f662e034..2c47e7845 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1063,7 +1063,7 @@ class Post < ActiveRecord::Base !PostVote.exists?(:user_id => user.id, :post_id => id) end - def vote!(score, voter = CurrentUser.user) + def vote!(vote, voter = CurrentUser.user) unless voter.is_voter? raise PostVote::Error.new("You do not have permission to vote") end @@ -1072,7 +1072,7 @@ class Post < ActiveRecord::Base raise PostVote::Error.new("You have already voted for this post") end - votes.create!(user: voter, score: vote) + votes.create!(user: voter, vote: vote) reload # PostVote.create modifies our score. Reload to get the new score. end diff --git a/app/models/post_vote.rb b/app/models/post_vote.rb index 26fd280d4..ac503d64d 100644 --- a/app/models/post_vote.rb +++ b/app/models/post_vote.rb @@ -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 diff --git a/test/unit/post_vote_test.rb b/test/unit/post_vote_test.rb index 4b35fa200..27e820225 100644 --- a/test/unit/post_vote_test.rb +++ b/test/unit/post_vote_test.rb @@ -14,17 +14,17 @@ class PostVoteTest < ActiveSupport::TestCase context "Voting for a post" do should "interpret up as +1 score" do - vote = PostVote.create(:post_id => @post.id, :score => "up") + vote = PostVote.create(:post_id => @post.id, :vote => "up") assert_equal(1, vote.score) end should "interpret down as -1 score" do - vote = PostVote.create(:post_id => @post.id, :score => "down") + vote = PostVote.create(:post_id => @post.id, :vote => "down") assert_equal(-1, vote.score) end should "not accept any other scores" do - vote = PostVote.create(:post_id => @post.id, :score => "xxx") + vote = PostVote.create(:post_id => @post.id, :vote => "xxx") assert(vote.errors.any?) end end