Files
sure/app/controllers/account_sharings_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

63 lines
2.2 KiB
Ruby

class AccountSharingsController < ApplicationController
before_action :set_account
def show
@family_members = Current.family.users.where.not(id: @account.owner_id).where(active: true)
@account_shares = @account.account_shares.includes(:user).index_by(&:user_id)
end
def update
# Non-owners can update their own include_in_finances preference
if !@account.owned_by?(Current.user) && params[:update_finance_inclusion].present?
share = @account.account_shares.find_by!(user: Current.user)
include_value = params.permit(:include_in_finances)[:include_in_finances]
share.update!(include_in_finances: ActiveModel::Type::Boolean.new.cast(include_value))
redirect_back_or_to account_path(@account), notice: t("account_sharings.update.finance_toggle_success")
return
end
unless @account.owned_by?(Current.user)
redirect_to account_path(@account), alert: t("account_sharings.update.not_owner")
return
end
eligible_members = Current.family.users.where.not(id: @account.owner_id).where(active: true)
AccountShare.transaction do
sharing_members_params.each do |member_params|
user = eligible_members.find_by(id: member_params[:user_id])
next unless user
share = @account.account_shares.find_by(user: user)
if ActiveModel::Type::Boolean.new.cast(member_params[:shared])
permission = AccountShare::PERMISSIONS.include?(member_params[:permission]) ? member_params[:permission] : (share&.permission || "read_only")
if share
share.update!(permission: permission)
else
@account.account_shares.create!(user: user, permission: permission, include_in_finances: true)
end
elsif share
share.destroy!
end
end
end
redirect_back_or_to accounts_path, notice: t("account_sharings.update.success")
end
private
def set_account
@account = Current.user.accessible_accounts.find(params[:account_id])
end
def sharing_members_params
return [] unless params.dig(:sharing, :members)
params.require(:sharing).permit(
members: [ :user_id, :shared, :permission ]
)[:members]&.values || []
end
end