2024-08-09 11:22:57 -04:00
|
|
|
class Account::TradesController < ApplicationController
|
|
|
|
layout :with_sidebar
|
|
|
|
|
|
|
|
before_action :set_account
|
|
|
|
|
|
|
|
def new
|
|
|
|
@entry = @account.entries.account_trades.new(entryable_attributes: {})
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
2024-08-23 10:06:24 -04:00
|
|
|
@entries = @account.entries.reverse_chronological.where(entryable_type: %w[Account::Trade Account::Transaction])
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2024-08-09 20:11:27 -04:00
|
|
|
@builder = Account::EntryBuilder.new(entry_params)
|
2024-08-09 11:22:57 -04:00
|
|
|
|
|
|
|
if entry = @builder.save
|
|
|
|
entry.sync_account_later
|
|
|
|
redirect_to account_path(@account), notice: t(".success")
|
|
|
|
else
|
2024-08-09 20:11:27 -04:00
|
|
|
flash[:alert] = t(".failure")
|
|
|
|
redirect_back_or_to account_path(@account)
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_account
|
|
|
|
@account = Current.family.accounts.find(params[:account_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def entry_params
|
2024-08-09 20:11:27 -04:00
|
|
|
params.require(:account_entry)
|
|
|
|
.permit(:type, :date, :qty, :ticker, :price, :amount, :currency, :transfer_account_id)
|
|
|
|
.merge(account: @account)
|
2024-08-09 11:22:57 -04:00
|
|
|
end
|
|
|
|
end
|