From 43dd16e3fb06495b2a01358cd68acf24a3c1f9a1 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Thu, 23 Jan 2025 21:14:01 -0500 Subject: [PATCH] Only update account balance if changed (#1676) * Only update balance if changed * Update test assertions --- app/models/account.rb | 4 +++- test/interfaces/accountable_resource_interface_test.rb | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/models/account.rb b/app/models/account.rb index 19d883ca..b23a8e15 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -135,9 +135,11 @@ class Account < ApplicationRecord 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 diff --git a/test/interfaces/accountable_resource_interface_test.rb b/test/interfaces/accountable_resource_interface_test.rb index d44baf7e..913854a3 100644 --- a/test/interfaces/accountable_resource_interface_test.rb +++ b/test/interfaces/accountable_resource_interface_test.rb @@ -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 @@ -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