osem-fcy/app/controllers/conference_registration_controller.rb

125 lines
4.9 KiB
Ruby
Raw Normal View History

2013-01-07 09:04:09 +01:00
class ConferenceRegistrationController < ApplicationController
before_filter :verify_user
2014-08-12 11:51:59 +03:00
load_resource :conference, find_by: :short_title
authorize_resource :conference_registration, class: Registration
2013-01-07 09:04:09 +01:00
def register
2014-06-26 15:43:24 +03:00
@workshops = @conference.events.where('require_registration = ? AND state LIKE ?',
true, 'confirmed')
2014-06-23 19:07:50 +03:00
@user = current_user
2014-06-26 15:43:24 +03:00
@registration = @user.registrations.where(conference_id: @conference.id).first
2013-01-07 09:04:09 +01:00
@registered = true
if @registration.nil?
@registered = false
2014-06-26 15:43:24 +03:00
@registration = @user.registrations.new(conference_id: @conference.id)
2013-01-07 09:04:09 +01:00
end
# Check if there's an existing SupporterRegistration for this email and link it when appropriate
2014-06-26 15:43:24 +03:00
@registration.supporter_registration ||= @conference.supporter_registrations.where(
email: @user.email).first
@registration.supporter_registration ||= SupporterRegistration.new(conference_id: @conference.id)
2013-01-07 09:04:09 +01:00
end
2013-01-14 08:49:49 +01:00
# TODO this is ugly
2013-01-07 09:04:09 +01:00
def update
2014-06-23 19:07:50 +03:00
user = current_user
2014-08-12 11:51:59 +03:00
registration = user.registrations.where(conference_id: @conference.id).first
2013-01-07 09:04:09 +01:00
update_registration = true
# First verify that the supporter code is legit
2014-06-26 15:43:24 +03:00
if !params[:registration][:supporter_registration_attributes].nil? &&
!params[:registration][:supporter_registration_attributes][:code].empty?
regs = conference.supporter_registrations.where(
code: params[:registration][:supporter_registration_attributes][:code])
if regs.count != 0
2014-06-26 15:43:24 +03:00
if regs.where(email: user.email).count == 0
2014-08-12 11:51:59 +03:00
redirect_to(conference_register_path(conference_id: @conference.short_title),
2014-06-26 15:43:24 +03:00
alert: "This code is already in use.
Please contact #{conference.contact.email} for assistance.")
return
end
end
end
2013-01-07 09:04:09 +01:00
begin
if registration.nil?
update_registration = false
2014-06-23 19:07:50 +03:00
user.update_attributes(registration_params[:user_attributes])
params[:registration].delete :user_attributes
supporter_reg = params[:registration][:supporter_registration_attributes]
params[:registration].delete :supporter_registration_attributes
2014-06-23 19:07:50 +03:00
registration = user.registrations.new(registration_params)
2014-08-12 11:51:59 +03:00
if @conference.use_supporter_levels? && !supporter_reg.nil?
if !supporter_reg[:id].blank?
2014-06-26 15:43:24 +03:00
# Means that their supporter registration was entered ahead of time, by an admin
registration.supporter_registration = SupporterRegistration.find(supporter_reg[:id])
2014-06-23 19:07:50 +03:00
if registration.supporter_registration.email != user.email
2014-06-26 15:43:24 +03:00
raise 'Invalid code'
end
else
2014-08-12 11:51:59 +03:00
registration.supporter_registration = @conference.
supporter_registrations.new(registration_params[:supporter_registration_attributes])
end
end
2014-08-12 11:51:59 +03:00
registration.conference_id = @conference.id
2013-01-07 09:04:09 +01:00
registration.save!
if user.subscriptions.where(conference: @conference).blank?
subscription = Subscription.new(conference_id: @conference.id, user_id: user.id)
redirect_message = subscription.save ? 'You are now Registered and will be receiving Email Notifications.' : 'You are now Registered.'
end
2013-01-07 09:04:09 +01:00
else
registration.update_attributes!(registration_params)
2013-01-07 09:04:09 +01:00
end
2014-07-21 14:37:26 +02:00
rescue => e
2014-06-26 15:43:24 +03:00
Rails.logger.debug e.backtrace.join('\n')
2014-08-12 11:51:59 +03:00
redirect_to(conference_register_path(conference_id: @conference.short_title),
2014-06-26 15:43:24 +03:00
alert: 'Registration failed:' + e.message)
2013-01-07 09:04:09 +01:00
return
end
if update_registration
2014-06-26 15:43:24 +03:00
redirect_message = 'Registration updated.'
2013-01-14 08:49:49 +01:00
else
# Track ahoy event
ahoy.track 'Registered', title: 'New registration'
2014-08-12 11:51:59 +03:00
if @conference.email_settings.send_on_registration?
Mailbot.delay.registration_mail(@conference, current_user)
end
2013-01-07 09:04:09 +01:00
end
2014-08-12 11:51:59 +03:00
redirect_to(conference_register_path(conference_id: @conference.short_title),
2014-06-26 15:43:24 +03:00
notice: redirect_message)
2013-01-07 09:04:09 +01:00
end
def unregister
2014-06-23 19:07:50 +03:00
user = current_user
2014-08-12 11:51:59 +03:00
registration = user.registrations.where(conference_id: @conference.id).first
subscription = user.subscriptions.where(conference: @conference)
unless subscription.blank?
subscription.first.destroy
end
2013-01-07 09:04:09 +01:00
registration.destroy
redirect_to :root
end
protected
def registration_params
params.require(:registration).
permit(
:conference_id, :attending_social_events, :attending_with_partner,
:using_affiliated_lodging, :arrival, :departure,
:other_dietary_choice, :handicapped_access_required, :dietary_choice_id,
2014-06-30 19:15:06 +03:00
:volunteer, :other_special_needs,
social_event_ids: [],
vchoice_ids: [],
qanswer_ids: [],
qanswers_attributes: [],
2014-06-23 19:07:50 +03:00
user_attributes: [
:id, :name],
supporter_registration_attributes: [
:id, :supporter_level_id, :code
])
end
2013-01-07 09:04:09 +01:00
end