1
0

favourite.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: favourites
  5. #
  6. # id :bigint(8) not null, primary key
  7. # created_at :datetime not null
  8. # updated_at :datetime not null
  9. # account_id :bigint(8) not null
  10. # status_id :bigint(8) not null
  11. #
  12. class Favourite < ApplicationRecord
  13. include Paginable
  14. update_index('statuses', :status)
  15. belongs_to :account, inverse_of: :favourites
  16. belongs_to :status, inverse_of: :favourites
  17. has_one :notification, as: :activity, dependent: :destroy
  18. validates :status_id, uniqueness: { scope: :account_id }
  19. before_validation do
  20. self.status = status.reblog if status&.reblog?
  21. end
  22. after_create :increment_cache_counters
  23. after_destroy :decrement_cache_counters
  24. after_destroy :invalidate_cleanup_info
  25. private
  26. def increment_cache_counters
  27. status&.increment_count!(:favourites_count)
  28. end
  29. def decrement_cache_counters
  30. return if association(:status).loaded? && status.marked_for_destruction?
  31. status&.decrement_count!(:favourites_count)
  32. end
  33. def invalidate_cleanup_info
  34. return unless status&.account_id == account_id && account.local?
  35. account.statuses_cleanup_policy&.invalidate_last_inspected(status, :unfav)
  36. end
  37. end