From 8c1a7af37f80f6f018b0161dcd26b4e14f478c8a Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Thu, 13 Jun 2024 09:16:00 -0400 Subject: [PATCH] Allow for optional start date on account creation (#866) --- app/controllers/accounts_controller.rb | 24 ++++++++------ app/models/account.rb | 16 +++++++++ app/views/accounts/new.html.erb | 33 +++++++++++++----- config/locales/views/accounts/en.yml | 8 ++--- test/controllers/accounts_controller_test.rb | 35 ++++++++++++++------ 5 files changed, 83 insertions(+), 33 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index ad2cd8b7..9fabaebc 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -3,6 +3,7 @@ class AccountsController < ApplicationController include Filterable before_action :set_account, only: %i[ show destroy sync update ] + after_action :sync_account, only: :create def index @accounts = Current.family.accounts @@ -38,17 +39,14 @@ class AccountsController < ApplicationController end def create - @account = Current.family.accounts.build(account_params.except(:accountable_type, :start_date)) - @account.accountable = Accountable.from_type(account_params[:accountable_type])&.new + @account = Current.family + .accounts + .create_with_optional_start_balance! \ + attributes: account_params.except(:start_date, :start_balance), + start_date: account_params[:start_date], + start_balance: account_params[:start_balance] - if @account.save - @valuation = @account.valuations.new(date: account_params[:start_date] || Date.today, value: @account.balance, currency: @account.currency) - @valuation.save! - - redirect_to account_path(@account), notice: t(".success") - else - render "new", status: :unprocessable_entity - end + redirect_to account_path(@account), notice: t(".success") end def destroy @@ -82,6 +80,10 @@ class AccountsController < ApplicationController end def account_params - params.require(:account).permit(:name, :accountable_type, :balance, :start_date, :currency, :subtype, :is_active) + params.require(:account).permit(:name, :accountable_type, :balance, :start_date, :start_balance, :currency, :subtype, :is_active) + end + + def sync_account + @account.sync_later end end diff --git a/app/models/account.rb b/app/models/account.rb index c77f498f..a41e9b3f 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -80,4 +80,20 @@ class Account < ApplicationRecord grouped_accounts end + + def self.create_with_optional_start_balance!(attributes:, start_date: nil, start_balance: nil) + account = self.new(attributes.except(:accountable_type)) + account.accountable = Accountable.from_type(attributes[:accountable_type])&.new + + # Always build the initial valuation + account.valuations.build(date: Date.current, value: attributes[:balance], currency: account.currency) + + # Conditionally build the optional start valuation + if start_date.present? && start_balance.present? + account.valuations.build(date: start_date, value: start_balance, currency: account.currency) + end + + account.save! + account + end end diff --git a/app/views/accounts/new.html.erb b/app/views/accounts/new.html.erb index 48ecb650..c0518c00 100644 --- a/app/views/accounts/new.html.erb +++ b/app/views/accounts/new.html.erb @@ -20,14 +20,18 @@
- Select <%= lucide_icon("corner-down-left", class: "inline w-3 h-3") %> + Select + <%= lucide_icon("corner-down-left", class: "inline w-3 h-3") %>
- Navigate <%= lucide_icon("arrow-up", class: "inline w-3 h-3") %> <%= lucide_icon("arrow-down", class: "inline w-3 h-3") %> + Navigate + <%= lucide_icon("arrow-up", class: "inline w-3 h-3") %> + <%= lucide_icon("arrow-down", class: "inline w-3 h-3") %>
- ESC + + ESC
<% elsif params[:step] == 'method' && @account.accountable.present? %> @@ -47,14 +51,18 @@
- Select <%= lucide_icon("corner-down-left", class: "inline w-3 h-3") %> + Select + <%= lucide_icon("corner-down-left", class: "inline w-3 h-3") %>
- Navigate <%= lucide_icon("arrow-up", class: "inline w-3 h-3") %> <%= lucide_icon("arrow-down", class: "inline w-3 h-3") %> + Navigate + <%= lucide_icon("arrow-up", class: "inline w-3 h-3") %> + <%= lucide_icon("arrow-down", class: "inline w-3 h-3") %>
- ESC + + ESC
<% else %> @@ -69,8 +77,17 @@ <%= f.hidden_field :accountable_type %> <%= f.text_field :name, placeholder: t(".name.placeholder"), required: "required", label: t(".name.label"), autofocus: true %> <%= render "accounts/#{permitted_accountable_partial(@account.accountable_type)}", f: f %> - <%= f.money_field :balance_money, label: t(".balance.label"), required: "required" %> - <%= f.date_field :start_date, label: t(".start_date.label"), required: true, max: Date.today, value: Date.today %> + <%= f.money_field :balance_money, label: t(".balance"), required: "required" %> + +
+ <%= check_box_tag :add_start_values, class: "maybe-checkbox maybe-checkbox--light peer mb-1" %> + <%= label_tag :add_start_values, t(".optional_start_balance_message"), class: "pl-1 text-sm text-gray-500" %> + + +
<%= f.submit "Add #{@account.accountable.model_name.human.downcase}" %> <% end %> diff --git a/config/locales/views/accounts/en.yml b/config/locales/views/accounts/en.yml index 9728b7fc..1c527b8b 100644 --- a/config/locales/views/accounts/en.yml +++ b/config/locales/views/accounts/en.yml @@ -19,17 +19,17 @@ en: index: new_account: New account new: - balance: - label: Balance + balance: Current balance currency: all_others: All Others popular: Popular name: label: Account name placeholder: Example account name + optional_start_balance_message: Add a start balance for this account select_accountable_type: What would you like to add? - start_date: - label: Start date + start_balance: Start balance (optional) + start_date: Start date (optional) title: Add an account show: confirm_accept: Delete "%{name}" diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb index 9173e904..398e5d2f 100644 --- a/test/controllers/accounts_controller_test.rb +++ b/test/controllers/accounts_controller_test.rb @@ -27,20 +27,35 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest assert_equal "Account updated", flash[:notice] end - test "should create account" do - assert_difference -> { Account.count }, +1 do - post accounts_path, params: { account: { accountable_type: "Account::Credit" } } + test "should create an account" do + assert_difference [ "Account.count", "Valuation.count" ], 1 do + post accounts_path, params: { + account: { + accountable_type: "Account::Depository", + balance: 200, + subtype: "checking" + } + } + + assert_equal "New account created successfully", flash[:notice] assert_redirected_to account_url(Account.order(:created_at).last) end end - test "should create a valuation together with account" do - balance = 700 - start_date = 3.days.ago.to_date - post accounts_path, params: { account: { accountable_type: "Account::Credit", balance:, start_date: } } + test "can add optional start date and balance to an account on create" do + assert_difference -> { Account.count } => 1, -> { Valuation.count } => 2 do + post accounts_path, params: { + account: { + accountable_type: "Account::Depository", + balance: 200, + subtype: "checking", + start_balance: 100, + start_date: 10.days.ago + } + } - new_valuation = Valuation.order(:created_at).last - assert new_valuation.value == balance - assert new_valuation.date == start_date + assert_equal "New account created successfully", flash[:notice] + assert_redirected_to account_url(Account.order(:created_at).last) + end end end