2025-01-16 14:36:37 -05:00
|
|
|
class BudgetCategoriesController < ApplicationController
|
2025-02-05 09:09:38 -05:00
|
|
|
before_action :set_budget
|
|
|
|
|
2025-01-16 14:36:37 -05:00
|
|
|
def index
|
2025-02-05 09:09:38 -05:00
|
|
|
@budget_categories = @budget.budget_categories.includes(:category)
|
2025-01-16 14:36:37 -05:00
|
|
|
render layout: "wizard"
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@recent_transactions = @budget.entries
|
|
|
|
|
|
|
|
if params[:id] == BudgetCategory.uncategorized.id
|
|
|
|
@budget_category = @budget.uncategorized_budget_category
|
|
|
|
@recent_transactions = @recent_transactions.where(account_transactions: { category_id: nil })
|
|
|
|
else
|
|
|
|
@budget_category = Current.family.budget_categories.find(params[:id])
|
|
|
|
@recent_transactions = @recent_transactions.joins("LEFT JOIN categories ON categories.id = account_transactions.category_id")
|
|
|
|
.where("categories.id = ? OR categories.parent_id = ?", @budget_category.category.id, @budget_category.category.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
@recent_transactions = @recent_transactions.order("account_entries.date DESC, ABS(account_entries.amount) DESC").take(3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@budget_category = Current.family.budget_categories.find(params[:id])
|
|
|
|
|
2025-02-05 09:09:38 -05:00
|
|
|
if @budget_category.update(budget_category_params)
|
|
|
|
respond_to do |format|
|
|
|
|
format.turbo_stream
|
|
|
|
format.html { redirect_to budget_budget_categories_path(@budget) }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render :index, status: :unprocessable_entity
|
|
|
|
end
|
2025-01-16 14:36:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def budget_category_params
|
2025-02-05 09:09:38 -05:00
|
|
|
params.require(:budget_category).permit(:budgeted_spending).tap do |params|
|
|
|
|
params[:budgeted_spending] = params[:budgeted_spending].presence || 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_budget
|
|
|
|
@budget = Current.family.budgets.find(params[:budget_id])
|
2025-01-16 14:36:37 -05:00
|
|
|
end
|
|
|
|
end
|