dawarich/app/controllers/family/memberships_controller.rb

38 lines
1 KiB
Ruby
Raw Normal View History

2025-09-27 07:23:33 -04:00
# frozen_string_literal: true
class Family::MembershipsController < ApplicationController
2025-09-27 07:23:33 -04:00
before_action :authenticate_user!
2025-09-28 07:10:07 -04:00
before_action :ensure_family_feature_enabled!
2025-09-27 07:23:33 -04:00
before_action :set_family
2025-10-04 10:17:26 -04:00
before_action :set_membership, only: %i[destroy]
2025-09-27 07:23:33 -04:00
def destroy
authorize @membership
2025-10-04 16:39:47 -04:00
member_user = @membership.user
service = Families::Memberships::Destroy.new(user: current_user, member_to_remove: member_user)
if service.call
if member_user == current_user
redirect_to new_family_path, notice: 'You have left the family'
else
redirect_to family_path, notice: "#{member_user.email} has been removed from the family"
end
2025-09-27 07:23:33 -04:00
else
2025-10-04 16:39:47 -04:00
redirect_to family_path, alert: service.error_message || 'Failed to remove member'
2025-09-27 07:23:33 -04:00
end
end
private
def set_family
@family = current_user.family
2025-10-04 16:39:47 -04:00
redirect_to new_family_path, alert: 'You are not in a family' and return unless @family
2025-09-27 07:23:33 -04:00
end
def set_membership
@membership = @family.family_memberships.find(params[:id])
end
end