Nested Categories (#1561)

* Prepare entry search for nested categories

* Subcategory implementation

* Remove caching for test stability
This commit is contained in:
Zach Gollwitzer
2024-12-20 11:37:26 -05:00
committed by GitHub
parent a4d10097d5
commit 77def1db40
31 changed files with 297 additions and 234 deletions

View File

@@ -10,6 +10,7 @@ class CategoriesController < ApplicationController
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)
end
def create
@@ -17,19 +18,21 @@ class CategoriesController < ApplicationController
if @category.save
@transaction.update(category_id: @category.id) if @transaction
redirect_back_or_to transactions_path, notice: t(".success")
redirect_back_or_to categories_path, notice: t(".success")
else
redirect_back_or_to transactions_path, alert: t(".failure", error: @category.errors.full_messages.to_sentence)
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
redirect_back_or_to transactions_path, notice: t(".success")
redirect_back_or_to categories_path, notice: t(".success")
end
def destroy
@@ -38,6 +41,12 @@ class CategoriesController < ApplicationController
redirect_back_or_to categories_path, notice: t(".success")
end
def bootstrap
Current.family.categories.bootstrap_defaults
redirect_back_or_to categories_path, notice: t(".success")
end
private
def set_category
@category = Current.family.categories.find(params[:id])
@@ -50,6 +59,6 @@ class CategoriesController < ApplicationController
end
def category_params
params.require(:category).permit(:name, :color)
params.require(:category).permit(:name, :color, :parent_id)
end
end

View File

@@ -24,7 +24,6 @@ class RegistrationsController < ApplicationController
if @user.save
@invitation&.update!(accepted_at: Time.current)
Category.create_default_categories(@user.family) unless @invitation
@session = create_session_for(@user)
redirect_to root_path, notice: t(".success")
else

View File

@@ -3,13 +3,13 @@ class TransactionsController < ApplicationController
def index
@q = search_params
result = Current.family.entries.account_transactions.search(@q).reverse_chronological
@pagy, @transaction_entries = pagy(result, limit: params[:per_page] || "50")
search_query = Current.family.transactions.search(@q).includes(:entryable).reverse_chronological
@pagy, @transaction_entries = pagy(search_query, limit: params[:per_page] || "50")
@totals = {
count: result.select { |t| t.currency == Current.family.currency }.count,
income: result.income_total(Current.family.currency).abs,
expense: result.expense_total(Current.family.currency)
count: search_query.select { |t| t.currency == Current.family.currency }.count,
income: search_query.income_total(Current.family.currency).abs,
expense: search_query.expense_total(Current.family.currency)
}
end