The personal finance app for everyone (by everyone)
Ruby
fix: Savings Goal Marked Reached While Still Short (#2180)
Jonathan Chang8a38a82c9309
diff --git a/app/models/goal.rb b/app/models/goal.rbindex da4f597aff..67c4f6daee 100644--- a/app/models/goal.rb+++ b/app/models/goal.rb@@ -101,8 +101,10 @@ class Goal < ApplicationRecord100elsif target_amount.to_d.zero?0+ elsif remaining_amount.to_d.zero?+ 100else- [ ((current_balance.to_d / target_amount.to_d) * 100).round, 100 ].min+ ((current_balance.to_d / target_amount.to_d) * 100).floor.clamp(0, 99)endend@@ -238,14 +240,14 @@ class Goal < ApplicationRecordendend- # :reached → progress_percent >= 100+ # :reached → completed, or no remaining amount# :on_track → has target_date and pace >= required monthly# :behind → has target_date and pace < required monthly# :no_target_date → open-endeddef statusreturn @status if defined?(@status)- @status = if progress_percent >= 100+ @status = if completed? || remaining_amount.to_d.zero?:reachedelsif target_date.nil?:no_target_datediff --git a/test/models/goal_test.rb b/test/models/goal_test.rbindex 70529f1499..e7d5959fef 100644--- a/test/models/goal_test.rb+++ b/test/models/goal_test.rb@@ -103,6 +103,48 @@ class GoalTest < ActiveSupport::TestCaseassert_equal 100, @goal.progress_percentend+ test "progress_percent stays below 100 while remaining amount is positive" do+ account = Account.create!(+ family: @family,+ accountable: Depository.new,+ name: "Almost There Savings",+ currency: "USD",+ balance: BigDecimal("999.50")+ )++ goal = @family.goals.create!(+ name: "Almost There",+ target_amount: BigDecimal("1000"),+ currency: "USD"+ ) { |new_goal| new_goal.goal_accounts.build(account: account) }++ assert_equal BigDecimal("0.5"), goal.remaining_amount+ assert_equal 99, goal.progress_percent+ assert_equal :no_target_date, goal.status+ end++ test "status stays reached for a goal completed while underfunded" do+ account = Account.create!(+ family: @family,+ accountable: Depository.new,+ name: "Completed Underfunded Savings",+ currency: "USD",+ balance: BigDecimal("999.50")+ )++ goal = @family.goals.create!(+ name: "Completed Underfunded",+ target_amount: BigDecimal("1000"),+ currency: "USD"+ ) { |new_goal| new_goal.goal_accounts.build(account: account) }++ goal.complete!++ assert_equal BigDecimal("0.5"), goal.remaining_amount+ assert_equal 100, goal.progress_percent+ assert_equal :reached, Goal.find(goal.id).status+ end+test "progress_percent is 0 for empty active goal" dofresh = goals(:car_paydown)fresh.update!(target_amount: 10_000)