more before_destroy and after_create

This commit is contained in:
nilsding 2014-12-28 21:58:11 +01:00
parent bb3e431393
commit 40f54fd19a
4 changed files with 18 additions and 9 deletions

View File

@ -35,10 +35,7 @@ class Ajax::CommentController < ApplicationController
return return
end end
comment.user.decrement! :commented_count @count = comment.answer.comment_count - 1
comment.answer.decrement! :comment_count
Notification.denotify comment.answer.user, comment
@count = comment.answer.comment_count
comment.destroy comment.destroy
@status = :okay @status = :okay

View File

@ -6,6 +6,18 @@ class Comment < ActiveRecord::Base
validates :content, length: { maximum: 160 } validates :content, length: { maximum: 160 }
after_create do
Notification.notify answer.user, self unless answer.user == self.user
user.increment! :commented_count
answer.increment! :comment_count
end
before_destroy do
Notification.denotify answer.user, self unless answer.user == self.user
user.decrement! :commented_count
answer.decrement! :comment_count
end
def notification_type(*_args) def notification_type(*_args)
Notifications::Commented Notifications::Commented
end end

View File

@ -5,10 +5,13 @@ class Question < ActiveRecord::Base
validates :content, length: { maximum: 255 } validates :content, length: { maximum: 255 }
before_destroy do
user.decrement! :asked_count unless self.author_is_anonymous
end
def can_be_removed? def can_be_removed?
return false if self.answers.count > 0 return false if self.answers.count > 0
return false if Inbox.where(question: self).count > 1 return false if Inbox.where(question: self).count > 1
self.user.decrement! :asked_count unless self.author_is_anonymous
true true
end end
end end

View File

@ -107,10 +107,7 @@ class User < ActiveRecord::Base
end end
def comment(answer, content) def comment(answer, content)
comment = Comment.create!(user: self, answer: answer, content: content) Comment.create!(user: self, answer: answer, content: content)
Notification.notify answer.user, comment unless answer.user == self
increment! :commented_count
answer.increment! :comment_count
end end
# @return [Boolean] is the user a moderator? # @return [Boolean] is the user a moderator?