Skip to content
This repository was archived by the owner on Jul 27, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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
Expand All @@ -10,7 +11,7 @@ def index

def 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_categories
end

def create
Expand All @@ -27,19 +28,26 @@ def create
format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, redirect_target_url) }
end
else
@categories = Current.family.categories.alphabetically.where(parent_id: nil).where.not(id: @category.id)
set_categories
render :new, status: :unprocessable_entity
end
end

def edit
@categories = Current.family.categories.alphabetically.where(parent_id: nil).where.not(id: @category.id)
end

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

def destroy
Expand All @@ -59,6 +67,14 @@ 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])
Expand Down
7 changes: 6 additions & 1 deletion app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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") }

Expand Down Expand Up @@ -91,6 +92,10 @@ def replace_and_destroy!(replacement)
end
end

def parent?
subcategories.any?
end

def subcategory?
parent.present?
end
Expand Down Expand Up @@ -121,7 +126,7 @@ def month_total_money(date: Date.current)

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
Expand Down
6 changes: 4 additions & 2 deletions app/views/categories/_form.html.erb
Original file line number Diff line number Diff line change
@@ -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 } %>
Expand Down Expand Up @@ -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 %>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here, we can just have a conditional:

<% unless category.parent? %>
  <%= f.select :parent_id ...
<% end %>

That way, if the user is not allowed to assign a parent, we don't ever give them the option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, i agree.

</div>
</section>

Expand Down