The personal finance app for everyone
Preference to set default_period (#1941)
Nikhil Badyal26762477a323
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rbindex df2b4d3c..5be606d2 100644--- a/app/controllers/accounts_controller.rb+++ b/app/controllers/accounts_controller.rb@@ -1,5 +1,6 @@class AccountsController < ApplicationControllerbefore_action :set_account, only: %i[sync chart sparkline]+ include Periodabledef index@manual_accounts = family.accounts.manual.alphabeticallydiff --git a/app/controllers/concerns/accountable_resource.rb b/app/controllers/concerns/accountable_resource.rbindex 16467e36..524be0fc 100644--- a/app/controllers/concerns/accountable_resource.rb+++ b/app/controllers/concerns/accountable_resource.rb@@ -2,7 +2,7 @@ module AccountableResourceextend ActiveSupport::Concernincluded do- include ScrollFocusable+ include ScrollFocusable, Periodablebefore_action :set_account, only: [ :show, :edit, :update, :destroy ]before_action :set_link_token, only: :newdiff --git a/app/controllers/concerns/periodable.rb b/app/controllers/concerns/periodable.rbnew file mode 100644index 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+enddiff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rbindex 34d6cca3..f2a91f62 100644--- a/app/controllers/pages_controller.rb+++ b/app/controllers/pages_controller.rb@@ -1,8 +1,8 @@class PagesController < ApplicationControllerskip_before_action :authenticate_user!, only: %i[early_access]+ include Periodabledef 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_logodiff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rbindex 9b82509d..4300477d 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -66,7 +66,7 @@ class UsersController < ApplicationControllerdef user_paramsparams.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 ])enddiff --git a/app/models/user.rb b/app/models/user.rbindex 24932861..479ce225 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -9,6 +9,7 @@ class User < ApplicationRecordvalidates :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.erbindex 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" %></div><div class="h-64 pb-4">diff --git a/app/views/accounts/show/_chart.html.erb b/app/views/accounts/show/_chart.html.erbindex d7427762..c78a3676 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 = params[:period] ? Period.from_key(params[:period]) : Period.last_30_days %>+<% period = @period || Period.last_30_days %><% default_value_title = account.asset? ? t(".balance") : t(".owed") %><div id="<%= dom_id(account, :chart) %>" class="bg-white shadow-xs rounded-xl border border-alpha-black-25 rounded-lg space-y-2">diff --git a/app/views/settings/preferences/show.html.erb b/app/views/settings/preferences/show.html.erbindex c51444dc..e053d969 100644--- a/app/views/settings/preferences/show.html.erb+++ b/app/views/settings/preferences/show.html.erb@@ -25,6 +25,11 @@{ label: t(".date_format") },{ data: { auto_submit_form_target: "auto" } } %>+ <%= form.select :default_period,+ Period.all.map { |period| [ period.label, period.key ] },+ { label: t(".default_period") },+ { data: { auto_submit_form_target: "auto" } } %>+<%= family_form.select :country,country_options,{ label: t(".country") },diff --git a/config/locales/views/settings/en.yml b/config/locales/views/settings/en.ymlindex 8596904e..8bc0dbf0 100644--- a/config/locales/views/settings/en.yml+++ b/config/locales/views/settings/en.yml@@ -20,6 +20,7 @@ en:date_format: Date formatgeneral_subtitle: Configure your preferencesgeneral_title: General+ default_period: Default Periodlanguage: Languagepage_title: Preferencestheme_dark: Darkdiff --git a/db/migrate/20250304140435_add_default_period_to_users.rb b/db/migrate/20250304140435_add_default_period_to_users.rbnew file mode 100644index 00000000..ee2f5e2f--- /dev/null+++ b/db/migrate/20250304140435_add_default_period_to_users.rb@@ -0,0 +1,5 @@+class AddDefaultPeriodToUsers < ActiveRecord::Migration[7.2]+ def change+ add_column :users, :default_period, :string, default: "last_30_days", null: false+ end+enddiff --git a/db/schema.rb b/db/schema.rbindex 492edb1e..59eabedf 100644--- a/db/schema.rb+++ b/db/schema.rb@@ -10,7 +10,7 @@## It's strongly recommended that you check this file into your version control system.-ActiveRecord::Schema[7.2].define(version: 2025_03_03_141007) do+ActiveRecord::Schema[7.2].define(version: 2025_03_04_140435) do# These are extensions that must be enabled in order to support this databaseenable_extension "pgcrypto"enable_extension "plpgsql"@@ -675,6 +675,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_03_141007) dot.boolean "otp_required", default: false, null: falset.string "otp_backup_codes", default: [], array: truet.boolean "show_sidebar", default: true+ t.string "default_period", default: "last_30_days", null: falset.index ["email"], name: "index_users_on_email", unique: truet.index ["family_id"], name: "index_users_on_family_id"t.index ["otp_secret"], name: "index_users_on_otp_secret", unique: true, where: "(otp_secret IS NOT NULL)"