1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-04 13:05:19 +02:00

Set categories list only for non parents

This commit is contained in:
Elvis Serrão 2025-01-28 13:30:00 -03:00
parent 797cf3c065
commit 8a7df90132
2 changed files with 17 additions and 5 deletions

View file

@ -2,6 +2,7 @@ class CategoriesController < ApplicationController
layout :with_sidebar layout :with_sidebar
before_action :set_category, only: %i[edit update destroy] before_action :set_category, only: %i[edit update destroy]
before_action :set_categories, only: %i[update edit]
before_action :set_transaction, only: :create before_action :set_transaction, only: :create
def index def index
@ -10,7 +11,7 @@ class CategoriesController < ApplicationController
def new def new
@category = Current.family.categories.new color: Category::COLORS.sample @category = Current.family.categories.new color: Category::COLORS.sample
@categories = Current.family.categories.alphabetically.where(parent_id: nil).where.not(id: @category.id) set_categories
end end
def create def create
@ -27,19 +28,21 @@ class CategoriesController < ApplicationController
format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, redirect_target_url) } format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, redirect_target_url) }
end end
else else
@categories = Current.family.categories.alphabetically.where(parent_id: nil).where.not(id: @category.id) set_categories
render :new, status: :unprocessable_entity render :new, status: :unprocessable_entity
end end
end end
def edit def edit
@categories = Current.family.categories.alphabetically.where(parent_id: nil).where.not(id: @category.id)
end end
def update def update
@category.update! category_params if @category.update(category_params)
redirect_back_or_to categories_path, notice: t(".success") redirect_back_or_to categories_path, notice: t(".success")
else
render :edit, status: :unprocessable_entity
end
end end
def destroy def destroy
@ -59,6 +62,14 @@ class CategoriesController < ApplicationController
@category = Current.family.categories.find(params[:id]) @category = Current.family.categories.find(params[:id])
end end
def set_categories
@categories = unless @category.parent?
Current.family.categories.alphabetically.roots.where.not(id: @category.id)
else
[]
end
end
def set_transaction def set_transaction
if params[:transaction_id].present? if params[:transaction_id].present?
@transaction = Current.family.transactions.find(params[:transaction_id]) @transaction = Current.family.transactions.find(params[:transaction_id])

View file

@ -15,6 +15,7 @@ class Category < ApplicationRecord
validate :nested_category_matches_parent_classification validate :nested_category_matches_parent_classification
scope :alphabetically, -> { order(:name) } scope :alphabetically, -> { order(:name) }
scope :roots, -> { where(parent_id: nil) }
scope :incomes, -> { where(classification: "income") } scope :incomes, -> { where(classification: "income") }
scope :expenses, -> { where(classification: "expense") } scope :expenses, -> { where(classification: "expense") }