The personal finance app for everyone (by everyone)
Ruby
Rebuild the account from the submission when a create fails late (#3733)
jaysbeekay0298626baf13
diff --git a/app/controllers/concerns/accountable_resource.rb b/app/controllers/concerns/accountable_resource.rbindex 083e70fe27..63883fed09 100644--- a/app/controllers/concerns/accountable_resource.rb+++ b/app/controllers/concerns/accountable_resource.rb@@ -64,12 +64,13 @@ module AccountableResource# usually the unsaved account with its nested accountable still attached --# but the opening valuation's `entries.create!` or `lock_saved_attributes!`# can raise too, and then it is an Entry or the accountable, neither of- # which the form can render. Recover the account through it, or start from- # a fresh one as `new` does.+ # which the form can render. Recover the account through it, or rebuild+ # it from what was submitted so the form comes back filled in rather+ # than blank.@account = if e.record.is_a?(Account)e.recordelse- e.record.try(:account) || Current.family.accounts.build(currency: Current.family.currency, accountable: accountable_type.new)+ e.record.try(:account) || rebuilt_account_from_submissionend@error_message = e.record.errors.full_messages.join(", ").presence || e.message# The `new` template's method-selection branch reads `@provider_configs`,@@ -128,6 +129,21 @@ module AccountableResourceendprivate+ # The account the 422 form renders when the exception carried none.+ #+ # `accountable_attributes` is defaulted the way `create_and_sync` defaults+ # it. `accountable_type=` only writes the type column: the accountable+ # object itself is built as a side effect of `accountable_attributes=` or+ # of `subtype=`. A submission that names neither -- a loan form whose+ # nested fields are all blank, say -- rebuilt an account with a nil+ # `accountable`, and the form came back without any of the type's own+ # inputs, which is the opposite of what this rescue exists to do.+ def rebuilt_account_from_submission+ attributes = account_params.except(:return_to, :opening_balance_date)+ attributes[:accountable_attributes] ||= {}+ Current.family.accounts.build(attributes)+ end+def set_link_optionsaccount_type_name = accountable_type.namediff --git a/test/controllers/loans_controller_test.rb b/test/controllers/loans_controller_test.rbindex 33854be725..4234e7cea6 100644--- a/test/controllers/loans_controller_test.rb+++ b/test/controllers/loans_controller_test.rb@@ -853,6 +853,30 @@ class LoansControllerTest < ActionDispatch::IntegrationTestassert_select "form[action='#{loans_path}']", count: 1# Rebuilt from the submission, not a blank account: what was typed comes# back for correction.+ assert_select "input[name='account[name]'][value='Loan With Bad Anchor']", count: 1+ assert_select "input[name='account[accountable_attributes][interest_rate]'][value='6']", count: 1+ end++ # The submission that trips this rescue need not carry nested attributes at+ # all. `accountable_type=` writes only the type column, so rebuilding from+ # such a submission left `accountable` nil and the 422 form came back+ # without a single one of the loan's own fields -- nothing for the borrower+ # to correct, on a form whose whole purpose here is correction.+ test "a late create failure renders the loan's fields even when the form sent none" do+ invalid_entry = Entry.new.tap(&:validate)+ Account::OpeningBalanceManager.any_instance.stubs(:set_opening_balance)+ .raises(ActiveRecord::RecordInvalid.new(invalid_entry))++ assert_no_difference "Account.count" do+ post loans_path, params: { account: {+ name: "Loan Without Nested Attributes", balance: 50_000, currency: "USD", accountable_type: "Loan"+ } }+ end++ assert_response :unprocessable_entity+ assert_select "form[action='#{loans_path}']", count: 1+ assert_select "input[name='account[name]'][value='Loan Without Nested Attributes']", count: 1+ assert_select "input[name='account[accountable_attributes][interest_rate]']", minimum: 1endprivate