merge branch changeable-votes
This commit is contained in:
@@ -69,7 +69,41 @@ class Comment < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module VoteMethods
|
||||
def vote!(val)
|
||||
numerical_score = val == "up" ? 1 : -1
|
||||
vote = votes.create(:score => numerical_score)
|
||||
|
||||
if vote.errors.empty?
|
||||
if vote.is_positive?
|
||||
update_column(:score, score + 1)
|
||||
elsif vote.is_negative?
|
||||
update_column(:score, score - 1)
|
||||
end
|
||||
end
|
||||
|
||||
return vote
|
||||
end
|
||||
|
||||
def unvote!
|
||||
vote = votes.where("user_id = ?", CurrentUser.user.id).first
|
||||
|
||||
if vote
|
||||
if vote.is_positive?
|
||||
update_column(:score, score - 1)
|
||||
else
|
||||
update_column(:score, score + 1)
|
||||
end
|
||||
|
||||
vote.destroy
|
||||
else
|
||||
raise CommentVote::Error.new("You have not voted for this comment")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
include VoteMethods
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.user.id
|
||||
@@ -122,21 +156,6 @@ class Comment < ActiveRecord::Base
|
||||
do_not_bump_post == "1"
|
||||
end
|
||||
|
||||
def vote!(val)
|
||||
numerical_score = val == "up" ? 1 : -1
|
||||
vote = votes.create(:score => numerical_score)
|
||||
|
||||
if vote.errors.empty?
|
||||
if vote.is_positive?
|
||||
update_column(:score, score + 1)
|
||||
elsif vote.is_negative?
|
||||
update_column(:score, score - 1)
|
||||
end
|
||||
end
|
||||
|
||||
return vote
|
||||
end
|
||||
|
||||
def editable_by?(user)
|
||||
creator_id == user.id || user.is_janitor?
|
||||
end
|
||||
|
||||
@@ -652,6 +652,24 @@ class Post < ActiveRecord::Base
|
||||
raise PostVote::Error.new("You have already voted for this post")
|
||||
end
|
||||
end
|
||||
|
||||
def unvote!
|
||||
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.update_all("score = score - 1, up_score = up_score - 1", {:id => id})
|
||||
self.score -= 1
|
||||
else
|
||||
Post.update_all("score = score + 1, down_score = down_score + 1", {:id => id})
|
||||
self.score += 1
|
||||
end
|
||||
|
||||
vote.destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module CountMethods
|
||||
|
||||
Reference in New Issue
Block a user