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)
|
|
|
|
|
|
2015-04-09 10:19:32 +02:00
|
|
|
# Redirect to registration edit when user is already registered
|
|
|
|
|
if @conference.user_registered?(current_user)
|
2017-02-02 01:28:22 +02:00
|
|
|
# Authorization needs to happen in every action before the return statement
|
|
|
|
|
# We authorize the #edit action, since we redirect to it
|
|
|
|
|
authorize! :edit, current_user.registrations.find_by(conference_id: @conference.id)
|
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
|
|
|
end
|
2015-04-18 21:44:40 +03:00
|
|
|
|
2017-02-02 01:28:22 +02:00
|
|
|
if !@conference.registration_open? || @conference.registration_limit_exceeded?
|
|
|
|
|
message = "Sorry, you can not register for #{@conference.title}. Registration limit exceeded or the registration is not open."
|
|
|
|
|
@ignore_not_signed_in_user = true
|
2015-04-18 21:44:40 +03:00
|
|
|
end
|
2017-02-02 01:28:22 +02:00
|
|
|
authorize! :new, @registration, message: message
|
2015-04-18 21:44:40 +03:00
|
|
|
|
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
|
2016-07-27 19:38:57 +05:30
|
|
|
@total_price = Ticket.total_price(@conference, current_user, paid: true)
|
|
|
|
|
@tickets = current_user.ticket_purchases.by_conference(@conference).paid
|
|
|
|
|
@ticket_payments = @tickets.group_by(&:ticket_id)
|
|
|
|
|
@total_quantity = @tickets.group(:ticket_id).sum(:quantity)
|
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
|
2017-04-03 18:34:37 +03:00
|
|
|
flash.now[: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
|
2017-04-03 18:34:37 +03:00
|
|
|
flash.now[: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
|
2017-04-06 23:19:58 -04:00
|
|
|
params.require(:registration)
|
|
|
|
|
.permit(
|
2014-08-22 15:08:54 +02:00
|
|
|
: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
|