The personal finance app for everyone (by everyone)
Ruby
Start All period one month before first Transaction/Trade entry (#4068)
Febryand Nur Hidayat962d91f8847e
diff --git a/app/models/family.rb b/app/models/family.rbindex 7c5f9a4474..2ff947e93f 100644--- a/app/models/family.rb+++ b/app/models/family.rb@@ -638,6 +638,15 @@ class Family < ApplicationRecordentries.order(:date).first&.date || Date.currentend+ # Earliest Transaction/Trade entry date: the family's first real activity.+ # Unlike oldest_entry_date (any entry type; kept as-is for budgets and the+ # AI balance sheet), valuations and opening balances don't count here, so a+ # single old valuation can't stretch All-range charts back for years (#4007).+ # Returns nil when the family has no Transaction/Trade entries yet.+ def earliest_activity_date+ entries.where(entryable_type: %w[Transaction Trade]).minimum(:date)+ end+# Used for invalidating family / balance sheet related aggregation queriesdef build_cache_key(key, invalidate_on_data_updates: false)# Our data sync process updates this timestamp whenever any family account successfully completes a data update.diff --git a/app/models/period.rb b/app/models/period.rbindex 087e7bc715..17b0d26be1 100644--- a/app/models/period.rb+++ b/app/models/period.rb@@ -78,14 +78,19 @@ class Period},"all_time" => {date_range: -> {- oldest_date = Current.family&.oldest_entry_date- # If no family or no entries exist, use a reasonable historical fallback- # to ensure "All Time" represents a meaningful range, not just today- start_date = if oldest_date && oldest_date < Date.current- oldest_date- else- 5.years.ago.to_date- end+ # Anchor to one month before the first real activity (a Transaction or+ # Trade), not to the oldest entry of any kind -- a stray Valuation or+ # opening balance dated far in the past would otherwise stretch every+ # All chart into a long flat zero line (#4007). Only a family with no+ # transactions or trades falls back to oldest_entry_date, i.e. the+ # previous behaviour; a family that has activity never does, even when+ # that activity is today.+ family = Current.family+ earliest_activity_date = family&.earliest_activity_date+ start_date = earliest_activity_date ? earliest_activity_date - 1.month : family&.oldest_entry_date+ # The guard is unchanged: a start of today or later is not a meaningful+ # "All Time" range, so it degrades to the historical 5-year range.+ start_date = 5.years.ago.to_date unless start_date && start_date < Date.current[ start_date, Date.current ]},label_short: "All",diff --git a/test/models/family_test.rb b/test/models/family_test.rbindex ff18f971bb..057797cd5b 100644--- a/test/models/family_test.rb+++ b/test/models/family_test.rb@@ -665,6 +665,37 @@ class FamilyTest < ActiveSupport::TestCaseassert_raises(ArgumentError) { family.resolved_categorization_provider }end+ test "earliest_activity_date ignores valuations, using earliest Transaction/Trade entry" do+ family = families(:empty)+ account = family.accounts.create!(name: "Checking", balance: 0, currency: "USD", accountable: Depository.new)++ account.entries.create!(date: Date.new(2000, 1, 15), name: "Old valuation",+ amount: 5000, currency: "USD", entryable: Valuation.new)+ transaction_date = Date.new(2025, 3, 10)+ account.entries.create!(date: transaction_date, name: "Groceries",+ amount: -50, currency: "USD", entryable: Transaction.new)++ assert_equal transaction_date, family.earliest_activity_date++ # A Trade earlier than the Transaction must win, so a regression that+ # drops Trade from the filter is caught (#4007).+ trade_date = Date.new(2020, 6, 15)+ account.entries.create!(date: trade_date, name: "Investment purchase",+ amount: 100, currency: "USD",+ entryable: Trade.new(security: securities(:aapl), qty: 1, price: 100, currency: "USD"))++ assert_equal trade_date, family.earliest_activity_date+ end++ test "earliest_activity_date returns nil when family has no Transaction/Trade entries" do+ family = families(:empty)+ account = family.accounts.create!(name: "House", balance: 0, currency: "USD", accountable: Depository.new)+ account.entries.create!(date: Date.new(2000, 1, 15), name: "Old valuation",+ amount: 5000, currency: "USD", entryable: Valuation.new)++ assert_nil family.earliest_activity_date+ end+privatedef set_preview_features(user, enabled)user.update!(preferences: (user.preferences || {}).merge("preview_features_enabled" => enabled))diff --git a/test/models/period_test.rb b/test/models/period_test.rbindex ad4dd14229..ae41ed547a 100644--- a/test/models/period_test.rb+++ b/test/models/period_test.rb@@ -96,14 +96,14 @@ class PeriodTest < ActiveSupport::TestCaseassert_equal "All", period.label_shortend- test "all_time period uses family's oldest entry date" do- # Mock Current.family to return a family with oldest_entry_date+ test "all_time period uses family's earliest activity date, starting one month before" do+ # Mock Current.family to return a family with earliest_activity_datemock_family = mock("family")- mock_family.expects(:oldest_entry_date).returns(2.years.ago.to_date)+ mock_family.expects(:earliest_activity_date).returns(2.years.ago.to_date)Current.expects(:family).at_least_once.returns(mock_family)period = Period.from_key("all_time")- assert_equal 2.years.ago.to_date, period.start_date+ assert_equal 2.years.ago.to_date - 1.month, period.start_dateassert_equal Date.current, period.end_dateend@@ -138,14 +138,50 @@ class PeriodTest < ActiveSupport::TestCaseassert_equal Date.current, period.end_dateend- test "all_time period uses fallback when oldest_entry_date equals current date" do- # Mock a family that has no historical entries (oldest_entry_date returns today)- mock_family = mock("family")- mock_family.expects(:oldest_entry_date).returns(Date.current)- Current.expects(:family).at_least_once.returns(mock_family)+ test "all_time period anchors one month back when first activity is today" do+ # A family whose first Transaction is today still anchors to real activity+ # (one month back), not to the 5-year fallback (#4007).+ family = families(:empty)+ account = family.accounts.create!(name: "Checking", balance: 0, currency: "USD", accountable: Depository.new)+ account.entries.create!(date: Date.current, name: "First transaction",+ amount: -50, currency: "USD", entryable: Transaction.new)+ Current.stubs(:family).returns(family)period = Period.from_key("all_time")- assert_equal 5.years.ago.to_date, period.start_date++ assert_equal Date.current - 1.month, period.start_date+ assert_equal Date.current, period.end_date+ end++ test "all_time period ignores old valuations, starting one month before first transaction" do+ family = families(:empty)+ account = family.accounts.create!(name: "Checking", balance: 0, currency: "USD", accountable: Depository.new)+ account.entries.create!(date: Date.new(2000, 1, 15), name: "Old valuation",+ amount: 5000, currency: "USD", entryable: Valuation.new)+ first_transaction_date = Date.new(2025, 3, 10)+ account.entries.create!(date: first_transaction_date, name: "Groceries",+ amount: -50, currency: "USD", entryable: Transaction.new)++ Current.stubs(:family).returns(family)+ period = Period.from_key("all_time")++ assert_equal first_transaction_date - 1.month, period.start_date+ assert_equal Date.current, period.end_date+ end++ test "all_time period falls back to oldest entry date for valuation-only families" do+ # A family with only Valuations (no Transaction/Trade) keeps today's+ # behaviour: the range starts exactly at the oldest entry date (#4007).+ family = families(:empty)+ account = family.accounts.create!(name: "House", balance: 0, currency: "USD", accountable: Depository.new)+ valuation_date = Date.new(2000, 1, 15)+ account.entries.create!(date: valuation_date, name: "Old valuation",+ amount: 5000, currency: "USD", entryable: Valuation.new)++ Current.stubs(:family).returns(family)+ period = Period.from_key("all_time")++ assert_equal valuation_date, period.start_dateassert_equal Date.current, period.end_dateendend