2024-02-02 09:05:04 -06:00
|
|
|
class AccountsController < ApplicationController
|
2024-02-02 10:39:16 -06:00
|
|
|
before_action :authenticate_user!
|
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
def new
|
2024-02-02 23:06:29 +00:00
|
|
|
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]}")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
head :not_found
|
|
|
|
end
|
2024-02-02 15:31:32 -06:00
|
|
|
end
|
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
def show
|
|
|
|
end
|
2024-02-02 10:39:16 -06:00
|
|
|
|
|
|
|
def create
|
2024-02-05 10:36:46 +11:00
|
|
|
@account = Account.new(account_params.merge(family: Current.family))
|
2024-02-02 23:06:29 +00:00
|
|
|
@account.accountable = account_params[:accountable_type].constantize.new
|
2024-02-02 10:39:16 -06:00
|
|
|
|
|
|
|
if @account.save
|
2024-02-02 23:06:29 +00:00
|
|
|
redirect_to accounts_path, notice: "New account created successfully"
|
2024-02-02 10:39:16 -06:00
|
|
|
else
|
2024-02-02 23:06:29 +00:00
|
|
|
render "new", status: :unprocessable_entity
|
2024-02-02 10:39:16 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def account_params
|
2024-02-02 23:06:29 +00:00
|
|
|
params.require(:account).permit(:name, :accountable_type, :balance, :subtype)
|
2024-02-02 10:39:16 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def account_type_class
|
2024-02-02 21:33:43 +00:00
|
|
|
if params[:type].present? && Account.accountable_types.include?(params[:type])
|
2024-02-02 16:54:15 +00:00
|
|
|
params[:type].constantizes
|
|
|
|
else
|
|
|
|
Account # Default to Account if type is not provided or invalid
|
|
|
|
end
|
2024-02-02 10:39:16 -06:00
|
|
|
end
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|