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

82 lines
2.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2014-06-23 19:07:50 +03:00
module Admin
class RegistrationsController < Admin::BaseController
2014-08-12 11:51:59 +03:00
load_and_authorize_resource :conference, find_by: :short_title
load_and_authorize_resource :registration, through: :conference
before_action :set_user, except: [:index]
2013-01-07 09:04:09 +01:00
2014-06-23 19:07:50 +03:00
def index
2014-08-12 11:51:59 +03:00
authorize! :show, Registration.new(conference_id: @conference.id)
2014-06-23 19:07:50 +03:00
@pdf_filename = "#{@conference.title}.pdf"
@registrations = @conference.registrations.includes(:user).order('registrations.created_at ASC')
2014-06-24 12:14:53 +03:00
@attended = @conference.registrations.where('attended = ?', true).count
@registration_distribution = @conference.registration_distribution
@affiliation_distribution = @conference.affiliation_distribution
@code_of_conduct = @conference.code_of_conduct.present?
respond_to do |format|
format.html
format.json do
render json: RegistrationDatatable.new(view_context, conference: @conference)
end
end
2014-06-23 19:07:50 +03:00
end
2013-07-12 10:45:40 +03:00
def edit; end
2013-07-12 11:43:31 +03:00
2014-06-23 19:07:50 +03:00
def update
@user.update_attributes(user_params)
2016-05-01 18:50:07 +03:00
@registration.update_attributes(registration_params)
2014-06-23 19:07:50 +03:00
if @registration.save
redirect_to admin_conference_registrations_path(@conference.short_title),
2015-10-06 14:15:38 +03:00
notice: "Successfully updated registration for #{@registration.user.email}!"
2014-06-23 19:07:50 +03:00
else
flash.now[:error] = "An error prohibited the Registration for #{@registration.user.email}: "\
"#{@registration.errors.full_messages.join('. ')}."
render :edit
end
end
2013-07-12 17:40:27 +03:00
2014-06-23 19:07:50 +03:00
def destroy
2014-08-12 11:51:59 +03:00
if can? :destroy, @registration
@registration.destroy
redirect_to admin_conference_registrations_path(@conference.short_title),
notice: "Deleted registration for #{@user.name}!"
2014-06-23 19:07:50 +03:00
else
redirect_to admin_conference_registrations_path(@conference.short_title),
error: '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
2015-07-10 15:40:11 +02:00
def toggle_attendance
@registration.attended = !@registration.attended
if @registration.save
2015-07-10 15:40:11 +02:00
head :ok
else
2015-07-10 15:40:11 +02:00
head :unprocessable_entity
end
end
2015-10-28 18:05:15 +02:00
private
2014-06-24 12:14:53 +03:00
def set_user
@user = User.find_by(id: @registration.user_id)
2014-06-24 12:14:53 +03:00
end
def user_params
params.require(:user).permit(:name, :nickname, :affiliation)
end
def registration_params
params.require(:registration).permit(
:user_id, :conference_id, :arrival, :departure, :attended,
:volunteer, :other_special_needs, :accepted_code_of_conduct,
vchoice_ids: [], qanswer_ids: [], qanswers_attributes: [], event_ids: []
)
2014-06-24 12:14:53 +03:00
end
2013-07-12 10:54:59 +03:00
end
2014-06-23 19:07:50 +03:00
end