Add 'all_time' period option to Period model (#279)

* Add 'all_time' period option to Period model

Introduces an 'all_time' period to the Period model, which spans from the family's oldest entry date to the current date. Includes tests to verify correct creation and date range calculation for the new period.

* Update test/models/period_test.rb

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Mark Hendriksen <hendriksen-mark@hotmail.com>

* Improve 'all_time' period fallback logic

Updates the 'all_time' period to use a 5-year fallback range when no family or entries exist, or when the oldest entry date is today. Adds tests to verify correct behavior for these edge cases.

* Update period.rb

---------

Signed-off-by: Mark Hendriksen <hendriksen-mark@hotmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
This commit is contained in:
Mark Hendriksen
2025-11-10 23:07:07 +01:00
committed by GitHub
parent c6771ebaab
commit 7f5cf4c082
2 changed files with 54 additions and 0 deletions

View File

@@ -57,4 +57,42 @@ class PeriodTest < ActiveSupport::TestCase
expected = "#{start_date.strftime("%b %d, %Y")} to #{end_date.strftime("%b %d, %Y")}"
assert_equal expected, period.comparison_label
end
test "all_time period can be created" do
period = Period.from_key("all_time")
assert_equal "all_time", period.key
assert_equal "All Time", period.label
assert_equal "All", period.label_short
assert_equal "vs. beginning", period.comparison_label
end
test "all_time period uses family's oldest entry date" do
# Mock Current.family to return a family with oldest_entry_date
mock_family = mock("family")
mock_family.expects(:oldest_entry_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 Date.current, period.end_date
end
test "all_time period uses fallback when no family or entries exist" do
Current.expects(:family).returns(nil)
period = Period.from_key("all_time")
assert_equal 5.years.ago.to_date, period.start_date
assert_equal Date.current, period.end_date
end
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)
period = Period.from_key("all_time")
assert_equal 5.years.ago.to_date, period.start_date
assert_equal Date.current, period.end_date
end
end