2014-08-22 15:08:54 +02:00
class ConferenceRegistrationsController < ApplicationController
2015-03-12 23:53:57 +01:00
before_filter :authenticate_user! , except : [ :new , :create ]
2014-08-22 15:08:54 +02:00
load_resource :conference , find_by : :short_title
2016-03-17 14:54:35 +05:30
authorize_resource :conference_registrations , class : Registration , except : [ :new , :create ]
2014-08-28 15:21:05 +02:00
before_action :set_registration , only : [ :edit , :update , :destroy , :show ]
2014-08-22 15:08:54 +02:00
def new
2016-03-17 14:54:35 +05:30
@registration = Registration . new ( conference_id : @conference . id )
2016-05-04 23:46:52 +02:00
authorize! :new , @registration , message : " Sorry, you can not register for #{ @conference . title } . Registration limit exceeded or the registration is not open. "
2016-03-17 14:54:35 +05:30
2015-04-09 10:19:32 +02:00
# Redirect to registration edit when user is already registered
if @conference . user_registered? ( current_user )
2016-07-05 00:10:18 +05:30
redirect_to edit_conference_conference_registration_path ( @conference . short_title )
2015-04-09 10:19:32 +02:00
return
2015-03-12 23:53:57 +01:00
# ichain does not allow us to create users during registration
2016-03-22 18:14:40 +01:00
elsif ( ENV [ 'OSEM_ICHAIN_ENABLED' ] == 'true' ) && ! current_user
2015-03-12 23:53:57 +01:00
redirect_to root_path , alert : 'You need to sign in or sign up before continuing.'
2015-04-09 10:19:32 +02:00
return
2015-03-12 23:53:57 +01:00
end
2015-04-18 21:44:40 +03:00
# avoid openid sign_in to redirect to register/new when the sign_in user had already a registration
if current_user && @conference . user_registered? ( current_user )
2016-07-05 00:10:18 +05:30
redirect_to edit_conference_conference_registration_path ( @conference . short_title )
2015-04-18 21:44:40 +03:00
end
2015-04-23 14:53:55 +03:00
# @user variable needs to be set so that _sign_up_form_embedded works properly
2015-04-18 21:44:40 +03:00
@user = @registration . build_user
2014-08-22 15:08:54 +02:00
end
2014-08-28 15:21:05 +02:00
def show
@total_price = Ticket . total_price ( @conference , current_user )
2014-12-09 15:19:01 +01:00
@tickets = current_user . ticket_purchases . where ( conference_id : @conference . id )
2014-08-22 15:08:54 +02:00
end
2014-08-28 15:21:05 +02:00
def edit ; end
2014-08-22 15:08:54 +02:00
def create
2015-04-18 21:44:40 +03:00
@registration = @conference . registrations . new ( registration_params )
2015-04-23 14:53:55 +03:00
2015-06-11 15:22:54 +03:00
if current_user . nil?
2015-04-23 14:53:55 +03:00
# @user variable needs to be set so that _sign_up_form_embedded works properly
@user = @registration . build_user ( user_params )
else
@user = current_user
end
@registration . user = @user
2016-03-17 14:54:35 +05:30
authorize! :create , @registration
2014-08-22 15:08:54 +02:00
2014-12-05 16:04:34 +01:00
if @registration . save
2014-08-22 15:08:54 +02:00
# Trigger ahoy event
ahoy . track 'Registered' , title : 'New registration'
2015-03-12 23:53:57 +01:00
# Sign in the new user
2016-08-06 00:57:07 +02:00
unless current_user
2015-03-12 23:53:57 +01:00
sign_in ( @registration . user )
end
2014-12-05 16:04:34 +01:00
if @conference . tickets . any? && ! current_user . supports? ( @conference )
2016-03-10 14:25:38 +05:30
redirect_to conference_tickets_path ( @conference . short_title ) ,
notice : 'You are now registered and will be receiving E-Mail notifications.'
2014-08-28 15:21:05 +02:00
else
2016-07-05 00:10:18 +05:30
redirect_to conference_conference_registration_path ( @conference . short_title ) ,
2016-03-10 14:25:38 +05:30
notice : 'You are now registered and will be receiving E-Mail notifications.'
2014-08-22 15:08:54 +02:00
end
else
2015-04-18 21:44:40 +03:00
flash [ :error ] = " Could not create your registration for #{ @conference . title } : " \
2014-08-22 15:08:54 +02:00
" #{ @registration . errors . full_messages . join ( '. ' ) } . "
render :new
end
end
def update
2014-08-28 15:21:05 +02:00
if @registration . update_attributes ( registration_params )
2016-07-05 00:10:18 +05:30
redirect_to conference_conference_registration_path ( @conference . short_title ) ,
2014-08-28 15:21:05 +02:00
notice : 'Registration was successfully updated.'
2014-08-22 15:08:54 +02:00
else
2015-04-18 21:44:40 +03:00
flash [ :error ] = " Could not update your registration for #{ @conference . title } : " \
2014-08-22 15:08:54 +02:00
" #{ @registration . errors . full_messages . join ( '. ' ) } . "
render :edit
end
end
def destroy
2014-08-28 15:21:05 +02:00
if @registration . destroy
redirect_to root_path ,
notice : " You are not registered for #{ @conference . title } anymore! "
else
2016-07-05 00:10:18 +05:30
redirect_to conference_conference_registration_path ( @conference . short_title ) ,
2016-03-23 11:42:40 +05:30
error : " Could not delete your registration for #{ @conference . title } : " \
2014-08-28 15:21:05 +02:00
" #{ @registration . errors . full_messages . join ( '. ' ) } . "
end
2014-08-22 15:08:54 +02:00
end
protected
def set_registration
2015-03-12 23:53:57 +01:00
@registration = Registration . find_by ( conference : @conference , user : current_user )
2016-08-06 00:57:07 +02:00
unless @registration
2016-07-05 00:10:18 +05:30
redirect_to new_conference_conference_registration_path ( @conference . short_title ) ,
2016-03-16 11:20:56 +05:30
error : " Can't find a registration for #{ @conference . title } for you. Please register. "
2014-12-05 16:04:34 +01:00
end
2014-08-22 15:08:54 +02:00
end
2015-04-18 21:44:40 +03:00
def user_params
params . require ( :user ) . permit ( :username , :email , :name , :password , :password_confirmation )
end
2014-08-22 15:08:54 +02:00
def registration_params
params . require ( :registration ) .
permit (
:conference_id , :arrival , :departure ,
:volunteer ,
vchoice_ids : [ ] , qanswer_ids : [ ] ,
qanswers_attributes : [ ] ,
event_ids : [ ] ,
user_attributes : [
2015-03-12 23:53:57 +01:00
:username , :email , :name , :password , :password_confirmation ]
2014-08-28 15:21:05 +02:00
)
2014-08-22 15:08:54 +02:00
end
end