The personal finance app for everyone (by everyone)
Ruby
Keep property details and address through a backup and restore (#5056)
arnaurodondev84d71ed2249b
diff --git a/app/models/family/data_exporter.rb b/app/models/family/data_exporter.rbindex a4801cdd4b..548bfad5e3 100644--- a/app/models/family/data_exporter.rb+++ b/app/models/family/data_exporter.rb@@ -248,13 +248,20 @@ class Family::DataExporter# Export accounts with full accountable data@family.accounts.includes(:accountable).find_each do |account|+ data = account.as_json(+ include: {+ accountable: {}+ }+ )+ # A property's address lives in its own table, so it is not part of+ # the accountable's columns.+ if account.accountable.is_a?(Property) && (address = account.accountable.address)+ data["accountable"]["address"] = address.as_json(only: Family::DataImporter::PROPERTY_ADDRESS_ATTRIBUTES)+ end+lines << {type: "Account",- data: account.as_json(- include: {- accountable: {}- }- )+ data: data}.to_jsonenddiff --git a/app/models/family/data_importer.rb b/app/models/family/data_importer.rbindex 946a61e329..247a862f31 100644--- a/app/models/family/data_importer.rb+++ b/app/models/family/data_importer.rb@@ -32,6 +32,8 @@ class Family::DataImporterendSUPPORTED_TYPES = %w[Account Balance Category Tag Merchant ProviderMerchant RecurringTransaction RecurrenceRule RecurringOccurrence RecurringAllocation RecurringPriceChange RecurringMatchRejection Transaction Transfer RejectedTransfer Trade Holding Valuation Budget BudgetCategory Rule].freeze+ PROPERTY_DETAIL_ATTRIBUTES = %w[year_built area_value area_unit].freeze+ PROPERTY_ADDRESS_ATTRIBUTES = %w[line1 line2 locality region county postal_code country].freezeACCOUNTABLE_TYPE_CLASSES = {"Depository" => Depository, "Investment" => Investment, "Crypto" => Crypto,"Property" => Property, "Vehicle" => Vehicle, "OtherAsset" => OtherAsset,@@ -347,6 +349,8 @@ class Family::DataImporterendend+ assign_property_details(accountable, accountable_data) if accountable.is_a?(Property)+account = @family.accounts.build(accountable: accountable)end@@ -384,6 +388,16 @@ class Family::DataImporterendend+ # What the property form edits. Provider links (avm_*) are left out: the+ # restored family has to link its own provider. A missing or null value+ # keeps the model's default, such as the area unit.+ def assign_property_details(property, accountable_data)+ property.assign_attributes(accountable_data.slice(*PROPERTY_DETAIL_ATTRIBUTES).compact)++ address_data = accountable_data["address"]+ property.build_address(address_data.slice(*PROPERTY_ADDRESS_ATTRIBUTES)) if address_data.is_a?(Hash)+ end+def importable_account_status(status)status.to_s.in?(%w[active disabled draft]) ? status.to_s : "active"enddiff --git a/test/models/family/data_importer_test.rb b/test/models/family/data_importer_test.rbindex a106a447cc..f2fd5b3b95 100644--- a/test/models/family/data_importer_test.rb+++ b/test/models/family/data_importer_test.rb@@ -1507,6 +1507,89 @@ class Family::DataImporterTest < ActiveSupport::TestCaseassert imported_holding.security_lockedend+ test "round trips property details and address through full export" do+ source_family = Family.create!(name: "Source Property Family", currency: "USD", locale: "en", date_format: "%Y-%m-%d")+ source_family.accounts.create!(+ name: "Round Trip Land",+ balance: 150_000_000,+ currency: "PYG",+ accountable: Property.new(+ subtype: "plot",+ year_built: 2001,+ area_value: 12_000,+ area_unit: "sqm",+ address: Address.new(line1: "Ruta 2 km 40", locality: "Caacupe", region: "Cordillera", postal_code: "3000", country: "PY")+ )+ )++ zip_data = Family::DataExporter.new(source_family).generate_export+ ndjson = nil+ Zip::File.open_buffer(zip_data) { |zip| ndjson = zip.read("all.ndjson") }++ Family::DataImporter.new(@family, ndjson).import!++ property = @family.accounts.find_by!(name: "Round Trip Land").accountable+ assert_equal "plot", property.subtype+ assert_equal 2001, property.year_built+ assert_equal 12_000, property.area_value+ assert_equal "sqm", property.area_unit++ address = property.address+ assert_not_nil address, "the address should survive a backup and restore"+ assert_equal "Ruta 2 km 40", address.line1+ assert_equal "Caacupe", address.locality+ assert_equal "Cordillera", address.region+ assert_equal "3000", address.postal_code+ assert_equal "PY", address.country+ end++ test "imports property details from an export without an address" do+ ndjson = build_ndjson([+ {+ type: "Account",+ data: {+ id: "old-property-1",+ name: "Imported Land",+ balance: "1000",+ currency: "USD",+ accountable_type: "Property",+ accountable: { subtype: "agri_land", year_built: nil, area_value: 50, area_unit: "sqm", id: "ignored", avm_provider: "rentcast" }+ }+ }+ ])++ Family::DataImporter.new(@family, ndjson).import!++ property = @family.accounts.find_by!(name: "Imported Land").accountable+ assert_equal 50, property.area_value+ assert_equal "sqm", property.area_unit+ assert_nil property.year_built+ assert_nil property.avm_provider, "provider links are not restored"+ assert_nil property.address+ end++ test "an explicit null property detail keeps the default" do+ ndjson = build_ndjson([+ {+ type: "Account",+ data: {+ id: "old-property-2",+ name: "Null Unit Land",+ balance: "1000",+ currency: "USD",+ accountable_type: "Property",+ accountable: { subtype: "plot", area_value: 50, area_unit: nil }+ }+ }+ ])++ Family::DataImporter.new(@family, ndjson).import!++ property = @family.accounts.find_by!(name: "Null Unit Land").accountable+ assert_equal 50, property.area_value+ assert_equal "sqft", property.area_unit+ end+test "round trips raw balance history through full export" dosource_family = Family.create!(name: "Source Balance Family",