account_stat.rb 981 B

123456789101112131415161718192021222324252627282930313233343536
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: account_stats
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8) not null
  8. # statuses_count :bigint(8) default(0), not null
  9. # following_count :bigint(8) default(0), not null
  10. # followers_count :bigint(8) default(0), not null
  11. # created_at :datetime not null
  12. # updated_at :datetime not null
  13. # last_status_at :datetime
  14. #
  15. class AccountStat < ApplicationRecord
  16. belongs_to :account, inverse_of: :account_stat
  17. update_index('accounts#account', :account) if Chewy.enabled?
  18. def increment_count!(key)
  19. update(attributes_for_increment(key))
  20. end
  21. def decrement_count!(key)
  22. update(key => [public_send(key) - 1, 0].max)
  23. end
  24. private
  25. def attributes_for_increment(key)
  26. attrs = { key => public_send(key) + 1 }
  27. attrs[:last_status_at] = Time.now.utc if key == :statuses_count
  28. attrs
  29. end
  30. end