status_stat.rb 891 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: status_stats
  5. #
  6. # id :bigint(8) not null, primary key
  7. # status_id :bigint(8) not null
  8. # replies_count :bigint(8) default(0), not null
  9. # reblogs_count :bigint(8) default(0), not null
  10. # favourites_count :bigint(8) default(0), not null
  11. # created_at :datetime not null
  12. # updated_at :datetime not null
  13. #
  14. class StatusStat < ApplicationRecord
  15. belongs_to :status, inverse_of: :status_stat
  16. after_commit :reset_parent_cache
  17. def replies_count
  18. [attributes['replies_count'], 0].max
  19. end
  20. def reblogs_count
  21. [attributes['reblogs_count'], 0].max
  22. end
  23. def favourites_count
  24. [attributes['favourites_count'], 0].max
  25. end
  26. private
  27. def reset_parent_cache
  28. Rails.cache.delete("statuses/#{status_id}")
  29. end
  30. end