1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-21 06:09:38 +02:00
Maybe/app/controllers/categories_controller.rb
Elvis Serrão 0b17976256
Don't allow a subcategory to be assigned to another subcategory to ensure 1 level of nesting max (#1730)
* Improve category level limit validation

* Set categories list only for non parents

* Disable select field

* Add info about the disabled select

* Don’t render a select input for parent categories

* Handle correctly turbo_stream request format

* Add turbo_stream format to requests on create and update action's tests

* Remove no_content status from update action

* Revert "Remove no_content status from update action"

This reverts commit 866140c196.

* Revert "Add turbo_stream format to requests on create and update action's tests"

This reverts commit c6bf21490f.

* Add correct redirect url for both html and turbo_stream formats

* Remove useless turbo_frame_tag
2025-01-30 12:35:30 -05:00

87 lines
2.3 KiB
Ruby

class CategoriesController < ApplicationController
layout :with_sidebar
before_action :set_category, only: %i[edit update destroy]
before_action :set_categories, only: %i[update edit]
before_action :set_transaction, only: :create
def index
@categories = Current.family.categories.alphabetically
end
def new
@category = Current.family.categories.new color: Category::COLORS.sample
set_categories
end
def create
@category = Current.family.categories.new(category_params)
if @category.save
@transaction.update(category_id: @category.id) if @transaction
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
else
set_categories
render :new, status: :unprocessable_entity
end
end
def edit
end
def update
if @category.update(category_params)
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
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@category.destroy
redirect_back_or_to categories_path, notice: t(".success")
end
def bootstrap
Current.family.categories.bootstrap_defaults
redirect_back_or_to categories_path, notice: t(".success")
end
private
def set_category
@category = Current.family.categories.find(params[:id])
end
def set_categories
@categories = unless @category.parent?
Current.family.categories.alphabetically.roots.where.not(id: @category.id)
else
[]
end
end
def set_transaction
if params[:transaction_id].present?
@transaction = Current.family.transactions.find(params[:transaction_id])
end
end
def category_params
params.require(:category).permit(:name, :color, :parent_id, :classification, :lucide_icon)
end
end