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('.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 %>