osem-fcy/app/controllers/proposal_controller.rb

204 lines
6.3 KiB
Ruby
Raw Normal View History

2013-01-07 09:04:09 +01:00
class ProposalController < ApplicationController
2014-06-23 19:07:50 +03:00
before_filter :verify_user, except: [:show]
2013-01-16 08:22:20 +01:00
before_filter :setup
2014-06-06 10:23:01 +02:00
before_filter :verify_access, only: [:edit, :update, :destroy, :confirm, :restart]
2013-01-08 08:36:21 +01:00
2013-01-16 08:22:20 +01:00
def setup
2014-06-23 19:07:50 +03:00
@user = current_user if current_user
# FIXME: @conference also comes from verify_user, but we need setup also in show
# which can be accessed anonymusly
@conference = Conference.find_by(short_title: params[:conference_id])
2013-01-16 08:22:20 +01:00
@url = conference_proposal_index_path(@conference.short_title)
@event_types = @conference.event_types
end
def verify_access
2013-01-08 08:51:34 +01:00
if params.has_key? :proposal_id
params[:id] = params[:proposal_id]
end
2013-01-08 08:36:21 +01:00
begin
if !organizer_or_admin?
2014-06-23 19:07:50 +03:00
@event = @user.events.find(params[:id])
2013-01-08 08:36:21 +01:00
else
@event = Event.find(params[:id])
end
2014-07-21 14:37:26 +02:00
rescue => e
Rails.logger.debug("Proposal failure in verify_access: #{e.message}")
2014-06-23 19:07:50 +03:00
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: 'Invalid or uneditable proposal.')
2013-01-08 08:36:21 +01:00
end
end
2013-01-07 09:04:09 +01:00
def index
2014-06-23 19:07:50 +03:00
@events = @user.proposals @conference
2013-01-07 09:04:09 +01:00
end
def destroy
2014-06-23 19:07:50 +03:00
proposal = @user.events.find_by_id(params[:id])
2014-06-06 10:23:01 +02:00
if proposal
proposal.withdraw
proposal.save
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: 'Proposal withdrawn.')
else
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: 'Error! Could not find proposal!')
end
2013-01-07 09:04:09 +01:00
end
def new
@event = Event.new
end
def edit
@url = conference_proposal_path(@conference.short_title, params[:id])
@event_types = @conference.event_types
@attachments = @event.event_attachments
if @event.nil?
2014-06-23 19:07:50 +03:00
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: 'Invalid or uneditable proposal.')
2013-01-07 09:04:09 +01:00
end
end
def update
session[:return_to] ||= request.referer
2014-06-23 19:07:50 +03:00
submitter = params[:user]
2013-01-07 09:04:09 +01:00
2014-06-23 19:07:50 +03:00
params[:event].delete :users_attributes
params[:event].delete :user
2013-01-07 09:04:09 +01:00
2014-06-23 19:07:50 +03:00
if submitter[:name].blank?
redirect_to edit_conference_proposal_path(@conference.short_title, @event),
alert: 'Your name cannot be blank'
return
end
if submitter[:biography].blank?
2014-06-23 19:07:50 +03:00
redirect_to edit_conference_proposal_path(@conference.short_title, @event),
alert: 'Your biography cannot be blank'
return
end
2014-06-23 19:07:50 +03:00
if submitter[:name] != @user.name || submitter[:biography] != @user.biography
@user.update_attributes(submitter)
2013-01-07 09:04:09 +01:00
end
event = Event.find_by_id(params[:id])
begin
event.update_attributes!(params[:event])
2014-06-23 19:07:50 +03:00
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: "'#{event.title}' was successfully updated.")
2014-07-21 14:37:26 +02:00
rescue => e
2014-06-23 19:07:50 +03:00
redirect_to edit_conference_proposal_path(@conference.short_title, @event), alert: e.message
2013-01-07 09:04:09 +01:00
end
end
def create
2014-06-23 19:07:50 +03:00
user = current_user
2013-01-07 09:04:09 +01:00
session[:return_to] ||= request.referer
event_params = params[:event]
2014-06-23 19:07:50 +03:00
submitter = params[:user]
params[:event].delete :user
2013-01-07 09:04:09 +01:00
2013-01-16 08:22:20 +01:00
@event = Event.new(event_params)
@event.conference = @conference
2014-06-23 19:07:50 +03:00
if submitter[:name].blank?
flash[:error] = 'Your public name cannot be blank.'
render action: 'new'
2013-01-16 08:22:20 +01:00
return
end
if submitter[:biography].blank?
2014-06-23 19:07:50 +03:00
flash[:error] = 'Your biography cannot be blank.'
render action: 'new'
2013-01-16 08:22:20 +01:00
return
end
2013-01-07 09:04:09 +01:00
# First, update the submitter's info, if they've changed anything
2014-06-23 19:07:50 +03:00
if submitter[:name] != user.name || submitter[:biography] != user.biography
user.update_attributes(submitter)
2013-01-07 09:04:09 +01:00
end
2014-06-23 19:07:50 +03:00
@event.event_users.new(user: user,
event_role: 'submitter')
@event.event_users.new(user: user,
event_role: 'speaker')
2013-01-07 09:04:09 +01:00
begin
@event.save!
2014-07-21 14:37:26 +02:00
rescue => e
2013-01-07 09:04:09 +01:00
@url = conference_proposal_index_path(@conference.short_title)
@event_types = @conference.event_types
2014-06-23 19:07:50 +03:00
@user = current_user
2013-01-07 09:04:09 +01:00
flash[:error] = "Could not submit proposal: #{e.message}"
2014-06-23 19:07:50 +03:00
render action: 'new'
2013-01-07 09:04:09 +01:00
return
end
2014-06-23 19:07:50 +03:00
registration = user.registrations.where(conference_id: @conference.id).first
ahoy.track 'Event submission', title: 'New submission'
2013-01-07 09:04:09 +01:00
if registration.nil?
2014-06-23 19:07:50 +03:00
redirect_to(register_conference_path(@conference.short_title),
notice: 'Event was successfully submitted.\
You probably want to register for the conference now!')
2013-01-07 09:04:09 +01:00
else
2014-06-23 19:07:50 +03:00
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: 'Event was successfully submitted.')
2013-01-07 09:04:09 +01:00
end
end
def show
@event = Event.find(params[:id])
@speaker = @event.speakers.first || @event.submitter
2013-01-07 09:04:09 +01:00
end
2013-01-08 08:51:34 +01:00
def confirm
if @event.transition_possible? :confirm
begin
2014-06-06 10:23:01 +02:00
@event.confirm!
rescue InvalidTransition => e
2014-06-23 19:07:50 +03:00
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: "Event was NOT confirmed: #{e.message}")
2013-01-08 08:51:34 +01:00
return
end
if !@conference.user_registered?(current_user)
2014-06-23 19:07:50 +03:00
redirect_to(register_conference_path(@conference.short_title),
notice: 'Event was confirmed. Please register to attend the conference.')
2013-01-08 08:51:34 +01:00
return
end
2014-06-23 19:07:50 +03:00
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: 'Event was confirmed.')
2013-01-08 08:51:34 +01:00
else
2014-06-23 19:07:50 +03:00
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: 'Event was NOT confirmed!')
2013-01-08 08:51:34 +01:00
end
end
2014-06-06 10:23:01 +02:00
def restart
if @event.transition_possible? :restart
begin
@event.restart
@event.save
rescue InvalidTransition => e
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: "Event was NOT restarted: #{e.message}")
return
end
# Success
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: 'Event was restarted. Review pending!')
else
# Error
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: 'Event was NOT restarted!')
end
end
2013-01-07 09:04:09 +01:00
end