Files
sure/app/helpers/budgets_helper.rb
T
e5750a6c09 feat(budgets): add per-user personal budgets with strict isolation (#2891)
* feat(budgets): add per-user personal budgets with strict isolation

Families can now opt into personal budgets (toggleable via family
settings): each family member gets their own budget for a given
period instead of sharing a single family-wide budget.

- Add families.personal_budgets flag and budgets.user_id, with
  partial unique indexes so shared budgets (user_id IS NULL) and
  personal budgets (user_id IS NOT NULL) can't collide.
- Budget.find_or_bootstrap scopes lookup/creation by user when the
  family has personal_budgets enabled.
- Scope most_recent_initialized_budget (used to seed a new budget
  from the prior period) by user_id so one user's copy-forward never
  bleeds into another user's budget.
- budgets.user_id cascades on user deletion so personal budgets don't
  outlive their owner.

* feat(budgets): enforce user-specific budget ownership and cascade deletion

* feat(budgets): display user name for personal budgets in budget card on the plan section

* feat(budgets): enhance personal budgets display for admins with preview feature indication

* feat(budgets): enforce user-specific budget and category visibility for personal budgets

* feat(budgets): create budget section titles and add translations notice in preferences

* feat(budgets): let household and personal budgets coexist with sharing

Previously enabling personal_budgets made the shared household budget
unreachable. Budget.find_or_bootstrap now takes an explicit household:
flag so both can be resolved independently for the same period, with a
new household_budget_enabled family setting to opt out of the household
side and keep personal budgets only.

Adds a BudgetShare model (read_only/read_write) so a member can grant
another family member access to their personal budget, enforced via
Budget#viewable_by?/editable_by? across BudgetsController,
BudgetCategoriesController, PlansController, and the read-only API.
Preferences gains a Budget sharing card (gated on preview access like
the rest of the personal budgets UI) and an owner switcher pill (
Household / mine / shared-with-me) appears on the budget page and the
Plan hub card.

Also fixes personal budgets showing the same "actual spending" as the
household budget: actual spending/income now scope to the budget
owner's own accounts instead of the viewer's full accessible set,
via a new accounts: override on IncomeStatement.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* feat(budgets): enhance budget switcher with icons and improved styling

* feat(budgets): remove user name display from budget card and header

* feat(budgets): remove unique index on taggable_type and taggable_id in taggings

* feat(budgets): enhance budget sharing functionality and improve UI elements

* Collapse personal budget migrations

---------

Signed-off-by: JulienGourmet <69808509+jubbakka@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: sure-admin <sure-admin@splashblot.com>
2026-08-18 08:36:46 +02:00

82 lines
3.3 KiB
Ruby

module BudgetsHelper
# Forwards the current `owner` param (if any) so links generated while
# viewing the household budget or a shared member's budget keep pointing
# at that same budget instead of silently resetting to "mine". Defined
# here (rather than only in BudgetOwnership) so it's available to any view
# context, including isolated view tests that render budget partials
# without a full BudgetsController/BudgetCategoriesController/
# PlansController request cycle.
def budget_owner_query
params[:owner].present? ? { owner: params[:owner] } : {}
end
def budget_has_over_budget?(budget)
return false unless budget.initialized?
budget.budget_categories.any?(&:any_over_budget?) || budget.uncategorized_budget_category.any_over_budget?
end
def budget_categories_view_state(budget)
@budget_categories_view_state ||= {}
@budget_categories_view_state[budget.object_id] ||= build_budget_categories_view_state(budget)
end
private
def build_budget_categories_view_state(budget)
uncategorized_budget_category = budget.uncategorized_budget_category
all_category_groups = BudgetCategory::Group.for(budget.budget_categories)
over_budget_groups = if budget.initialized?
filtered_groups_for(all_category_groups) { |budget_category| budget_category.any_over_budget? }
else
[]
end
show_over_budget_uncategorized = budget.initialized? && uncategorized_budget_category.any_over_budget?
over_budget_count = visible_count_for(over_budget_groups) { |budget_category| budget_category.any_over_budget? }
over_budget_count += 1 if show_over_budget_uncategorized
on_track_groups = if budget.initialized?
filtered_groups_for(all_category_groups) { |budget_category| budget_category.visible_on_track? }
else
all_category_groups
end
show_on_track_uncategorized = all_category_groups.any? && (!budget.initialized? || uncategorized_budget_category.on_track?)
on_track_count = visible_count_for(on_track_groups) { |budget_category| parent_visible_for_on_track?(budget, budget_category) }
on_track_count += 1 if show_on_track_uncategorized
visible_expenses_empty = on_track_count.zero?
{
uncategorized_budget_category: uncategorized_budget_category,
visible_expenses_empty: visible_expenses_empty,
over_budget_groups: over_budget_groups,
show_over_budget_uncategorized: show_over_budget_uncategorized,
over_budget_count: over_budget_count,
on_track_groups: on_track_groups,
show_on_track_uncategorized: show_on_track_uncategorized,
on_track_count: on_track_count
}
end
def parent_visible_for_on_track?(budget, budget_category)
budget.initialized? ? budget_category.visible_on_track? : true
end
def filtered_groups_for(groups)
groups.each_with_object([]) do |group, filtered_groups|
visible_subcategories = group.budget_subcategories.select { |budget_category| yield(budget_category) }
next unless yield(group.budget_category) || visible_subcategories.any?
filtered_groups << BudgetCategory::Group.new(group.budget_category, visible_subcategories)
end
end
def visible_count_for(groups)
groups.sum do |group|
(yield(group.budget_category) ? 1 : 0) + group.budget_subcategories.count
end
end
end