Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(accounts): persist subtype when it is assigned before accountable_type (#2432)

Artem Danilov92456936fb93
diff --git a/app/models/account.rb b/app/models/account.rb
index feb2c193d5..8ed07849db 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -98,19 +98,43 @@ class Account < ApplicationRecord
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
delegate :subtype, to: :accountable, allow_nil: true
- # Writer for subtype that delegates to the accountable.
- # This allows forms to set subtype directly on the account.
+ # Writer for subtype that delegates to the accountable, allowing forms to set
+ # subtype directly on the account.
#
- # On create the accountable may not be built yet: mass-assignment can apply
- # `subtype` before `accountable_attributes` (which is what builds the
- # accountable via accepts_nested_attributes_for). With no accountable in place
- # `accountable&.subtype = value` is a silent no-op and the chosen subtype is
- # dropped. Build the accountable from the delegated type first so the value is
- # preserved; the later `accountable_attributes` assignment (update_only) then
- # updates this same record instead of building a new one.
+ # On create the accountable is not built yet, and the chosen subtype is easy to
+ # drop because of mass-assignment ordering. Two cases:
+ #
+ # 1. `subtype` is applied while `accountable_type` is already known — build
+ # the accountable from the delegated type so the value lands on it. The
+ # later `accountable_attributes` assignment (update_only) then updates that
+ # same record instead of building a new one.
+ # 2. `subtype` is applied *before* `accountable_type` — this is the real
+ # controller path: strong-params `permit` preserves filter order, and
+ # `account_params` lists `:subtype` before `:accountable_type`, so the
+ # writer runs while the type (and thus `accountable_class`) is still
+ # unknown. We can't build the accountable yet, so stash the value and
+ # apply it from `accountable_type=` once the type is set.
def subtype=(value)
self.accountable = accountable_class.new if accountable.nil? && accountable_type.present?
- accountable&.subtype = value
+
+ if accountable
+ accountable.subtype = value
+ else
+ @deferred_subtype = value
+ end
+ end
+
+ # Applies a subtype that arrived before the type was known (see `subtype=`
+ # case 2). `super` resolves `accountable_type`/`accountable_class` first, then
+ # the re-entrant `subtype=` builds the accountable and assigns the value.
+ def accountable_type=(value)
+ super
+
+ if defined?(@deferred_subtype)
+ pending = @deferred_subtype
+ remove_instance_variable(:@deferred_subtype)
+ self.subtype = pending
+ end
end
accepts_nested_attributes_for :accountable, update_only: true
diff --git a/test/models/account_test.rb b/test/models/account_test.rb
index 3bceb5dae3..db36249c60 100644
--- a/test/models/account_test.rb
+++ b/test/models/account_test.rb
@@ -129,6 +129,40 @@ class AccountTest < ActiveSupport::TestCase
assert_equal "checking", account.subtype
end
+ test "subtype assigned before accountable_type is not dropped" do
+ # The real controller path: strong-params `permit` preserves filter order,
+ # and `account_params` lists `:subtype` before `:accountable_type`, so the
+ # subtype writer runs while the type is still unknown.
+ account = Account.new
+ account.subtype = "savings"
+ account.accountable_type = "Depository"
+
+ assert_not_nil account.accountable
+ assert_equal "savings", account.subtype
+ assert_equal "savings", account.accountable.subtype
+ end
+
+ test "subtype persists on create when attributes arrive in permit order" do
+ Account.any_instance.stubs(:sync_later)
+
+ # Mirrors `account_params`: `permit` yields keys in filter order, so the
+ # create hash carries `subtype` before `accountable_type` — the ordering
+ # that previously dropped the subtype on create.
+ account = Account.create_and_sync({
+ family: @family,
+ owner: @admin,
+ name: "Savings Account",
+ balance: 100,
+ subtype: "savings",
+ currency: "USD",
+ accountable_type: "Depository"
+ })
+
+ assert account.persisted?
+ assert_equal "savings", account.reload.subtype
+ assert_equal "savings", account.accountable.subtype
+ end
+
test "accountable display names expose singular and group contexts" do
assert_equal "Investment", Investment.singular_display_name
assert_equal "Investments", Investment.display_name