diff --git a/app/models/budget.rb b/app/models/budget.rb index f8cb991c..acdd128d 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -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 diff --git a/test/models/budget_test.rb b/test/models/budget_test.rb new file mode 100644 index 00000000..cff5cd6c --- /dev/null +++ b/test/models/budget_test.rb @@ -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