1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-30 10:39:40 +02:00

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
This commit is contained in:
Zach Gollwitzer 2025-07-23 18:26:04 -04:00
parent 32ec57146e
commit 527a6128b6
2 changed files with 92 additions and 1 deletions

View file

@ -49,7 +49,10 @@ class Budget < ApplicationRecord
private
def oldest_valid_budget_date(family)
@oldest_valid_budget_date ||= family.oldest_entry_date.beginning_of_month
# Allow going back to either the earliest entry date OR 2 years ago, whichever is earlier
two_years_ago = 2.years.ago.beginning_of_month
oldest_entry_date = family.oldest_entry_date.beginning_of_month
[ two_years_ago, oldest_entry_date ].min
end
end

View file

@ -0,0 +1,88 @@
require "test_helper"
class BudgetTest < ActiveSupport::TestCase
setup do
@family = families(:empty)
end
test "budget_date_valid? allows going back 2 years even without entries" do
two_years_ago = 2.years.ago.beginning_of_month
assert Budget.budget_date_valid?(two_years_ago, family: @family)
end
test "budget_date_valid? allows going back to earliest entry date if more than 2 years ago" do
# Create an entry 3 years ago
old_account = Account.create!(
family: @family,
accountable: Depository.new,
name: "Old Account",
status: "active",
currency: "USD",
balance: 1000
)
old_entry = Entry.create!(
account: old_account,
entryable: Transaction.new(category: categories(:income)),
date: 3.years.ago,
name: "Old Transaction",
amount: 100,
currency: "USD"
)
# Should allow going back to the old entry date
assert Budget.budget_date_valid?(3.years.ago.beginning_of_month, family: @family)
end
test "budget_date_valid? does not allow dates before earliest entry or 2 years ago" do
# Create an entry 1 year ago
account = Account.create!(
family: @family,
accountable: Depository.new,
name: "Test Account",
status: "active",
currency: "USD",
balance: 500
)
Entry.create!(
account: account,
entryable: Transaction.new(category: categories(:income)),
date: 1.year.ago,
name: "Recent Transaction",
amount: 100,
currency: "USD"
)
# Should not allow going back more than 2 years
refute Budget.budget_date_valid?(3.years.ago.beginning_of_month, family: @family)
end
test "budget_date_valid? does not allow future dates beyond current month" do
refute Budget.budget_date_valid?(2.months.from_now, family: @family)
end
test "previous_budget_param returns nil when date is too old" do
# Create a budget at the oldest allowed date
two_years_ago = 2.years.ago.beginning_of_month
budget = Budget.create!(
family: @family,
start_date: two_years_ago,
end_date: two_years_ago.end_of_month,
currency: "USD"
)
assert_nil budget.previous_budget_param
end
test "previous_budget_param returns param when date is valid" do
budget = Budget.create!(
family: @family,
start_date: Date.current.beginning_of_month,
end_date: Date.current.end_of_month,
currency: "USD"
)
assert_not_nil budget.previous_budget_param
end
end