mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 15:49:39 +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
|
@ -15,6 +15,7 @@ class Category < ApplicationRecord
|
|||
validate :nested_category_matches_parent_classification
|
||||
|
||||
scope :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 < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def parent?
|
||||
subcategories.any?
|
||||
end
|
||||
|
||||
def subcategory?
|
||||
parent.present?
|
||||
end
|
||||
|
@ -121,7 +126,7 @@ class Category < ApplicationRecord
|
|||
|
||||
private
|
||||
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")
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue