fixed comment vote functional test

This commit is contained in:
albert
2011-04-01 19:01:17 -04:00
parent e7748e1fc9
commit cbd877d020
43 changed files with 135 additions and 20369 deletions

View File

@@ -37,23 +37,14 @@ class Comment < ActiveRecord::Base
end
def vote!(score)
if !CurrentUser.user.can_comment_vote?
raise CommentVote::Error.new("You can only vote ten times an hour on comments")
elsif score == "down" && creator.is_janitor?
raise CommentVote::Error.new("You cannot downvote janitor/moderator/admin comments")
elsif votes.find_by_user_id(CurrentUser.user.id).nil?
if score == "up"
increment!(:score)
elsif score == "down"
decrement!(:score)
end
votes.create
else
raise CommentVote::Error.new("You have already voted for this comment")
vote = votes.create(:score => score)
if vote.errors.any?
raise CommentVote::Error.new(vote.errors.full_messages.join("; "))
elsif vote.is_positive?
increment!(:score)
elsif vote.is_negative?
decrement!(:score)
end
end
end

View File

@@ -4,12 +4,42 @@ class CommentVote < ActiveRecord::Base
belongs_to :comment
belongs_to :user
before_validation :initialize_user, :on => :create
validates_presence_of :user_id, :comment_id
validates_presence_of :user_id, :comment_id, :score
validates_uniqueness_of :user_id, :scope => :comment_id
validate :validate_user_can_vote
validate :validate_comment_can_be_down_voted
validates_inclusion_of :score, :in => [-1, 1], :message => "must be 1 or -1"
def self.prune!
destroy_all(["created_at < ?", 14.days.ago])
end
def validate_user_can_vote
if !user.can_comment_vote?
errors.add :user, "can not comment vote"
false
else
true
end
end
def validate_comment_can_be_down_voted
if is_negative? && comment.creator.is_janitor?
errors.add :user, "can not downvote a janitor comment"
false
else
true
end
end
def is_positive?
score == 1
end
def is_negative?
score == -1
end
def initialize_user
self.user_id = CurrentUser.user.id
end

View File

@@ -1,5 +1,6 @@
class NoteVersion < ActiveRecord::Base
before_validation :initialize_updater
belongs_to :updater, :class_name => "User"
def initialize_updater
self.updater_id = CurrentUser.id

View File

@@ -9,4 +9,8 @@ class PoolVersion < ActiveRecord::Base
self.updater_id = CurrentUser.id
self.updater_ip_addr = CurrentUser.ip_addr
end
def post_id_array
@post_id_array ||= post_ids.scan(/\d+/).map(&:to_i)
end
end