Skip to content
This repository was archived by the owner on Jul 27, 2025. It is now read-only.

Commit 527a612

Browse files
committed
Fix budget navigation to allow selecting previous months
- Allow going back 2 years minimum even without entries - Update oldest_valid_budget_date to use min of entry date or 2 years ago - Add comprehensive tests for budget date validation - Fixes issue where users couldn't select prior budget months
1 parent 32ec571 commit 527a612

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

app/models/budget.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ def find_or_bootstrap(family, start_date:)
4949

5050
private
5151
def oldest_valid_budget_date(family)
52-
@oldest_valid_budget_date ||= family.oldest_entry_date.beginning_of_month
52+
# Allow going back to either the earliest entry date OR 2 years ago, whichever is earlier
53+
two_years_ago = 2.years.ago.beginning_of_month
54+
oldest_entry_date = family.oldest_entry_date.beginning_of_month
55+
[ two_years_ago, oldest_entry_date ].min
5356
end
5457
end
5558

test/models/budget_test.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require "test_helper"
2+
3+
class BudgetTest < ActiveSupport::TestCase
4+
setup do
5+
@family = families(:empty)
6+
end
7+
8+
test "budget_date_valid? allows going back 2 years even without entries" do
9+
two_years_ago = 2.years.ago.beginning_of_month
10+
assert Budget.budget_date_valid?(two_years_ago, family: @family)
11+
end
12+
13+
test "budget_date_valid? allows going back to earliest entry date if more than 2 years ago" do
14+
# Create an entry 3 years ago
15+
old_account = Account.create!(
16+
family: @family,
17+
accountable: Depository.new,
18+
name: "Old Account",
19+
status: "active",
20+
currency: "USD",
21+
balance: 1000
22+
)
23+
24+
old_entry = Entry.create!(
25+
account: old_account,
26+
entryable: Transaction.new(category: categories(:income)),
27+
date: 3.years.ago,
28+
name: "Old Transaction",
29+
amount: 100,
30+
currency: "USD"
31+
)
32+
33+
# Should allow going back to the old entry date
34+
assert Budget.budget_date_valid?(3.years.ago.beginning_of_month, family: @family)
35+
end
36+
37+
test "budget_date_valid? does not allow dates before earliest entry or 2 years ago" do
38+
# Create an entry 1 year ago
39+
account = Account.create!(
40+
family: @family,
41+
accountable: Depository.new,
42+
name: "Test Account",
43+
status: "active",
44+
currency: "USD",
45+
balance: 500
46+
)
47+
48+
Entry.create!(
49+
account: account,
50+
entryable: Transaction.new(category: categories(:income)),
51+
date: 1.year.ago,
52+
name: "Recent Transaction",
53+
amount: 100,
54+
currency: "USD"
55+
)
56+
57+
# Should not allow going back more than 2 years
58+
refute Budget.budget_date_valid?(3.years.ago.beginning_of_month, family: @family)
59+
end
60+
61+
test "budget_date_valid? does not allow future dates beyond current month" do
62+
refute Budget.budget_date_valid?(2.months.from_now, family: @family)
63+
end
64+
65+
test "previous_budget_param returns nil when date is too old" do
66+
# Create a budget at the oldest allowed date
67+
two_years_ago = 2.years.ago.beginning_of_month
68+
budget = Budget.create!(
69+
family: @family,
70+
start_date: two_years_ago,
71+
end_date: two_years_ago.end_of_month,
72+
currency: "USD"
73+
)
74+
75+
assert_nil budget.previous_budget_param
76+
end
77+
78+
test "previous_budget_param returns param when date is valid" do
79+
budget = Budget.create!(
80+
family: @family,
81+
start_date: Date.current.beginning_of_month,
82+
end_date: Date.current.end_of_month,
83+
currency: "USD"
84+
)
85+
86+
assert_not_nil budget.previous_budget_param
87+
end
88+
end

0 commit comments

Comments
 (0)