Skip to content

Commit

Permalink
Dont update last_status_at unless positive value
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski committed Apr 16, 2024
1 parent 0d84f1f commit 0fc3a4e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/concerns/account/counters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def initial_values(key, value)
def duplicate_values(key, value)
Arel.sql(
["#{key} = (account_stats.#{key} + #{value})", 'updated_at = CURRENT_TIMESTAMP'].tap do |values|
values << 'last_status_at = CURRENT_TIMESTAMP' if key == :statuses_count
values << 'last_status_at = CURRENT_TIMESTAMP' if key == :statuses_count && value.positive?
end.join(', ')
)
end
Expand Down
13 changes: 13 additions & 0 deletions spec/models/concerns/account/counters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,18 @@

expect(account.statuses_count).to eq 5
end

it 'preserves last_status_at when decrementing statuses_count' do
account_stat = Fabricate(
:account_stat,
account: account,
last_status_at: 3.days.ago,
statuses_count: 10
)

expect { account.decrement_count!(:statuses_count) }
.to change(account_stat.reload, :statuses_count).by(-1)
.and not_change(account_stat.reload, :last_status_at)
end
end
end

0 comments on commit 0fc3a4e

Please sign in to comment.