mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 07:39:39 +02:00
An overhaul and cleanup of the transactions feature including: - Simplification of transactions search and filtering - Consolidation of account sync logic after transaction change - Split sidebar modal and modal into "drawer" and "modal" concepts - Refactor of transaction partials and folder organization - Cleanup turbo frames and streams for transaction updates, including new Transactions::RowsController for inline updates - Refactored and added several integration and systems tests
80 lines
2 KiB
Ruby
80 lines
2 KiB
Ruby
class TransactionsController < ApplicationController
|
|
layout "with_sidebar"
|
|
|
|
before_action :set_transaction, only: %i[ show edit update destroy ]
|
|
|
|
def index
|
|
@q = search_params
|
|
result = Current.family.transactions.search(@q).ordered
|
|
@pagy, @transactions = pagy(result, items: 50)
|
|
|
|
@totals = {
|
|
count: result.count,
|
|
income: result.inflows.sum(&:amount_money).abs,
|
|
expense: result.outflows.sum(&:amount_money).abs
|
|
}
|
|
end
|
|
|
|
def show
|
|
end
|
|
|
|
def new
|
|
@transaction = Transaction.new.tap do |txn|
|
|
if params[:account_id]
|
|
txn.account = Current.family.accounts.find(params[:account_id])
|
|
end
|
|
end
|
|
end
|
|
|
|
def edit
|
|
end
|
|
|
|
def create
|
|
@transaction = Current.family.accounts
|
|
.find(params[:transaction][:account_id])
|
|
.transactions.build(transaction_params.merge(amount: amount))
|
|
|
|
@transaction.save!
|
|
@transaction.sync_account_later
|
|
redirect_to transactions_url, notice: t(".success")
|
|
end
|
|
|
|
def update
|
|
@transaction.update! transaction_params
|
|
@transaction.sync_account_later
|
|
|
|
redirect_to transaction_url(@transaction), notice: t(".success")
|
|
end
|
|
|
|
def destroy
|
|
@transaction.destroy!
|
|
@transaction.sync_account_later
|
|
redirect_to transactions_url, notice: t(".success")
|
|
end
|
|
|
|
private
|
|
|
|
def set_transaction
|
|
@transaction = Current.family.transactions.find(params[:id])
|
|
end
|
|
|
|
def amount
|
|
if nature.income?
|
|
transaction_params[:amount].to_d * -1
|
|
else
|
|
transaction_params[:amount].to_d
|
|
end
|
|
end
|
|
|
|
def nature
|
|
params[:transaction][:nature].to_s.inquiry
|
|
end
|
|
|
|
def search_params
|
|
params.fetch(:q, {}).permit(:start_date, :end_date, :search, accounts: [], account_ids: [], categories: [], merchants: [])
|
|
end
|
|
|
|
def transaction_params
|
|
params.require(:transaction).permit(:name, :date, :amount, :currency, :notes, :excluded, :category_id, :merchant_id, tag_ids: [], taggings_attributes: [ :id, :tag_id, :_destroy ])
|
|
end
|
|
end
|