From f8174990ca29b554d13cce7fc032417f9e9dbc24 Mon Sep 17 00:00:00 2001 From: Josh Brown Date: Fri, 9 Feb 2024 14:26:54 +0000 Subject: [PATCH] Fix accountable type references in new account flow (#395) * Fix accountable type references in new account flow * Add remaining references * Refactor accountable type checking * Refactor new account template to use account * Refactor account create action * Refactor account sidebar --- app/controllers/accounts_controller.rb | 25 ++++++------------------- app/models/account.rb | 2 +- app/models/concerns/accountable.rb | 11 +++++++++++ app/views/accounts/new.html.erb | 14 +++++++------- app/views/layouts/application.html.erb | 4 ++-- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index e54c2439..6fe6e98f 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -2,23 +2,18 @@ class AccountsController < ApplicationController before_action :authenticate_user! def new - if params[:type].blank? || Account.accountable_types.include?("Account::#{params[:type]}") - @account = if params[:type].blank? - Account.new - else - Account.new(accountable_type: "Account::#{params[:type]}", balance: nil) - end - else - head :not_found - end + @account = Account.new( + balance: nil, + accountable: Accountable.from_type(params[:type])&.new + ) end def show end def create - @account = Current.family.accounts.build(account_params) - @account.accountable = account_params[:accountable_type].constantize.new + @account = Current.family.accounts.build(account_params.except(:accountable_type)) + @account.accountable = Accountable.from_type(account_params[:accountable_type])&.new if @account.save redirect_to accounts_path, notice: t(".success") @@ -32,12 +27,4 @@ class AccountsController < ApplicationController def account_params params.require(:account).permit(:name, :accountable_type, :balance, :balance_cents, :subtype) end - - def account_type_class - if params[:type].present? && Account.accountable_types.include?(params[:type]) - params[:type].constantizes - else - Account # Default to Account if type is not provided or invalid - end - end end diff --git a/app/models/account.rb b/app/models/account.rb index f5140ab1..2159c192 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,7 +1,7 @@ class Account < ApplicationRecord belongs_to :family - delegated_type :accountable, types: %w[ Account::Credit Account::Depository Account::Investment Account::Loan Account::OtherAsset Account::OtherLiability Account::Property Account::Vehicle], dependent: :destroy + delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy delegate :type_name, to: :accountable diff --git a/app/models/concerns/accountable.rb b/app/models/concerns/accountable.rb index 11ec8f24..e23d856d 100644 --- a/app/models/concerns/accountable.rb +++ b/app/models/concerns/accountable.rb @@ -1,6 +1,17 @@ module Accountable extend ActiveSupport::Concern + TYPES = %w[ Account::Credit Account::Depository Account::Investment Account::Loan Account::OtherAsset Account::OtherLiability Account::Property Account::Vehicle ] + + def self.from_type(type) + return nil unless types.include?(type) || TYPES.include?(type) + "Account::#{type.demodulize}".constantize + end + + def self.types + TYPES.map { |type| type.demodulize } + end + included do has_one :account, as: :accountable, touch: true end diff --git a/app/views/accounts/new.html.erb b/app/views/accounts/new.html.erb index ea60252f..b30c96f3 100644 --- a/app/views/accounts/new.html.erb +++ b/app/views/accounts/new.html.erb @@ -1,7 +1,7 @@

<%= t('.title')%>

<%= modal do %> - <% if params[:type].blank? || Account.accountable_types.include?("Account::#{params[:type]}") == false %> + <% if @account.accountable.blank? %>
<%= t '.select_accountable_type' %>
@@ -31,7 +31,7 @@ Close ESC - <% elsif params[:step] == 'method' && params[:type].present? %> + <% elsif params[:step] == 'method' && @account.accountable.present? %>
<%= link_to new_account_path, class: "flex w-8 h-8 shrink-0 grow-0 items-center justify-center rounded-lg bg-[#141414]/5" do %> <%= lucide_icon('arrow-left', class: 'text-gray-500 w-5 h-5') %> @@ -42,9 +42,9 @@ - <%= render "entry_method", type: Account::Depository.new, text: 'Enter account balance manually', icon: "keyboard" %> - <%= render "entry_method", type: Account::Depository.new, text: 'Securely link bank account with data provider (coming soon)', icon: "link-2", disabled: true %> - <%= render "entry_method", type: Account::Depository.new, text: 'Upload spreadsheet (coming soon)', icon: "sheet", disabled: true %> + <%= render "entry_method", type: @account.accountable, text: 'Enter account balance manually', icon: "keyboard" %> + <%= render "entry_method", type: @account.accountable, text: 'Securely link bank account with data provider (coming soon)', icon: "link-2", disabled: true %> + <%= render "entry_method", type: @account.accountable, text: 'Upload spreadsheet (coming soon)', icon: "sheet", disabled: true %>
@@ -64,7 +64,7 @@ <%= link_to new_account_path(step: 'method', type: params[:type]), class: "flex w-8 h-8 shrink-0 grow-0 items-center justify-center rounded-lg bg-[#141414]/5" do %> <%= lucide_icon('arrow-left', class: 'text-gray-500 w-5 h-5') %> <% end %> - Add account + Add <%= @account.accountable.model_name.human.downcase %>
<%= form_with model: @account, url: accounts_path, scope: :account, html: { class: "space-y-4 m-5 mt-1", data: { turbo: false } } do |f| %> @@ -86,7 +86,7 @@
<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 59dc4c63..dd0873de 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -70,8 +70,8 @@

<%= t('.new_account') %>

<% end %> - <% Account.accountable_types.each do |type| %> - <%= render 'accounts/account_list', type: type.constantize %> + <% Accountable.types.each do |type| %> + <%= render 'accounts/account_list', type: Accountable.from_type(type) %> <% end %>