account_stat.rb 917 B

12345678910111213141516171819202122232425262728293031323334
  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. def increment_count!(key)
  18. update(attributes_for_increment(key))
  19. end
  20. def decrement_count!(key)
  21. update(key => [public_send(key) - 1, 0].max)
  22. end
  23. private
  24. def attributes_for_increment(key)
  25. attrs = { key => public_send(key) + 1 }
  26. attrs[:last_status_at] = Time.now.utc if key == :statuses_count
  27. attrs
  28. end
  29. end