You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
688 B

  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