2024-02-02 09:05:04 -06:00
|
|
|
class AccountsController < ApplicationController
|
2024-03-01 17:17:34 -05:00
|
|
|
include Filterable
|
2024-02-02 10:39:16 -06:00
|
|
|
before_action :authenticate_user!
|
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
def new
|
2024-02-09 14:26:54 +00:00
|
|
|
@account = Account.new(
|
2024-02-27 12:43:49 -05:00
|
|
|
balance: nil,
|
2024-02-09 14:26:54 +00:00
|
|
|
accountable: Accountable.from_type(params[:type])&.new
|
|
|
|
)
|
2024-02-02 15:31:32 -06:00
|
|
|
end
|
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
def show
|
2024-02-20 09:07:55 -05:00
|
|
|
@account = Current.family.accounts.find(params[:id])
|
2024-03-19 09:10:40 -04:00
|
|
|
@balance_series = @account.series(@period)
|
|
|
|
@valuation_series = @account.valuations.to_series
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|
2024-02-02 10:39:16 -06:00
|
|
|
|
2024-03-07 10:55:51 -05:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@account = Current.family.accounts.find(params[:id])
|
|
|
|
|
|
|
|
if @account.update(account_params.except(:accountable_type))
|
|
|
|
|
|
|
|
@account.sync_later if account_params[:is_active] == "1"
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to accounts_path, notice: t(".success") }
|
|
|
|
format.turbo_stream do
|
|
|
|
render turbo_stream: [
|
|
|
|
turbo_stream.append("notification-tray", partial: "shared/notification", locals: { type: "success", content: t(".success") }),
|
|
|
|
turbo_stream.replace("account_#{@account.id}", partial: "accounts/account", locals: { account: @account })
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render "edit", status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-02 10:39:16 -06:00
|
|
|
def create
|
2024-02-09 14:26:54 +00:00
|
|
|
@account = Current.family.accounts.build(account_params.except(:accountable_type))
|
|
|
|
@account.accountable = Accountable.from_type(account_params[:accountable_type])&.new
|
2024-02-02 10:39:16 -06:00
|
|
|
|
|
|
|
if @account.save
|
2024-02-06 14:58:17 -03:00
|
|
|
redirect_to accounts_path, notice: t(".success")
|
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-03-07 10:55:51 -05:00
|
|
|
params.require(:account).permit(:name, :accountable_type, :balance, :currency, :subtype, :is_active)
|
2024-02-02 10:39:16 -06:00
|
|
|
end
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|