2024-02-02 09:05:04 -06:00
|
|
|
class RegistrationsController < ApplicationController
|
2024-04-03 10:35:55 -04:00
|
|
|
skip_authentication
|
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
layout "auth"
|
2024-02-02 16:06:55 +00:00
|
|
|
|
2024-02-02 17:49:28 -06:00
|
|
|
before_action :set_user, only: :create
|
2024-11-01 10:23:27 -05:00
|
|
|
before_action :set_invitation
|
2024-04-13 09:28:45 -04:00
|
|
|
before_action :claim_invite_code, only: :create, if: :invite_code_required?
|
2024-02-02 17:49:28 -06:00
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
def new
|
2024-11-01 10:23:27 -05:00
|
|
|
@user = User.new(email: @invitation&.email)
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2024-11-01 10:23:27 -05:00
|
|
|
if @invitation
|
|
|
|
@user.family = @invitation.family
|
|
|
|
@user.role = @invitation.role
|
|
|
|
@user.email = @invitation.email
|
|
|
|
else
|
|
|
|
family = Family.new
|
|
|
|
@user.family = family
|
|
|
|
@user.role = :admin
|
|
|
|
end
|
2024-02-02 09:05:04 -06:00
|
|
|
|
|
|
|
if @user.save
|
2024-11-01 10:23:27 -05:00
|
|
|
@invitation&.update!(accepted_at: Time.current)
|
2024-10-03 14:42:22 -04:00
|
|
|
@session = create_session_for(@user)
|
2024-11-01 10:23:27 -05:00
|
|
|
redirect_to root_path, notice: t(".success")
|
2024-02-02 09:05:04 -06:00
|
|
|
else
|
2024-11-11 15:41:17 +01:00
|
|
|
render :new, status: :unprocessable_entity, alert: t(".failure")
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-11-01 10:23:27 -05:00
|
|
|
def set_invitation
|
|
|
|
token = params[:invitation]
|
|
|
|
token ||= params[:user][:invitation] if params[:user].present?
|
|
|
|
@invitation = Invitation.pending.find_by(token: token)
|
|
|
|
end
|
|
|
|
|
2024-08-23 10:06:24 -04:00
|
|
|
def set_user
|
2024-11-01 10:23:27 -05:00
|
|
|
@user = User.new user_params.except(:invite_code, :invitation)
|
2024-08-23 10:06:24 -04:00
|
|
|
end
|
2024-02-02 17:49:28 -06:00
|
|
|
|
2024-11-01 10:23:27 -05:00
|
|
|
def user_params(specific_param = nil)
|
|
|
|
params = self.params.require(:user).permit(:name, :email, :password, :password_confirmation, :invite_code, :invitation)
|
|
|
|
specific_param ? params[specific_param] : params
|
2024-08-23 10:06:24 -04:00
|
|
|
end
|
2024-02-02 17:49:28 -06:00
|
|
|
|
2024-08-23 10:06:24 -04:00
|
|
|
def claim_invite_code
|
|
|
|
unless InviteCode.claim! params[:user][:invite_code]
|
|
|
|
redirect_to new_registration_path, alert: t("registrations.create.invalid_invite_code")
|
|
|
|
end
|
2024-02-02 17:49:28 -06:00
|
|
|
end
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|