dawarich/app/controllers/families_controller.rb

116 lines
3 KiB
Ruby
Raw Normal View History

2025-09-27 07:23:33 -04:00
# frozen_string_literal: true
class FamiliesController < ApplicationController
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, only: %i[show edit update destroy leave]
def index
redirect_to family_path(current_user.family) if current_user.in_family?
end
def show
authorize @family
2025-09-28 07:10:07 -04:00
# Use optimized family methods for better performance
@members = @family.members.includes(:family_membership).order(:email)
@pending_invitations = @family.active_invitations.order(:created_at)
# Use cached counts to avoid extra queries
@member_count = @family.member_count
@can_invite = @family.can_add_members?
2025-09-27 07:23:33 -04:00
end
def new
redirect_to family_path(current_user.family) if current_user.in_family?
@family = Family.new
end
def create
service = Families::Create.new(
user: current_user,
name: family_params[:name]
)
if service.call
redirect_to family_path(service.family), notice: 'Family created successfully!'
else
@family = Family.new(family_params)
2025-09-28 07:10:07 -04:00
# Handle validation errors
if service.errors.any?
service.errors.each do |attribute, message|
@family.errors.add(attribute, message)
end
end
# Handle service-level errors
if service.error_message.present?
@family.errors.add(:base, service.error_message)
2025-09-27 07:23:33 -04:00
end
2025-09-28 07:10:07 -04:00
flash.now[:alert] = service.error_message || 'Failed to create family'
2025-09-27 08:04:10 -04:00
render :new, status: :unprocessable_content
2025-09-27 07:23:33 -04:00
end
end
def edit
authorize @family
end
def update
authorize @family
if @family.update(family_params)
redirect_to family_path(@family), notice: 'Family updated successfully!'
else
2025-09-27 08:04:10 -04:00
render :edit, status: :unprocessable_content
2025-09-27 07:23:33 -04:00
end
end
def destroy
authorize @family
if @family.members.count > 1
redirect_to family_path(@family), alert: 'Cannot delete family with members. Remove all members first.'
else
@family.destroy
redirect_to families_path, notice: 'Family deleted successfully!'
end
end
def leave
2025-09-27 15:14:40 -04:00
authorize @family, :leave?
2025-09-27 07:23:33 -04:00
2025-09-27 15:14:40 -04:00
service = Families::Leave.new(user: current_user)
2025-09-27 07:23:33 -04:00
2025-09-27 15:14:40 -04:00
if service.call
redirect_to families_path, notice: 'You have left the family'
else
redirect_to family_path(@family), alert: service.error_message || 'Cannot leave family.'
end
rescue Pundit::NotAuthorizedError
# Handle case where owner with members tries to leave
redirect_to family_path(@family),
alert: 'You cannot leave the family while you are the owner and there are other members. Remove all members first or transfer ownership.'
2025-09-27 07:23:33 -04:00
end
private
2025-09-28 07:10:07 -04:00
def ensure_family_feature_enabled!
unless DawarichSettings.family_feature_enabled?
redirect_to root_path, alert: 'Family feature is not available'
end
end
2025-09-27 07:23:33 -04:00
def set_family
@family = current_user.family
redirect_to families_path unless @family
end
def family_params
params.require(:family).permit(:name)
end
end