Refactor counters to use `counter_cache`
This commit is contained in:
parent
aaee1bc324
commit
8cbfd273bc
|
@ -1,8 +1,8 @@
|
||||||
class Answer < ApplicationRecord
|
class Answer < ApplicationRecord
|
||||||
extend Answer::TimelineMethods
|
extend Answer::TimelineMethods
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user, counter_cache: :answered_count
|
||||||
belongs_to :question
|
belongs_to :question, counter_cache: :answer_count
|
||||||
has_many :comments, dependent: :destroy
|
has_many :comments, dependent: :destroy
|
||||||
has_many :smiles, class_name: "Appendable::Reaction", foreign_key: :parent_id, dependent: :destroy
|
has_many :smiles, class_name: "Appendable::Reaction", foreign_key: :parent_id, dependent: :destroy
|
||||||
has_many :subscriptions, dependent: :destroy
|
has_many :subscriptions, dependent: :destroy
|
||||||
|
@ -18,15 +18,12 @@ class Answer < ApplicationRecord
|
||||||
|
|
||||||
SHORT_ANSWER_MAX_LENGTH = 640
|
SHORT_ANSWER_MAX_LENGTH = 640
|
||||||
|
|
||||||
# rubocop:disable Rails/SkipsModelValidations
|
|
||||||
after_create do
|
after_create do
|
||||||
Inbox.where(user: self.user, question: self.question).destroy_all
|
Inbox.where(user: self.user, question: self.question).destroy_all
|
||||||
|
|
||||||
Notification.notify self.question.user, self unless self.question.user == self.user or self.question.user.nil?
|
Notification.notify self.question.user, self unless self.question.user == self.user or self.question.user.nil?
|
||||||
Subscription.subscribe self.user, self
|
Subscription.subscribe self.user, self
|
||||||
Subscription.subscribe self.question.user, self unless self.question.author_is_anonymous
|
Subscription.subscribe self.question.user, self unless self.question.author_is_anonymous
|
||||||
user.increment! :answered_count
|
|
||||||
question.increment! :answer_count
|
|
||||||
end
|
end
|
||||||
|
|
||||||
before_destroy do
|
before_destroy do
|
||||||
|
@ -39,19 +36,15 @@ class Answer < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
user&.decrement! :answered_count
|
|
||||||
question&.decrement! :answer_count
|
|
||||||
self.smiles.each do |smile|
|
self.smiles.each do |smile|
|
||||||
Notification.denotify self.user, smile
|
Notification.denotify self.user, smile
|
||||||
end
|
end
|
||||||
self.comments.each do |comment|
|
self.comments.each do |comment|
|
||||||
comment.user&.decrement! :commented_count
|
|
||||||
Subscription.denotify comment, self
|
Subscription.denotify comment, self
|
||||||
end
|
end
|
||||||
Notification.denotify question&.user, self
|
Notification.denotify question&.user, self
|
||||||
Subscription.destruct self
|
Subscription.destruct self
|
||||||
end
|
end
|
||||||
# rubocop:enable Rails/SkipsModelValidations
|
|
||||||
|
|
||||||
def notification_type(*_args)
|
def notification_type(*_args)
|
||||||
Notification::QuestionAnswered
|
Notification::QuestionAnswered
|
||||||
|
|
|
@ -1,18 +1,15 @@
|
||||||
class Comment < ApplicationRecord
|
class Comment < ApplicationRecord
|
||||||
belongs_to :user
|
belongs_to :user, counter_cache: :commented_count
|
||||||
belongs_to :answer
|
belongs_to :answer, counter_cache: :comment_count
|
||||||
validates :user_id, presence: true
|
validates :user_id, presence: true
|
||||||
validates :answer_id, presence: true
|
validates :answer_id, presence: true
|
||||||
has_many :smiles, class_name: "Appendable::Reaction", foreign_key: :parent_id, dependent: :destroy
|
has_many :smiles, class_name: "Appendable::Reaction", foreign_key: :parent_id, dependent: :destroy
|
||||||
|
|
||||||
validates :content, length: { maximum: 512 }
|
validates :content, length: { maximum: 512 }
|
||||||
|
|
||||||
# rubocop:disable Rails/SkipsModelValidations
|
|
||||||
after_create do
|
after_create do
|
||||||
Subscription.subscribe self.user, answer, false
|
Subscription.subscribe self.user, answer, false
|
||||||
Subscription.notify self, answer
|
Subscription.notify self, answer
|
||||||
user.increment! :commented_count
|
|
||||||
answer.increment! :comment_count
|
|
||||||
end
|
end
|
||||||
|
|
||||||
before_destroy do
|
before_destroy do
|
||||||
|
@ -25,10 +22,7 @@ class Comment < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
Subscription.denotify self, answer
|
Subscription.denotify self, answer
|
||||||
user&.decrement! :commented_count
|
|
||||||
answer&.decrement! :comment_count
|
|
||||||
end
|
end
|
||||||
# rubocop:enable Rails/SkipsModelValidations
|
|
||||||
|
|
||||||
def notification_type(*_args)
|
def notification_type(*_args)
|
||||||
Notification::Commented
|
Notification::Commented
|
||||||
|
|
Loading…
Reference in New Issue