1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-21 14:19:39 +02:00
Maybe/app/controllers/accounts_controller.rb
Dwight Watson 1cded2af90
Use Current association in AccountsController and add tests (#298)
* Add PagesControllerTest with authentication

* Rubocop fixes

* Move sign_in to setup block

* Remove instance variable

* Add tests for AccountsController

* Use specific account
2024-02-04 18:00:40 -06: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 = Current.family.accounts.build(account_params)
@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