Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Keep an account's subtype when an import gives it only under accountable (#5319)

Francisco R. Santos00d177cb07cb
diff --git a/app/models/family/data_importer.rb b/app/models/family/data_importer.rb
index 2f68c39542..21826a9879 100644
--- a/app/models/family/data_importer.rb
+++ b/app/models/family/data_importer.rb
@@ -349,7 +349,7 @@ class Family::DataImporter
else
# Build accountable
accountable = accountable_class.new
- accountable.subtype = accountable_data["subtype"] if accountable.respond_to?(:subtype=) && accountable_data["subtype"]
+ accountable.subtype = accountable_data["subtype"] if accountable.respond_to?(:subtype=) && accountable_data["subtype"].present?
assign_liability_details(accountable, accountable_data)
# Copy any other accountable attributes
@@ -371,12 +371,16 @@ class Family::DataImporter
balance: data["balance"].to_d,
cash_balance: data["cash_balance"]&.to_d || data["balance"].to_d,
currency: data["currency"] || @family.currency,
- subtype: data["subtype"],
institution_name: data["institution_name"],
institution_domain: data["institution_domain"],
notes: data["notes"],
status: importable_account_status(data["status"])
)
+ # `Account#subtype=` writes through to the accountable, so assigning a
+ # nil top-level subtype would wipe the nested one. The nested value
+ # wins, matching SureImport::Preflight#validate_accountables.
+ imported_subtype = accountable_data["subtype"].presence || data["subtype"].presence
+ account.subtype = imported_subtype if imported_subtype
# A file from before the setting existed leaves the account's own.
account.cost_basis_method = data["cost_basis_method"] if Account.cost_basis_methods.key?(data["cost_basis_method"])
# Older exports predate the choice and keep the account's own.
diff --git a/test/models/family/data_importer_test.rb b/test/models/family/data_importer_test.rb
index 942a81e6eb..98db6d9b9e 100644
--- a/test/models/family/data_importer_test.rb
+++ b/test/models/family/data_importer_test.rb
@@ -160,6 +160,30 @@ class Family::DataImporterTest < ActiveSupport::TestCase
assert_nil card.due_day
end
+ test "keeps the nested accountable subtype when the account has no top-level subtype" do
+ ndjson = build_ndjson([
+ { type: "Account", data: { id: "a1", name: "Nested", balance: "0", currency: "USD", accountable_type: "Depository", accountable: { subtype: "savings" } } },
+ { type: "Account", data: { id: "a2", name: "Both", balance: "0", currency: "USD", accountable_type: "Depository", subtype: "savings", accountable: { subtype: "checking" } } }
+ ])
+
+ Family::DataImporter.new(@family, ndjson).import!
+
+ assert_equal "savings", @family.accounts.find_by!(name: "Nested").accountable.subtype
+ assert_equal "checking", @family.accounts.find_by!(name: "Both").accountable.subtype
+ end
+
+ test "does not store a blank nested accountable subtype" do
+ ndjson = build_ndjson([
+ { type: "Account", data: { id: "a1", name: "Blank Nested", balance: "0", currency: "USD", accountable_type: "Depository", accountable: { subtype: "" } } },
+ { type: "Account", data: { id: "a2", name: "Blank Nested With Top Level", balance: "0", currency: "USD", subtype: "savings", accountable_type: "Depository", accountable: { subtype: "" } } }
+ ])
+
+ Family::DataImporter.new(@family, ndjson).import!
+
+ assert_nil @family.accounts.find_by!(name: "Blank Nested").accountable.subtype
+ assert_equal "savings", @family.accounts.find_by!(name: "Blank Nested With Top Level").accountable.subtype
+ end
+
test "imports a loan whose detail columns are null in the export" do
ndjson = build_ndjson([
{