Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Keep vehicle details through a backup and restore (#5313)

Francisco R. Santosc06c60920fbb
diff --git a/app/models/family/data_importer.rb b/app/models/family/data_importer.rb
index 48cba61537..2f68c39542 100644
--- a/app/models/family/data_importer.rb
+++ b/app/models/family/data_importer.rb
@@ -34,6 +34,7 @@ class Family::DataImporter
SUPPORTED_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].freeze
+ VEHICLE_DETAIL_ATTRIBUTES = %w[make model year mileage_value mileage_unit].freeze
ACCOUNTABLE_TYPE_CLASSES = {
"Depository" => Depository, "Investment" => Investment, "Crypto" => Crypto,
"Property" => Property, "Vehicle" => Vehicle, "OtherAsset" => OtherAsset,
@@ -360,6 +361,7 @@ class Family::DataImporter
end
assign_property_details(accountable, accountable_data) if accountable.is_a?(Property)
+ assign_vehicle_details(accountable, accountable_data) if accountable.is_a?(Vehicle)
account = @family.accounts.build(accountable: accountable)
end
@@ -410,6 +412,12 @@ class Family::DataImporter
property.build_address(address_data.slice(*PROPERTY_ADDRESS_ATTRIBUTES)) if address_data.is_a?(Hash)
end
+ # What the vehicle form edits. A missing or null value keeps the model's
+ # default, such as the mileage unit.
+ def assign_vehicle_details(vehicle, accountable_data)
+ vehicle.assign_attributes(accountable_data.slice(*VEHICLE_DETAIL_ATTRIBUTES).compact)
+ end
+
def importable_account_status(status)
status.to_s.in?(%w[active disabled draft]) ? status.to_s : "active"
end
diff --git a/test/models/family/data_importer_test.rb b/test/models/family/data_importer_test.rb
index f2e76841a6..942a81e6eb 100644
--- a/test/models/family/data_importer_test.rb
+++ b/test/models/family/data_importer_test.rb
@@ -102,6 +102,44 @@ class Family::DataImporterTest < ActiveSupport::TestCase
assert_equal Date.new(2029, 8, 31), card.expiration_date
end
+ test "round trips vehicle details through full export" do
+ source_family = Family.create!(name: "Source Vehicle Family", currency: "EUR", locale: "en", date_format: "%Y-%m-%d")
+ source_family.accounts.create!(
+ name: "Round Trip Car",
+ balance: 18_500,
+ currency: "EUR",
+ accountable: Vehicle.new(subtype: "car", make: "Toyota", model: "Camry", year: 2024, mileage_value: 15_000, mileage_unit: "km")
+ )
+
+ Family::DataImporter.new(@family, full_export_ndjson(source_family)).import!
+
+ vehicle = @family.accounts.find_by!(name: "Round Trip Car").accountable
+ assert_equal "car", vehicle.subtype
+ assert_equal "Toyota", vehicle.make
+ assert_equal "Camry", vehicle.model
+ assert_equal 2024, vehicle.year
+ assert_equal 15_000, vehicle.mileage_value
+ assert_equal "km", vehicle.mileage_unit
+ end
+
+ test "imports vehicles from an export without their details or with null details" do
+ ndjson = build_ndjson([
+ { type: "Account", data: { id: "old-car", name: "Old Car", balance: "5000", currency: "USD", accountable_type: "Vehicle", accountable: { subtype: "car" } } },
+ { type: "Account", data: { id: "null-car", name: "Null Car", balance: "5000", currency: "USD", accountable_type: "Vehicle",
+ accountable: { subtype: "car", make: nil, model: nil, year: nil, mileage_value: nil, mileage_unit: nil } } }
+ ])
+
+ Family::DataImporter.new(@family, ndjson).import!
+
+ [ "Old Car", "Null Car" ].each do |name|
+ vehicle = @family.accounts.find_by!(name: name).accountable
+ assert_nil vehicle.make
+ assert_nil vehicle.year
+ assert_nil vehicle.mileage_value
+ assert_equal "mi", vehicle.mileage_unit
+ end
+ end
+
test "imports loans and credit cards from an export without their details" do
ndjson = build_ndjson([
{ type: "Account", data: { id: "old-loan", name: "Old Loan", balance: "1000", currency: "USD", subtype: "auto", accountable_type: "Loan", accountable: { subtype: "auto" } } },