Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Fix two crashes in `rake demo_data:default` (#2586)

Anthonye38d552c15f3
diff --git a/app/models/demo/data_cleaner.rb b/app/models/demo/data_cleaner.rb
index 726ceb9e84..829411db23 100644
--- a/app/models/demo/data_cleaner.rb
+++ b/app/models/demo/data_cleaner.rb
@@ -11,6 +11,15 @@ class Demo::DataCleaner
# Clear SSO audit logs first (they reference users)
SsoAuditLog.destroy_all
+ # ApiKey#prevent_demo_monitoring_key_destroy! throws :abort to stop the demo
+ # monitoring key being revoked from the UI. That abort silently no-ops the
+ # entire Family.destroy_all cascade below (accounts/entries/trades all
+ # survive), which only then surfaces as a NOT NULL crash in
+ # `Security.destroy_all`. Safe to bypass here: this class only runs in
+ # dev/test (see #ensure_safe_environment!).
+
+ ApiKey.where(display_key: ApiKey::DEMO_MONITORING_KEY).delete_all
+
Family.destroy_all
Setting.destroy_all
InviteCode.destroy_all
diff --git a/app/models/demo/generator.rb b/app/models/demo/generator.rb
index e047424cce..417028b94c 100644
--- a/app/models/demo/generator.rb
+++ b/app/models/demo/generator.rb
@@ -871,16 +871,24 @@ class Demo::Generator
diff_amex = amex_balance - target_amex
diff_sapphire = sapphire_balance - target_sapphire
- if diff_amex.abs > 250
- adjust_payment = diff_amex.positive? ? diff_amex : 0
- create_transfer!(@chase_checking, @amex_gold, adjust_payment, "Amex Balance Adjust", Date.current)
- amex_balance -= adjust_payment
+ if diff_amex > 250
+ create_transfer!(@chase_checking, @amex_gold, diff_amex, "Amex Balance Adjust", Date.current)
+ amex_balance -= diff_amex
+ elsif diff_amex < -250
+ # Balance landed below target: a transfer can't have a negative payment
+ # amount, so bring the card up to target with a direct charge instead.
+ shortfall = diff_amex.abs
+ create_transaction!(@amex_gold, shortfall, "Balance Reconciliation", random_expense_category, Date.current)
+ amex_balance += shortfall
end
- if diff_sapphire.abs > 250
- adjust_payment = diff_sapphire.positive? ? diff_sapphire : 0
- create_transfer!(@chase_checking, @chase_sapphire, adjust_payment, "Sapphire Balance Adjust", Date.current)
- sapphire_balance -= adjust_payment
+ if diff_sapphire > 250
+ create_transfer!(@chase_checking, @chase_sapphire, diff_sapphire, "Sapphire Balance Adjust", Date.current)
+ sapphire_balance -= diff_sapphire
+ elsif diff_sapphire < -250
+ shortfall = diff_sapphire.abs
+ create_transaction!(@chase_sapphire, shortfall, "Balance Reconciliation", random_expense_category, Date.current)
+ sapphire_balance += shortfall
end
puts " 💳 Charges generated: #{charges_this_run} | Payments: #{payments_this_run}"