1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-20 21:59:38 +02:00
Maybe/app/controllers/accounts_controller.rb
2024-02-03 02:50:09 +00:00

43 lines
1.1 KiB
Ruby

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]}")
end
else
head :not_found
end
end
def show
end
def create
@account = Account.new(account_params.merge(family: current_family))
@account.accountable = account_params[:accountable_type].constantize.new
if @account.save
redirect_to accounts_path, notice: "New account created successfully"
else
render "new", status: :unprocessable_entity
end
end
private
def account_params
params.require(:account).permit(:name, :accountable_type, :balance, :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