Skip to content

Commit

Permalink
Only update account balance if changed (#1676)
Browse files Browse the repository at this point in the history
* Only update balance if changed

* Update test assertions
  • Loading branch information
zachgoll authored Jan 24, 2025
1 parent 61321f6 commit 43dd16e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ def enrich_data_later
end

def update_with_sync!(attributes)
should_update_balance = attributes[:balance] && attributes[:balance].to_d != balance

transaction do
update!(attributes)
update_balance!(attributes[:balance]) if attributes[:balance]
update_balance!(attributes[:balance]) if should_update_balance
end

sync_later
Expand Down
6 changes: 3 additions & 3 deletions test/interfaces/accountable_resource_interface_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ module AccountableResourceInterfaceTest
assert_equal "#{@account.accountable_name.humanize} account created", flash[:notice]
end

test "updates account balance by creating new valuation" do
test "updates account balance by creating new valuation if balance has changed" do
assert_difference [ "Account::Entry.count", "Account::Valuation.count" ], 1 do
patch account_url(@account), params: {
account: {
balance: 10000
balance: 12000
}
}
end
Expand All @@ -81,7 +81,7 @@ module AccountableResourceInterfaceTest
assert_no_difference [ "Account::Entry.count", "Account::Valuation.count" ] do
patch account_url(@account), params: {
account: {
balance: 10000
balance: 12000
}
}
end
Expand Down

0 comments on commit 43dd16e

Please sign in to comment.