diff options
author | Thibaut Girka <thib@sitedethib.com> | 2019-10-03 11:10:12 +0200 |
---|---|---|
committer | Thibaut Girka <thib@sitedethib.com> | 2019-10-03 11:10:12 +0200 |
commit | a84aedb7a7f050cbf5293351723f55a904992b3a (patch) | |
tree | 769d397390665bcddf9b44fc1ad4418b2757c5f1 /app/models/account_stat.rb | |
parent | 857c67f31b23b9c496e07eda41755ba449a37f17 (diff) | |
parent | bae268b2f6501ccb0ceeb8dd36180698839c4d3c (diff) |
Merge branch 'master' into glitch-soc/merge-upstream
Conflicts: - app/models/media_attachment.rb Upstream raised max image size from 8MB to 10MB while our limit is configurable. Raised the default to 10MB.
Diffstat (limited to 'app/models/account_stat.rb')
-rw-r--r-- | app/models/account_stat.rb | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/app/models/account_stat.rb b/app/models/account_stat.rb index 6d1097cec..c84e4217c 100644 --- a/app/models/account_stat.rb +++ b/app/models/account_stat.rb @@ -11,19 +11,36 @@ # created_at :datetime not null # updated_at :datetime not null # last_status_at :datetime +# lock_version :integer default(0), not null # class AccountStat < ApplicationRecord belongs_to :account, inverse_of: :account_stat - update_index('accounts#account', :account) if Chewy.enabled? + update_index('accounts#account', :account) def increment_count!(key) update(attributes_for_increment(key)) + rescue ActiveRecord::StaleObjectError + begin + reload_with_id + rescue ActiveRecord::RecordNotFound + # Nothing to do + else + retry + end end def decrement_count!(key) update(key => [public_send(key) - 1, 0].max) + rescue ActiveRecord::StaleObjectError + begin + reload_with_id + rescue ActiveRecord::RecordNotFound + # Nothing to do + else + retry + end end private @@ -33,4 +50,9 @@ class AccountStat < ApplicationRecord attrs[:last_status_at] = Time.now.utc if key == :statuses_count attrs end + + def reload_with_id + self.id = find_by!(account: account).id if new_record? + reload + end end |