diff --git a/app/controllers/ajax/comment_controller.rb b/app/controllers/ajax/comment_controller.rb index ead52c03..7d40b3bd 100644 --- a/app/controllers/ajax/comment_controller.rb +++ b/app/controllers/ajax/comment_controller.rb @@ -35,10 +35,7 @@ class Ajax::CommentController < ApplicationController return end - comment.user.decrement! :commented_count - comment.answer.decrement! :comment_count - Notification.denotify comment.answer.user, comment - @count = comment.answer.comment_count + @count = comment.answer.comment_count - 1 comment.destroy @status = :okay diff --git a/app/models/comment.rb b/app/models/comment.rb index 1cfe26b4..cf5001a7 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -6,6 +6,18 @@ class Comment < ActiveRecord::Base 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) Notifications::Commented end diff --git a/app/models/question.rb b/app/models/question.rb index 53aeb9d9..5f8289e4 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -5,10 +5,13 @@ class Question < ActiveRecord::Base validates :content, length: { maximum: 255 } + before_destroy do + user.decrement! :asked_count unless self.author_is_anonymous + end + def can_be_removed? return false if self.answers.count > 0 return false if Inbox.where(question: self).count > 1 - self.user.decrement! :asked_count unless self.author_is_anonymous true end end diff --git a/app/models/user.rb b/app/models/user.rb index 95941245..57a0f25d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -107,10 +107,7 @@ class User < ActiveRecord::Base end def comment(answer, content) - comment = Comment.create!(user: self, answer: answer, content: content) - Notification.notify answer.user, comment unless answer.user == self - increment! :commented_count - answer.increment! :comment_count + Comment.create!(user: self, answer: answer, content: content) end # @return [Boolean] is the user a moderator?