The personal finance app for everyone (by everyone)
Ruby
fix: currency being ignored for properties (#1556)
Thiago Diniz da Silveirac9f9e04071ae
diff --git a/app/controllers/concerns/accountable_resource.rb b/app/controllers/concerns/accountable_resource.rbindex 23fd761073..479ad9efcb 100644--- a/app/controllers/concerns/accountable_resource.rb+++ b/app/controllers/concerns/accountable_resource.rb@@ -62,8 +62,10 @@ module AccountableResourceendend- # Update remaining account attributes- update_params = account_params.except(:return_to, :balance, :currency, :opening_balance_date)+ # Update remaining account attributes. Note: currency is intentionally allowed+ # here so all account types (depositories, credit cards, loans, etc.) can+ # have their currency changed via this shared update path.+ update_params = account_params.except(:return_to, :balance, :opening_balance_date)unless @account.update(update_params)@error_message = @account.errors.full_messages.join(", ")render :edit, status: :unprocessable_entitydiff --git a/app/controllers/properties_controller.rb b/app/controllers/properties_controller.rbindex e937c4fb69..1de7a5f8cb 100644--- a/app/controllers/properties_controller.rb+++ b/app/controllers/properties_controller.rb@@ -10,7 +10,12 @@ class PropertiesController < ApplicationControllerdef create@account = Current.family.accounts.create!(- property_params.merge(currency: Current.family.currency, balance: 0, status: "draft", owner: Current.user)+ property_params.merge(+ balance: 0,+ status: "draft",+ owner: Current.user,+ currency: property_params[:currency].presence || Current.family.currency+ ))@account.auto_share_with_family! if Current.family.share_all_by_default?@@ -39,9 +44,14 @@ class PropertiesController < ApplicationControllerenddef update_balances- result = @account.set_current_balance(balance_params[:balance].to_d)+ result = nil+ Account.transaction do+ @account.update!(currency: balance_params[:currency]) if balance_params[:currency].present?+ result = @account.set_current_balance(balance_params[:balance].to_d)+ raise ActiveRecord::Rollback unless result.success?+ end- if result.success?+ if result&.success?@success_message = "Balance updated successfully."if @account.active?@@ -50,7 +60,7 @@ class PropertiesController < ApplicationControllerredirect_to address_property_path(@account)endelse- @error_message = result.error_message+ @error_message = result&.error_messagerender :balances, status: :unprocessable_entityendend@@ -93,6 +103,7 @@ class PropertiesController < ApplicationControllerparams.require(:account).permit(:name,+ :currency,:accountable_type,:institution_name,:institution_domain,diff --git a/test/controllers/properties_controller_test.rb b/test/controllers/properties_controller_test.rbindex 890f8b17e9..ce5a97847b 100644--- a/test/controllers/properties_controller_test.rb+++ b/test/controllers/properties_controller_test.rb@@ -14,6 +14,7 @@ class PropertiesControllerTest < ActionDispatch::IntegrationTestaccount: {name: "New Property",subtype: "house",+ currency: "EUR",institution_name: "Property Lender",institution_domain: "propertylender.example",notes: "Property notes",@@ -31,6 +32,7 @@ class PropertiesControllerTest < ActionDispatch::IntegrationTestassert created_account.accountable.is_a?(Property)assert_equal "draft", created_account.statusassert_equal 0, created_account.balance+ assert_equal "EUR", created_account.currencyassert_equal "Property Lender", created_account[:institution_name]assert_equal "propertylender.example", created_account[:institution_domain]assert_equal "Property notes", created_account[:notes]@@ -93,8 +95,12 @@ class PropertiesControllerTest < ActionDispatch::IntegrationTest}}+ @account.reload+ assert_equal 600000, @account.balance+ assert_equal "EUR", @account.currency+# If account is active, it renders balances view; otherwise redirects to address- if @account.reload.active?+ if @account.active?assert_response :successelseassert_redirected_to address_property_path(@account)diff --git a/test/interfaces/accountable_resource_interface_test.rb b/test/interfaces/accountable_resource_interface_test.rbindex 5ad9598e12..a17b6cf9ee 100644--- a/test/interfaces/accountable_resource_interface_test.rb+++ b/test/interfaces/accountable_resource_interface_test.rb@@ -14,4 +14,18 @@ module AccountableResourceInterfaceTestget edit_account_url(@account)assert_response :successend++ test "update saves currency change" do+ @account.update!(currency: "USD")++ patch send("#{@account.accountable_type.underscore}_path", @account), params: {+ account: {+ name: @account.name,+ currency: "EUR"+ }+ }++ @account.reload+ assert_equal "EUR", @account.currency+ endend