mirror of
https://github.com/we-promise/sure.git
synced 2026-09-02 21:31:07 +00:00
* fix(transactions): don't crash the rule-prompt flash when clearing a category needs_rule_notification? only checked saved_change_to_category_id? and eligible_for_category_rule?, neither of which accounts for category_id being nil. Clearing a category (Clear category / entryable_attributes category_id: nil) satisfies both, so the caller went on to read transaction.category.name against a nil category and crashed. A rule prompt only makes sense when a category was assigned, not cleared, so bail out early when there's no category to build a rule around. * feat(transactions): surface recently-used categories in the category picker Reframes the recency-vs-muscle-memory question as additive, not either/or: a small "Recent" section pinned above the existing alphabetical list, which stays exactly where it always was below it. Precedent for reordering the primary list by frequency (Office's old adaptive menus, browser-history-style resorting) is a well-known anti-pattern — position drifts under the user's hand. Every picker that does recency well (VS Code's command palette, Spotify, Slack's emoji picker) adds a small separate recent cluster instead. - Category#last_used_at, touched only in TransactionCategoriesController#update — the one place a category is actually hand-picked by a person, as opposed to a rule or import auto-assigning one. - Category.recently_used_for(family:, excluding:, limit:) batches the family-scoped query; dropdowns_controller excludes the already- selected category from the Recent section since it's already pinned to the top of the main list. - "Recent" hides itself the moment a search query is typed — it's a pre-search shortcut, not a second copy of search results. Its rows are force-hidden (not just filtered) so keyboard nav can't land on a row that's invisible only because its ancestor section is hidden. * fix(categories): address review feedback on recent-categories picker - Track last_used_at from every manual assignment path (transaction edit form, categorization wizard bulk-update, create-and-assign), not just the category-picker endpoint. Centralized as Transaction#record_category_usage!, called explicitly from each manual controller action rather than wired to a blanket after_save callback, since rule/import auto-assignment must not count as a "recent" pick. - Give recent-section rows a distinct DOM id (recent_category_option_<id>) from their canonical-list counterpart so aria-activedescendant can't resolve to a hidden duplicate during keyboard nav. - Fix migration to ActiveRecord::Migration[7.2] to match the rest of the repo. - Materialize @recent_categories with .to_a to avoid a redundant query.
139 lines
4.2 KiB
Ruby
139 lines
4.2 KiB
Ruby
class CategoriesController < ApplicationController
|
|
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
|
|
@categories = Current.family.categories.alphabetically_by_hierarchy.to_a
|
|
@category_groups = Category::Group.for(@categories)
|
|
@category_ids_with_transactions = Category.ids_with_transactions(
|
|
family: Current.family,
|
|
category_ids: @categories.map(&:id)
|
|
)
|
|
|
|
render layout: "settings"
|
|
end
|
|
|
|
def new
|
|
@category = Current.family.categories.new color: Category::COLORS.sample
|
|
set_categories
|
|
end
|
|
|
|
def merge
|
|
@categories = Current.family.categories.alphabetically
|
|
|
|
render layout: turbo_frame_request? ? false : "settings"
|
|
end
|
|
|
|
def create
|
|
@category = Current.family.categories.new(category_params)
|
|
|
|
if @category.save
|
|
if @transaction
|
|
@transaction.update(category_id: @category.id)
|
|
@transaction.record_category_usage!
|
|
end
|
|
|
|
flash[: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
|
|
set_categories
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def edit
|
|
end
|
|
|
|
def update
|
|
if @category.update(category_params)
|
|
flash[: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
|
|
@category.destroy
|
|
|
|
redirect_back_or_to categories_path, notice: t(".success")
|
|
end
|
|
|
|
def destroy_all
|
|
Current.family.categories.destroy_all
|
|
redirect_back_or_to categories_path, notice: t(".success")
|
|
end
|
|
|
|
def bootstrap
|
|
Current.family.categories.bootstrap!
|
|
|
|
redirect_back_or_to categories_path, notice: t(".success")
|
|
end
|
|
|
|
def perform_merge
|
|
permitted_params = category_merge_params
|
|
|
|
if permitted_params[:target_id].present? && Array(permitted_params[:source_ids]).include?(permitted_params[:target_id])
|
|
return redirect_to merge_categories_path, alert: t(".target_selected_as_source")
|
|
end
|
|
|
|
target = Current.family.categories.find_by(id: permitted_params[:target_id])
|
|
return redirect_to merge_categories_path, alert: t(".target_not_found") unless target
|
|
|
|
sources = Current.family.categories.where(id: permitted_params[:source_ids])
|
|
return redirect_to merge_categories_path, alert: t(".invalid_categories") unless sources.any?
|
|
|
|
merger = Category::Merger.new(family: Current.family, target_category: target, source_categories: sources)
|
|
return redirect_to merge_categories_path, alert: t(".no_categories_selected") unless merger.merge!
|
|
|
|
redirect_to categories_path, notice: t(".success", count: merger.merged_count)
|
|
rescue Category::Merger::UnauthorizedCategoryError => e
|
|
redirect_to merge_categories_path, alert: e.message
|
|
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotDestroyed => e
|
|
redirect_to merge_categories_path, alert: record_error_message(e)
|
|
end
|
|
|
|
private
|
|
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])
|
|
end
|
|
end
|
|
|
|
def category_params
|
|
params.require(:category).permit(:name, :color, :parent_id, :lucide_icon)
|
|
end
|
|
|
|
def category_merge_params
|
|
params.permit(:target_id, source_ids: [])
|
|
end
|
|
|
|
def record_error_message(error)
|
|
record = error.respond_to?(:record) ? error.record : nil
|
|
record&.errors&.full_messages&.to_sentence.presence || error.message
|
|
end
|
|
end
|