diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 0b2b26b4..34d6cca3 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -2,7 +2,7 @@ class PagesController < ApplicationController skip_before_action :authenticate_user!, only: %i[early_access] def dashboard - @period = Period.from_key(params[:period], fallback: true) + @period = params[:period] ? Period.from_key(params[:period]) : Period.last_30_days @balance_sheet = Current.family.balance_sheet @accounts = Current.family.accounts.active.with_attached_logo diff --git a/app/models/budget.rb b/app/models/budget.rb index 66a4acb7..c2393da4 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -54,7 +54,7 @@ class Budget < ApplicationRecord end def period - Period.new(start_date: start_date, end_date: end_date) + Period.custom(start_date: start_date, end_date: end_date) end def to_param diff --git a/app/models/period.rb b/app/models/period.rb index 65825058..7e1a8008 100644 --- a/app/models/period.rb +++ b/app/models/period.rb @@ -1,9 +1,12 @@ class Period include ActiveModel::Validations, Comparable - attr_reader :start_date, :end_date + class InvalidKeyError < StandardError; end - validates :start_date, :end_date, presence: true + attr_reader :key, :start_date, :end_date + + validates :start_date, :end_date, presence: true, if: -> { PERIODS[key].nil? } + validates :key, presence: true, if: -> { start_date.nil? || end_date.nil? } validate :must_be_valid_date_range PERIODS = { @@ -64,18 +67,18 @@ class Period } class << self - def default - from_key("last_30_days") + def from_key(key) + unless PERIODS.key?(key) + raise InvalidKeyError, "Invalid period key: #{key}" + end + + start_date, end_date = PERIODS[key].fetch(:date_range) + + new(key: key, start_date: start_date, end_date: end_date) end - def from_key(key, fallback: false) - if PERIODS[key].present? - start_date, end_date = PERIODS[key].fetch(:date_range) - new(start_date: start_date, end_date: end_date) - else - return default if fallback - raise ArgumentError, "Invalid period key: #{key}" - end + def custom(start_date:, end_date:) + new(start_date: start_date, end_date: end_date) end def all @@ -85,12 +88,12 @@ class Period PERIODS.each do |key, period| define_singleton_method(key) do - start_date, end_date = period.fetch(:date_range) - new(start_date: start_date, end_date: end_date) + from_key(key) end end - def initialize(start_date:, end_date:, date_format: "%b %d, %Y") + def initialize(start_date: nil, end_date: nil, key: nil, date_format: "%b %d, %Y") + @key = key @start_date = start_date @end_date = end_date @date_format = date_format @@ -121,37 +124,33 @@ class Period end end - def key - PERIODS.find { |_, period| period.fetch(:date_range) == [ start_date, end_date ] }&.first - end - def label - if known? - PERIODS[key].fetch(:label) + if key_metadata + key_metadata.fetch(:label) else "Custom Period" end end def label_short - if known? - PERIODS[key].fetch(:label_short) + if key_metadata + key_metadata.fetch(:label_short) else - "CP" + "Custom" end end def comparison_label - if known? - PERIODS[key].fetch(:comparison_label) + if key_metadata + key_metadata.fetch(:comparison_label) else "#{start_date.strftime(@date_format)} to #{end_date.strftime(@date_format)}" end end private - def known? - key.present? + def key_metadata + @key_metadata ||= PERIODS[key] end def must_be_valid_date_range diff --git a/app/views/accounts/chart.html.erb b/app/views/accounts/chart.html.erb index 467fd35f..c5cc51eb 100644 --- a/app/views/accounts/chart.html.erb +++ b/app/views/accounts/chart.html.erb @@ -1,4 +1,4 @@ -<% period = Period.from_key(params[:period], fallback: true) %> +<% period = params[:period] ? Period.from_key(params[:period]) : Period.last_30_days %> <% series = @account.balance_series(period: period) %> <% trend = series.trend %> diff --git a/app/views/accounts/show/_chart.html.erb b/app/views/accounts/show/_chart.html.erb index 2b0ee941..d7427762 100644 --- a/app/views/accounts/show/_chart.html.erb +++ b/app/views/accounts/show/_chart.html.erb @@ -1,6 +1,6 @@ <%# locals: (account:, title: nil, tooltip: nil, **args) %> -<% period = Period.from_key(params[:period], fallback: true) %> +<% period = params[:period] ? Period.from_key(params[:period]) : Period.last_30_days %> <% default_value_title = account.asset? ? t(".balance") : t(".owed") %>