account_counters.rb 688 B

123456789101112131415161718192021222324252627282930313233
  1. # frozen_string_literal: true
  2. module AccountCounters
  3. extend ActiveSupport::Concern
  4. included do
  5. has_one :account_stat, inverse_of: :account
  6. after_save :save_account_stat
  7. end
  8. delegate :statuses_count,
  9. :statuses_count=,
  10. :following_count,
  11. :following_count=,
  12. :followers_count,
  13. :followers_count=,
  14. :increment_count!,
  15. :decrement_count!,
  16. :last_status_at,
  17. to: :account_stat
  18. def account_stat
  19. super || build_account_stat
  20. end
  21. private
  22. def save_account_stat
  23. return unless association(:account_stat).loaded? && account_stat&.changed?
  24. account_stat.save
  25. end
  26. end