Skip to content
This repository was archived by the owner on Jul 27, 2025. It is now read-only.

fix: subcategories are not properly handled for budget allocations#1844

Merged
zachgoll merged 7 commits intomaybe-finance:mainfrom
pauleke65:1665-bug-subcategories-are-not-properly-handled-for-budget-allocations
Feb 11, 2025
Merged

fix: subcategories are not properly handled for budget allocations#1844
zachgoll merged 7 commits intomaybe-finance:mainfrom
pauleke65:1665-bug-subcategories-are-not-properly-handled-for-budget-allocations

Conversation

@pauleke65
Copy link
Contributor

This PR ensures that subcategory budget allocations cannot exceed the total budget allocated to their parent category.

Changes:

  • Updated the allocated_spending method to only sum parent category budgets.
  • Added logic in the budget category form to enforce a max allocation based on the parent’s available budget.

Issue Fixed:
Fixes #1665

Testing:

  • Tried allocating more to subcategories than the parent allows ✅
  • Verified that subcategory allocations are correctly restricted ✅

Demo:

Screen.Recording.2025-02-10.at.11.25.58.AM.mov

Copy link
Contributor

@zachgoll zachgoll left a comment

Choose a reason for hiding this comment

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

I like this solution! I think we should rearrange a bit of the code though to fit the budget domain a bit closer. I've left some ideas on how we could do that.

Finally, I think in order for these client-side validations to properly work, we'll need to update the Turbo stream responses for the update action so that subcategory forms and their directly siblings are replaced each time a subcategory budget is updated. This will ensure that the max: validation value is directly updated to reflect the new "max" based on the other subcategory sibling budget amounts.

@pauleke65
Copy link
Contributor Author

Finally, I think in order for these client-side validations to properly work, we'll need to update the Turbo stream responses for the update action so that subcategory forms and their directly siblings are replaced each time a subcategory budget is updated. This will ensure that the max: validation value is directly updated to reflect the new "max" based on the other subcategory sibling budget amounts.

I tried getting the forms to re-update on the new changes, but couldn't quite get it.
I'm still very new to rails, and not familiar with how turbo streams work but I'll give a second look

@zachgoll
Copy link
Contributor

@pauleke65 yeah the streams take a bit of getting used to! I think it can all be achieved here in this file:

https://github.com/maybe-finance/maybe/blob/main/app/views/budget_categories/update.turbo_stream.erb

We'll likely need to add something along the lines of (untested code below):

<% if @budget_category.subcategory? %>
  <% @budget_category.siblings.each do |sibling| %>
    <%= turbo_stream.update dom_id(sibling, :form), partial: "budget_categories/budget_category_form", locals: { budget_category: sibling } %>
  <% end %>
<% end %>

And then over in _budget_category_form.html.erb, add the id so it can be referenced:

<div id=<%= dom_id(budget_category, :form) %> class="w-full flex gap-3">

And finally, add that siblings method to budget_category.rb:

def siblings
  budget.budget_categories.select { |bc| bc.category.parent_id == category.parent_id && bc.id != id }
end

@pauleke65 pauleke65 requested a review from zachgoll February 10, 2025 20:01
Copy link
Contributor

@zachgoll zachgoll left a comment

Choose a reason for hiding this comment

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

Looks great! Just waiting for tests to pass and will get it merged.

@pauleke65
Copy link
Contributor Author

resolved lint issues

@zachgoll zachgoll merged commit fb6c6fa into maybe-finance:main Feb 11, 2025
5 checks passed
@pauleke65 pauleke65 deleted the 1665-bug-subcategories-are-not-properly-handled-for-budget-allocations branch February 15, 2025 18:23
pranav7 pushed a commit to pranav7/maybe that referenced this pull request Mar 2, 2025
…aybe-finance#1844)

* fix: `allocated_spending` logic

* fix: subcategories exceeding parent limit

* refactor: budget allocations and max allocation logic

* feat: add stream for budget category form validation

* feat: update uncategorized value via stream, refactor confirm button with stream

* fix: ensure live updates for parent & sibling budgets in Turbo Stream

* fix: lint issues
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Subcategories are not properly handled for budget allocations

3 participants