From 26762477a323b63c46f4c7064e1ed361d781e320 Mon Sep 17 00:00:00 2001 From: Nikhil Badyal <59223300+nikhilbadyal@users.noreply.github.com> Date: Fri, 7 Mar 2025 20:35:54 +0530 Subject: [PATCH] Preference to set default_period (#1941) --- app/controllers/accounts_controller.rb | 1 + app/controllers/concerns/accountable_resource.rb | 2 +- app/controllers/concerns/periodable.rb | 14 ++++++++++++++ app/controllers/pages_controller.rb | 2 +- app/controllers/users_controller.rb | 2 +- app/models/user.rb | 1 + app/views/accounts/chart.html.erb | 5 ++--- app/views/accounts/show/_chart.html.erb | 2 +- app/views/settings/preferences/show.html.erb | 5 +++++ config/locales/views/settings/en.yml | 1 + .../20250304140435_add_default_period_to_users.rb | 5 +++++ db/schema.rb | 3 ++- 12 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 app/controllers/concerns/periodable.rb create mode 100644 db/migrate/20250304140435_add_default_period_to_users.rb diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index df2b4d3c..5be606d2 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -1,5 +1,6 @@ class AccountsController < ApplicationController before_action :set_account, only: %i[sync chart sparkline] + include Periodable def index @manual_accounts = family.accounts.manual.alphabetically diff --git a/app/controllers/concerns/accountable_resource.rb b/app/controllers/concerns/accountable_resource.rb index 16467e36..524be0fc 100644 --- a/app/controllers/concerns/accountable_resource.rb +++ b/app/controllers/concerns/accountable_resource.rb @@ -2,7 +2,7 @@ module AccountableResource extend ActiveSupport::Concern included do - include ScrollFocusable + include ScrollFocusable, Periodable before_action :set_account, only: [ :show, :edit, :update, :destroy ] before_action :set_link_token, only: :new diff --git a/app/controllers/concerns/periodable.rb b/app/controllers/concerns/periodable.rb new file mode 100644 index 00000000..8cf02395 --- /dev/null +++ b/app/controllers/concerns/periodable.rb @@ -0,0 +1,14 @@ +module Periodable + extend ActiveSupport::Concern + + included do + before_action :set_period + end + + private + def set_period + @period = Period.from_key(params[:period] || Current.user&.default_period) + rescue Period::InvalidKeyError + @period = Period.last_30_days + end +end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 34d6cca3..f2a91f62 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -1,8 +1,8 @@ class PagesController < ApplicationController skip_before_action :authenticate_user!, only: %i[early_access] + include Periodable def dashboard - @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/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9b82509d..4300477d 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -66,7 +66,7 @@ class UsersController < ApplicationController def user_params params.require(:user).permit( - :first_name, :last_name, :email, :profile_image, :redirect_to, :delete_profile_image, :onboarded_at, :show_sidebar, + :first_name, :last_name, :email, :profile_image, :redirect_to, :delete_profile_image, :onboarded_at, :show_sidebar, :default_period, family_attributes: [ :name, :currency, :country, :locale, :date_format, :timezone, :id, :data_enrichment_enabled ] ) end diff --git a/app/models/user.rb b/app/models/user.rb index 24932861..479ce225 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,6 +9,7 @@ class User < ApplicationRecord validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP } validate :ensure_valid_profile_image + validates :default_period, inclusion: { in: Period::PERIODS.keys } normalizes :email, with: ->(email) { email.strip.downcase } normalizes :unconfirmed_email, with: ->(email) { email&.strip&.downcase } diff --git a/app/views/accounts/chart.html.erb b/app/views/accounts/chart.html.erb index c5cc51eb..22e2528e 100644 --- a/app/views/accounts/chart.html.erb +++ b/app/views/accounts/chart.html.erb @@ -1,5 +1,4 @@ -<% period = params[:period] ? Period.from_key(params[:period]) : Period.last_30_days %> -<% series = @account.balance_series(period: period) %> +<% series = @account.balance_series(period: @period) %> <% trend = series.trend %> <%= turbo_frame_tag dom_id(@account, :chart_details) do %> @@ -13,7 +12,7 @@ <% end %> <% end %> - <%= tag.span period.comparison_label, class: "text-secondary" %> + <%= tag.span @period.comparison_label, class: "text-secondary" %>