Files
sure/app/controllers/splits_controller.rb
soky srm 560c9fbff3 Family sharing (#1272)
* Initial account sharing changes

* Update schema.rb

* Update schema.rb

* Change sharing UI to modal

* UX fixes and sharing controls

* Scope include in finances better

* Update totals.rb

* Update totals.rb

* Scope reports to finance account scope

* Update impersonation_sessions_controller_test.rb

* Review fixes

* Update schema.rb

* Update show.html.erb

* FIX db validation

* Refine edit permissions

* Review items

* Review

* Review

* Add application level helper

* Critical review

* Address remaining review items

* Fix modals

* more scoping

* linter

* small UI fix

* Fix: Sync broadcasts push unscoped balance sheet to all users

* Update sync_complete_event.rb

 The fix removes the sidebar broadcasts (which rendered unscoped account groups using family.balance_sheet without user context)
  along with the now-unused sidebar_targets, account_group, and family_balance_sheet private methods.

  The sidebar will still update correctly — when the sync completes, Family::SyncCompleteEvent#broadcast fires family.broadcast_refresh, which triggers a
  morph-based page refresh for each user with their own authenticated session, rendering properly scoped sidebar content.
2026-03-25 10:50:23 +01:00

104 lines
2.7 KiB
Ruby

class SplitsController < ApplicationController
before_action :set_entry
before_action :require_split_write_permission!, only: %i[create update destroy]
def new
@categories = Current.family.categories.alphabetically
end
def create
unless @entry.transaction.splittable?
redirect_back_or_to transactions_path, alert: t("splits.create.not_splittable")
return
end
raw_splits = split_params[:splits]
raw_splits = raw_splits.values if raw_splits.respond_to?(:values)
splits = raw_splits.map do |s|
{ name: s[:name], amount: s[:amount].to_d * -1, category_id: s[:category_id].presence }
end
@entry.split!(splits)
@entry.sync_account_later
redirect_back_or_to transactions_path, notice: t("splits.create.success")
rescue ActiveRecord::RecordInvalid => e
redirect_back_or_to transactions_path, alert: e.message
end
def edit
resolve_to_parent!
unless @entry.split_parent?
redirect_to transactions_path, alert: t("splits.edit.not_split")
return
end
@categories = Current.family.categories.alphabetically
@children = @entry.child_entries.includes(:entryable)
end
def update
resolve_to_parent!
unless @entry.split_parent?
redirect_to transactions_path, alert: t("splits.edit.not_split")
return
end
raw_splits = split_params[:splits]
raw_splits = raw_splits.values if raw_splits.respond_to?(:values)
splits = raw_splits.map do |s|
{ name: s[:name], amount: s[:amount].to_d * -1, category_id: s[:category_id].presence }
end
Entry.transaction do
@entry.unsplit!
@entry.split!(splits)
end
@entry.sync_account_later
redirect_to transactions_path, notice: t("splits.update.success")
rescue ActiveRecord::RecordInvalid => e
redirect_to transactions_path, alert: e.message
end
def destroy
resolve_to_parent!
unless @entry.split_parent?
redirect_to transactions_path, alert: t("splits.edit.not_split")
return
end
@entry.unsplit!
@entry.sync_account_later
redirect_to transactions_path, notice: t("splits.destroy.success")
end
private
def set_entry
@entry = Current.accessible_entries.find(params[:transaction_id])
end
def require_split_write_permission!
permission = @entry.account.permission_for(Current.user)
unless permission.in?([ :owner, :full_control ])
redirect_back_or_to transactions_path, alert: t("accounts.not_authorized")
end
end
def resolve_to_parent!
@entry = @entry.parent_entry if @entry.split_child?
end
def split_params
params.require(:split).permit(splits: [ :name, :amount, :category_id ])
end
end