mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-23 15:19:38 +02:00
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 commit866140c196
. * Revert "Add turbo_stream format to requests on create and update action's tests" This reverts commitc6bf21490f
. * Add correct redirect url for both html and turbo_stream formats * Remove useless turbo_frame_tag
This commit is contained in:
parent
3b0f8ae8c2
commit
0b17976256
3 changed files with 31 additions and 8 deletions
|
@ -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,26 @@ 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)
|
||||||
|
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
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
@ -59,6 +67,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])
|
||||||
|
|
|
@ -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") }
|
||||||
|
|
||||||
|
@ -91,6 +92,10 @@ class Category < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parent?
|
||||||
|
subcategories.any?
|
||||||
|
end
|
||||||
|
|
||||||
def subcategory?
|
def subcategory?
|
||||||
parent.present?
|
parent.present?
|
||||||
end
|
end
|
||||||
|
@ -121,7 +126,7 @@ class Category < ApplicationRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
def category_level_limit
|
def 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")
|
errors.add(:parent, "can't have more than 2 levels of subcategories")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<%# locals: (category:, categories:) %>
|
<%# locals: (category:, categories:) %>
|
||||||
|
|
||||||
<div data-controller="color-avatar">
|
<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">
|
<section class="space-y-4">
|
||||||
<div class="w-fit m-auto">
|
<div class="w-fit m-auto">
|
||||||
<%= render partial: "shared/color_avatar", locals: { name: category.name, color: category.color } %>
|
<%= render partial: "shared/color_avatar", locals: { name: category.name, color: category.color } %>
|
||||||
|
@ -34,7 +34,9 @@
|
||||||
<div class="space-y-2">
|
<div class="space-y-2">
|
||||||
<%= f.select :classification, [["Income", "income"], ["Expense", "expense"]], { label: "Classification" }, required: true %>
|
<%= 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.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>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue