Skip to content

Instantly share code, notes, and snippets.

@Zeko369
Created March 17, 2020 22:51
Show Gist options
  • Save Zeko369/8d271d629156230acc66129de365926e to your computer and use it in GitHub Desktop.
Save Zeko369/8d271d629156230acc66129de365926e to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module Users
class RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
# GET /resource/sign_up
def new
if params[:invite_code].present?
@invite = Invite.find_by_hashid(params[:invite_code])
if @invite.present?
if @invite.joined?
flash[:error] = 'Invite code already used'
else
return build_new
end
else
flash[:error] = 'Incorrect invite code'
end
else
flash[:error] = 'Invite code missing'
end
redirect_to root_path
end
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/PerceivedComplexity
# POST /resource
def create
build_resource(sign_up_params)
invite_code = params.require(:user).permit(:invite_code)[:invite_code]
unless invite_code
flash[:error] = 'Missing invite code'
return redirect_to root_path
end
@invite = Invite.find_by_hashid(invite_code)
if @invite.blank?
flash[:error] = 'Cant find invite code'
return redirect_to root_path
elsif @invite.joined?
flash[:error] = 'Invite already used'
return redirect_to root_path
else
resource.namespace = @invite.namespace
end
resource.save
yield resource if block_given?
if resource.persisted?
@invite.joined_at = Time.zone.now
@invite.joined_user = resource
@invite.save!
if @invite.invitable_type == 'Coach'
resource.coach = @invite.invitable
resource.role = Role.coach
end
if resource.active_for_authentication?
set_flash_message! :notice, :signed_up
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
WelcomeMailer.welcome_email(resource.email, kif?).deliver_now
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/PerceivedComplexity
# GET /resource/edit
# def edit
# super
# end
# PUT /resource
# def update
# super
# end
# DELETE /resource
# def destroy
# super
# end
# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end
protected
def build_new
build_resource
yield resource if block_given?
resource.email = @invite.email if resource.email.blank?
if @invite.invitable_type == 'Coach'
resource.first_name = @invite.invitable.first_name unless resource.first_name.present?
resource.last_name = @invite.invitable.last_name unless resource.last_name.present?
end
respond_with resource
end
# If you have extra params to permit, append them to the sanitizer.
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: %i[name email password notification_level_id])
end
# If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
# end
# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end
# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment