2024-06-20 08:15:09 -04:00
|
|
|
class CategoriesController < ApplicationController
|
2024-10-24 11:02:27 -04:00
|
|
|
before_action :set_category, only: %i[edit update destroy]
|
2025-01-30 14:35:30 -03:00
|
|
|
before_action :set_categories, only: %i[update edit]
|
2024-05-02 07:24:31 -06:00
|
|
|
before_action :set_transaction, only: :create
|
2024-04-04 17:29:50 -04:00
|
|
|
|
2024-04-18 07:56:51 -04:00
|
|
|
def index
|
2024-06-20 08:15:09 -04:00
|
|
|
@categories = Current.family.categories.alphabetically
|
2025-02-21 11:57:59 -05:00
|
|
|
|
|
|
|
render layout: "settings"
|
2024-05-02 07:24:31 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2024-06-20 08:15:09 -04:00
|
|
|
@category = Current.family.categories.new color: Category::COLORS.sample
|
2025-01-30 14:35:30 -03:00
|
|
|
set_categories
|
2024-04-18 07:56:51 -04:00
|
|
|
end
|
|
|
|
|
2024-04-04 17:29:50 -04:00
|
|
|
def create
|
2024-10-24 11:02:27 -04:00
|
|
|
@category = Current.family.categories.new(category_params)
|
2024-05-02 07:24:31 -06:00
|
|
|
|
2024-10-24 11:02:27 -04:00
|
|
|
if @category.save
|
|
|
|
@transaction.update(category_id: @category.id) if @transaction
|
2024-12-20 11:37:26 -05:00
|
|
|
|
2025-01-16 14:36:37 -05:00
|
|
|
flash[:notice] = t(".success")
|
|
|
|
|
|
|
|
redirect_target_url = request.referer || categories_path
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_back_or_to categories_path, notice: t(".success") }
|
|
|
|
format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, redirect_target_url) }
|
|
|
|
end
|
2024-10-24 11:02:27 -04:00
|
|
|
else
|
2025-01-30 14:35:30 -03:00
|
|
|
set_categories
|
2024-12-20 11:37:26 -05:00
|
|
|
render :new, status: :unprocessable_entity
|
2024-10-24 11:02:27 -04:00
|
|
|
end
|
2024-04-04 17:29:50 -04:00
|
|
|
end
|
|
|
|
|
2024-05-02 07:24:31 -06:00
|
|
|
def edit
|
2024-04-04 17:29:50 -04:00
|
|
|
end
|
|
|
|
|
2024-05-02 07:24:31 -06:00
|
|
|
def update
|
2025-01-30 14:35:30 -03:00
|
|
|
if @category.update(category_params)
|
|
|
|
flash[:notice] = t(".success")
|
2024-05-02 07:24:31 -06:00
|
|
|
|
2025-01-30 14:35:30 -03:00
|
|
|
redirect_target_url = request.referer || categories_path
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_back_or_to categories_path, notice: t(".success") }
|
|
|
|
format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, redirect_target_url) }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render :edit, status: :unprocessable_entity
|
|
|
|
end
|
2024-04-04 17:29:50 -04:00
|
|
|
end
|
|
|
|
|
2024-10-24 11:02:27 -04:00
|
|
|
def destroy
|
|
|
|
@category.destroy
|
|
|
|
|
|
|
|
redirect_back_or_to categories_path, notice: t(".success")
|
|
|
|
end
|
|
|
|
|
2025-04-18 11:39:58 -04:00
|
|
|
def destroy_all
|
|
|
|
Current.family.categories.destroy_all
|
|
|
|
redirect_back_or_to categories_path, notice: "All categories deleted"
|
|
|
|
end
|
|
|
|
|
2024-12-20 11:37:26 -05:00
|
|
|
def bootstrap
|
2025-04-18 11:39:58 -04:00
|
|
|
Current.family.categories.bootstrap!
|
2024-12-20 11:37:26 -05:00
|
|
|
|
|
|
|
redirect_back_or_to categories_path, notice: t(".success")
|
|
|
|
end
|
|
|
|
|
2024-04-04 17:29:50 -04:00
|
|
|
private
|
2024-05-02 07:24:31 -06:00
|
|
|
def set_category
|
2024-06-20 08:15:09 -04:00
|
|
|
@category = Current.family.categories.find(params[:id])
|
2024-05-02 07:24:31 -06:00
|
|
|
end
|
2024-04-04 17:29:50 -04:00
|
|
|
|
2025-01-30 14:35:30 -03:00
|
|
|
def set_categories
|
|
|
|
@categories = unless @category.parent?
|
|
|
|
Current.family.categories.alphabetically.roots.where.not(id: @category.id)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-02 07:24:31 -06:00
|
|
|
def set_transaction
|
|
|
|
if params[:transaction_id].present?
|
|
|
|
@transaction = Current.family.transactions.find(params[:transaction_id])
|
|
|
|
end
|
|
|
|
end
|
2024-04-04 17:29:50 -04:00
|
|
|
|
2024-05-02 07:24:31 -06:00
|
|
|
def category_params
|
2025-01-16 14:36:37 -05:00
|
|
|
params.require(:category).permit(:name, :color, :parent_id, :classification, :lucide_icon)
|
2024-05-02 07:24:31 -06:00
|
|
|
end
|
2024-04-04 17:29:50 -04:00
|
|
|
end
|