1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 13:19:39 +02:00
Maybe/app/controllers/accounts_controller.rb

44 lines
1.1 KiB
Ruby
Raw Normal View History

2024-02-02 09:05:04 -06:00
class AccountsController < ApplicationController
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
def create
@account = Current.family.accounts.build(account_params)
2024-02-02 23:06:29 +00:00
@account.accountable = account_params[:accountable_type].constantize.new
if @account.save
2024-02-02 23:06:29 +00:00
redirect_to accounts_path, notice: "New account created successfully"
else
2024-02-02 23:06:29 +00:00
render "new", status: :unprocessable_entity
end
end
private
def account_params
2024-02-02 23:06:29 +00:00
params.require(:account).permit(:name, :accountable_type, :balance, :subtype)
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
end
2024-02-02 09:05:04 -06:00
end