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-09 14:26:54 +00:00
|
|
|
@account = Account.new(
|
2024-02-10 16:18:56 -06:00
|
|
|
original_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-14 13:02:11 -05:00
|
|
|
# Temporary while dummy data is being used
|
|
|
|
# @account = Current.family.accounts.find(params[:id])
|
|
|
|
@account = sample_account
|
2024-02-02 09:05:04 -06:00
|
|
|
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
|
|
|
|
|
2024-02-14 13:02:11 -05:00
|
|
|
|
|
|
|
|
2024-02-02 10:39:16 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def account_params
|
2024-02-10 16:18:56 -06:00
|
|
|
params.require(:account).permit(:name, :accountable_type, :original_balance, :original_currency, :subtype)
|
2024-02-02 10:39:16 -06:00
|
|
|
end
|
2024-02-14 13:02:11 -05:00
|
|
|
|
|
|
|
def sample_account
|
|
|
|
OpenStruct.new(
|
|
|
|
id: 1,
|
|
|
|
name: "Sample Account",
|
|
|
|
original_balance: BigDecimal("1115181"),
|
|
|
|
original_currency: "USD",
|
|
|
|
converted_balance: BigDecimal("1115181"), # Assuming conversion rate is 1 for simplicity
|
|
|
|
converted_currency: "USD",
|
|
|
|
dollar_change: BigDecimal("1553.43"), # Added dollar change
|
|
|
|
percent_change: BigDecimal("0.9"), # Added percent change
|
|
|
|
subtype: "Checking",
|
|
|
|
accountable_type: "Depository",
|
|
|
|
balances: sample_balances
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sample_balances
|
|
|
|
4.times.map do |i|
|
|
|
|
OpenStruct.new(
|
|
|
|
date: "Feb #{12 + i} 2024",
|
|
|
|
description: "Manually entered",
|
|
|
|
amount: BigDecimal("1000") + (i * BigDecimal("100")),
|
|
|
|
change: i == 3 ? -50 : (i == 2 ? 0 : 100 + (i * 10)),
|
|
|
|
percentage_change: i == 3 ? -5 : (i == 2 ? 0 : 10 + i),
|
|
|
|
icon: i == 3 ? "arrow-down" : (i == 2 ? "minus" : (i.even? ? "arrow-down" : "arrow-up"))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|