* Favoring dependent: :delete_all over :destroy The `dependent: :destroy` callback is slower than `dependent: :delete` and `dependent: :delete_all`. We need only favor the `dependent: :destroy` when there's callbacks that happen. In the case of :destroy, ActiveRecord instantiates each object and then runs destroy. Whereas in the case of :delete, ActiveRecord issues a SQL command to delete the related files. It is often "safer" to use :destroy, as it guarantees that you'll instantiate the record and run it's callbacks. But sometimes you have to go with the speed of SQL. This relates to #14140. Note there is still more to consider, but given that we're looking at moving from :destroy on all of an article's page views to :delete, we might buy enough time in the callback. * Removing redundancy of article destroy Prior to this commit, `before_destroy_actions` called the `bust_cache` method which in turn called `touch_actor_latest_article_updated_at` but `bust_cache` did not pass the destroying parameter. Then `before_destroy_actions` immediately called `touch_actor_latest_article_updated_at` with `destroying: true`. With this change, we remove one of those duplicate calls. * Fixing specs regarding relationships * Fixing specs regarding relationships
35 lines
1.2 KiB
Ruby
35 lines
1.2 KiB
Ruby
# @note When we destroy the related user, it's using dependent:
|
|
# :delete for the relationship. That means no before/after
|
|
# destroy callbacks will be called on this object.
|
|
#
|
|
# @note When we destroy the related article, it's using dependent:
|
|
# :delete for the relationship. That means no before/after
|
|
# destroy callbacks will be called on this object.
|
|
class Mention < ApplicationRecord
|
|
belongs_to :user
|
|
belongs_to :mentionable, polymorphic: true
|
|
|
|
validates :user_id, presence: true, uniqueness: { scope: %i[mentionable_id mentionable_type] }
|
|
validates :mentionable_id, presence: true
|
|
validates :mentionable_type, presence: true
|
|
validate :permission
|
|
|
|
after_create_commit :send_email_notification
|
|
|
|
def self.create_all(notifiable)
|
|
Mentions::CreateAllWorker.perform_async(notifiable.id, notifiable.class.name)
|
|
end
|
|
|
|
private
|
|
|
|
def send_email_notification
|
|
user = User.find(user_id)
|
|
return unless user.email.present? && user.notification_setting.email_mention_notifications
|
|
|
|
Mentions::SendEmailNotificationWorker.perform_async(id)
|
|
end
|
|
|
|
def permission
|
|
errors.add(:mentionable_id, "is not valid.") unless mentionable&.valid?
|
|
end
|
|
end
|