osem-fcy/app/controllers/admin/registrations_controller.rb

129 lines
5.1 KiB
Ruby
Raw Normal View History

2014-06-23 19:07:50 +03:00
module Admin
class RegistrationsController < ApplicationController
before_filter :verify_organizer
2013-01-07 09:04:09 +01:00
2014-06-23 19:07:50 +03:00
def index
session[:return_to] ||= request.referer
@pdf_filename = "#{@conference.title}.pdf"
@registrations = @conference.registrations.includes(:user).order("registrations.created_at ASC")
@attended = @conference.registrations.where("attended = ?", true).count
@headers = %w[name email nickname other_needs arrival departure attended]
end
2013-07-12 10:45:40 +03:00
2014-06-23 19:07:50 +03:00
def change_field
@registration = Registration.find(params[:id])
field = params[:view_field]
if @registration.send(field.to_sym)
@registration.update_attribute(:"#{field}",0)
else
@registration.update_attribute(:"#{field}",1)
end
2013-07-12 10:45:40 +03:00
2014-06-23 19:07:50 +03:00
redirect_to admin_conference_registrations_path(@conference.short_title)
flash[:notice] = "Updated '#{params[:view_field]}' => #{@registration.attended} for
#{(User.where("id = ?", @registration.user_id).first).email}"
end
2013-07-15 13:11:18 +03:00
2014-06-23 19:07:50 +03:00
def edit
@registration = @conference.registrations.where("id = ?", params[:id]).first
@user = User.where("id = ?", @registration.user_id).first
end
2013-07-12 11:43:31 +03:00
2014-06-23 19:07:50 +03:00
def update
@registration = @conference.registrations.where("id = ?", params[:id]).first
@user = User.where("id = ?", @registration.user_id).first
begin
@user.update_attributes!(params[:registration][:user_attributes])
params[:registration].delete :user_attributes
if params[:registration][:supporter_registration]
@registration.supporter_registration.update_attributes(params[:registration][:supporter_registration_attributes])
params[:registration].delete :supporter_registration_attributes
end
@registration.update_attributes!(params[:registration])
flash[:success] = "Successfully updated registration for #{@user.name} #{@user.email}"
redirect_to(admin_conference_registrations_path(@conference.short_title))
rescue Exception => e
Rails.logger.debug e.backtrace.join("\n")
redirect_to(admin_conference_registrations_path(@conference.short_title),
alert: 'Failed to update registration:' + e.message)
return
end
2013-07-12 11:43:31 +03:00
end
2014-06-23 19:07:50 +03:00
def new
@user = User.new
@registration = @user.registrations.new
@supporter_registration = @conference.supporter_registrations.new
end
def create
@user = User.prepare(user_params['user'])
@registration = Registration.new
unless @user.save
render action: 'new'
return
end
if @conference.user_registered? @user # Check if user is already registered to the conference
2013-07-12 17:40:27 +03:00
redirect_to admin_conference_registrations_path(@conference.short_title)
2014-06-23 19:07:50 +03:00
flash[:alert] = "#{@user.email} is already registred!"
return
end
2014-06-23 19:07:50 +03:00
# Build registration
@registration = @user.registrations.build
@registration.attributes = registration_params
@registration.conference_id = @conference.id
@registration.attended = true
2014-06-23 19:07:50 +03:00
if params[:registration][:supporter_registration]
@supporter_registration = @registration.build_supporter_registration
@supporter_registration.attributes = supporter_params['supporter_registration']
@supporter_registration.conference_id = @conference.id
else
# If we render action: 'new' we need the @supporter_registration variable to be set
@supporter_registration = @conference.supporter_registrations.new
end
if @registration.save
flash[:success] = "Successfully created new registration for #{@user.email}."
redirect_to admin_conference_registrations_path(@conference.short_title)
else
render action: 'new'
end
end
2013-07-12 17:40:27 +03:00
2014-06-23 19:07:50 +03:00
def destroy
if has_role?(current_user, "Admin")
registration = @conference.registrations.where(:id => params[:id]).first
user = User.where("id = ?", registration.user_id).first
begin registration.destroy
redirect_to admin_conference_registrations_path
flash[:notice] = "Deleted registration for #{user.name} #{user.email}"
rescue Exception => e
Rails.logger.debug e.backtrace.join("\n")
redirect_to(admin_conference_registrations_path(@conference.short_title),
alert: 'Failed to delete registration:' + e.message)
return
end
else
2014-05-15 21:04:08 +03:00
redirect_to(admin_conference_registrations_path(@conference.short_title),
2014-06-23 19:07:50 +03:00
alert: 'You must be an admin to delete a registration.')
end
2013-07-12 10:54:59 +03:00
end
2014-06-23 19:07:50 +03:00
protected
def registration_params
params.require(:registration).permit(:attending_with_partner, :using_affiliated_lodging, :handicapped_access_required, :other_special_needs, :attended)
end
def user_params
params.require(:registration).permit(user: [:email, :name, :nickname, :affiliation])
end
def supporter_params
params.require(:registration).permit(supporter_registration: [:supporter_level_id, :code])
end
2013-07-12 10:54:59 +03:00
end
2014-06-23 19:07:50 +03:00
end