about summary refs log tree commit diff
path: root/app/models/concerns/account_counters.rb
blob: 6e25e1905e4d6919ed91c3fda3497ffb2933d04a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

module AccountCounters
  extend ActiveSupport::Concern

  included do
    has_one :account_stat, inverse_of: :account
    after_save :save_account_stat
  end

  delegate :statuses_count,
           :statuses_count=,
           :following_count,
           :following_count=,
           :followers_count,
           :followers_count=,
           :increment_count!,
           :decrement_count!,
           :last_status_at,
           to: :account_stat

  def account_stat
    super || build_account_stat
  end

  private

  def save_account_stat
    return unless association(:account_stat).loaded? && account_stat&.changed?

    account_stat.save
  end
end