The personal finance app for everyone
Don't allow a subcategory to be assigned to another subcategory to ensure 1 level of nesting max (#1730)
Elvis Serrão0b17976256ee
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rbindex 03752869..a9d3d105 100644--- a/app/controllers/categories_controller.rb+++ b/app/controllers/categories_controller.rb@@ -2,6 +2,7 @@ class CategoriesController < ApplicationControllerlayout :with_sidebarbefore_action :set_category, only: %i[edit update destroy]+ before_action :set_categories, only: %i[update edit]before_action :set_transaction, only: :createdef index@@ -10,7 +11,7 @@ class CategoriesController < ApplicationControllerdef new@category = Current.family.categories.new color: Category::COLORS.sample- @categories = Current.family.categories.alphabetically.where(parent_id: nil).where.not(id: @category.id)+ set_categoriesenddef create@@ -27,19 +28,26 @@ class CategoriesController < ApplicationControllerformat.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, redirect_target_url) }endelse- @categories = Current.family.categories.alphabetically.where(parent_id: nil).where.not(id: @category.id)+ set_categoriesrender :new, status: :unprocessable_entityendenddef edit- @categories = Current.family.categories.alphabetically.where(parent_id: nil).where.not(id: @category.id)enddef update- @category.update! category_params+ if @category.update(category_params)+ flash[:notice] = t(".success")- redirect_back_or_to categories_path, 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+ endenddef destroy@@ -59,6 +67,14 @@ class CategoriesController < ApplicationController@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_transactionif params[:transaction_id].present?@transaction = Current.family.transactions.find(params[:transaction_id])diff --git a/app/models/category.rb b/app/models/category.rbindex ebeadbf9..d0ccaa60 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -15,6 +15,7 @@ class Category < ApplicationRecordvalidate :nested_category_matches_parent_classificationscope :alphabetically, -> { order(:name) }+ scope :roots, -> { where(parent_id: nil) }scope :incomes, -> { where(classification: "income") }scope :expenses, -> { where(classification: "expense") }@@ -91,6 +92,10 @@ class Category < ApplicationRecordendend+ def parent?+ subcategories.any?+ end+def subcategory?parent.present?end@@ -121,7 +126,7 @@ class Category < ApplicationRecordprivatedef category_level_limit- if subcategory? && parent.subcategory?+ if (subcategory? && parent.subcategory?) || (parent? && subcategory?)errors.add(:parent, "can't have more than 2 levels of subcategories")endenddiff --git a/app/views/categories/_form.html.erb b/app/views/categories/_form.html.erbindex 8927c0bf..884d5509 100644--- a/app/views/categories/_form.html.erb+++ b/app/views/categories/_form.html.erb@@ -1,7 +1,7 @@<%# locals: (category:, categories:) %><div data-controller="color-avatar">- <%= styled_form_with model: category, class: "space-y-4", data: { turbo_frame: :_top } do |f| %>+ <%= styled_form_with model: category, class: "space-y-4" do |f| %><section class="space-y-4"><div class="w-fit m-auto"><%= render partial: "shared/color_avatar", locals: { name: category.name, color: category.color } %>@@ -34,7 +34,9 @@<div class="space-y-2"><%= f.select :classification, [["Income", "income"], ["Expense", "expense"]], { label: "Classification" }, required: true %><%= f.text_field :name, placeholder: t(".placeholder"), required: true, autofocus: true, label: "Name", data: { color_avatar_target: "name" } %>- <%= f.select :parent_id, categories.pluck(:name, :id), { include_blank: "(unassigned)", label: "Parent category (optional)" } %>+ <% unless category.parent? %>+ <%= f.select :parent_id, categories.pluck(:name, :id), { include_blank: "(unassigned)", label: "Parent category (optional)" }, disabled: category.parent? %>+ <% end %></div></section>