Refactored proposal. Enabled cancancan

This commit is contained in:
Henne Vogelsang 2014-07-23 16:24:05 +02:00
parent 0ae7991943
commit c982cbb88a
13 changed files with 204 additions and 231 deletions

View file

@ -1,203 +1,162 @@
class ProposalController < ApplicationController
before_filter :verify_user, except: [:show]
before_filter :setup
before_filter :verify_access, only: [:edit, :update, :destroy, :confirm, :restart]
def setup
@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])
@url = conference_proposal_index_path(@conference.short_title)
@event_types = @conference.event_types
end
def verify_access
if params.has_key? :proposal_id
params[:id] = params[:proposal_id]
end
begin
if !organizer_or_admin?
@event = @user.events.find(params[:id])
else
@event = Event.find(params[:id])
end
rescue => e
Rails.logger.debug("Proposal failure in verify_access: #{e.message}")
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: 'Invalid or uneditable proposal.')
end
end
before_action :set_conference, only: [:show]
before_action :set_event, only: [:show, :edit, :update, :destroy, :confirm, :restart]
def index
@events = @user.proposals @conference
@events = current_user.proposals(@conference)
end
def destroy
proposal = @user.events.find_by_id(params[:id])
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
def show
authorize! :show, @event
# FIXME: We should show more than the first speaker
@speaker = @event.speakers.first || @event.submitter
end
def new
authorize! :new, Event
@url = conference_proposal_index_path(@conference.short_title)
@event = Event.new
end
def edit
authorize! :edit, @event
@url = conference_proposal_path(@conference.short_title, params[:id])
@event_types = @conference.event_types
@attachments = @event.event_attachments
if @event.nil?
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: 'Invalid or uneditable proposal.')
end
end
def update
session[:return_to] ||= request.referer
submitter = params[:user]
params[:event].delete :users_attributes
params[:event].delete :user
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?
redirect_to edit_conference_proposal_path(@conference.short_title, @event),
alert: 'Your biography cannot be blank'
return
end
if submitter[:name] != @user.name || submitter[:biography] != @user.biography
@user.update_attributes(submitter)
end
event = Event.find_by_id(params[:id])
begin
event.update_attributes!(params[:event])
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: "'#{event.title}' was successfully updated.")
rescue => e
redirect_to edit_conference_proposal_path(@conference.short_title, @event), alert: e.message
end
end
def create
user = current_user
session[:return_to] ||= request.referer
authorize! :create, Event
@url = conference_proposal_index_path(@conference.short_title)
event_params = params[:event]
submitter = params[:user]
params[:event].delete :user
@event = Event.new(event_params)
@event = Event.new(params[:event])
@event.conference = @conference
if submitter[:name].blank?
flash[:error] = 'Your public name cannot be blank.'
render action: 'new'
return
end
if submitter[:biography].blank?
flash[:error] = 'Your biography cannot be blank.'
render action: 'new'
return
end
# First, update the submitter's info, if they've changed anything
if submitter[:name] != user.name || submitter[:biography] != user.biography
user.update_attributes(submitter)
current_user.assign_attributes(params[:user])
if current_user.changed?
current_user.save
end
@event.event_users.new(user: user,
@event.event_users.new(user: current_user,
event_role: 'submitter')
@event.event_users.new(user: user,
@event.event_users.new(user: current_user,
event_role: 'speaker')
begin
@event.save!
rescue => e
@url = conference_proposal_index_path(@conference.short_title)
@event_types = @conference.event_types
@user = current_user
flash[:error] = "Could not submit proposal: #{e.message}"
if !@event.save
flash[:error] = "Could not submit proposal: #{@event.errors.full_messages.join(', ')}"
render action: 'new'
return
end
registration = user.registrations.where(conference_id: @conference.id).first
registration = current_user.registrations.where(conference_id: @conference.id).first
ahoy.track 'Event submission', title: 'New submission'
if registration.nil?
redirect_to(register_conference_path(@conference.short_title),
notice: 'Event was successfully submitted.\
You probably want to register for the conference now!')
alert: 'Event was successfully submitted.
You should register for the conference now.')
else
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: 'Event was successfully submitted.')
end
end
def show
@event = Event.find(params[:id])
@speaker = @event.speakers.first || @event.submitter
def update
authorize! :update, @event
@url = conference_proposal_path(@conference.short_title, params[:id])
# First, update the submitter's info, if they've changed anything
current_user.assign_attributes(params[:user])
if current_user.changed?
current_user.save
end
# FIXME: Hmmmmm
params[:event].delete :users_attributes
params[:event].delete :user
if !@event.update(params[:event])
flash[:error] = "Could not update proposal: #{@event.errors.full_messages.join(', ')}"
render action: 'new'
return
end
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: "Proposal was successfully updated.")
end
def destroy
authorize! :destroy, @event
@url = conference_proposal_path(@conference.short_title, params[:id])
begin
@event.withdraw
rescue Transitions::InvalidTransition
redirect_to(:back, error: "Event can't be withdrawn")
return
end
@event.save(validate: false)
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: "Proposal was successfully withdrawn.")
end
def confirm
if @event.transition_possible? :confirm
begin
@event.confirm!
rescue Transitions::InvalidTransition => e
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: "Event was NOT confirmed: #{e.message}")
return
end
authorize! :update, @event
@url = conference_proposal_path(@conference.short_title, params[:id])
if !@conference.user_registered?(current_user)
redirect_to(register_conference_path(@conference.short_title),
notice: 'Event was confirmed. Please register to attend the conference.')
return
end
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: 'Event was confirmed.')
else
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: 'Event was NOT confirmed!')
begin
@event.confirm!
rescue Transitions::InvalidTransition
redirect_to(:back, error: "Event can't be confirmed")
return
end
if !@event.save
flash[:error] = "Could not confirm proposal: #{@event.errors.full_messages.join(', ')}"
render action: 'new'
return
end
if !@conference.user_registered?(current_user)
redirect_to(register_conference_path(@conference.short_title),
alert: 'The proposal was confirmed. Please register to attend the conference.')
return
end
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: 'The proposal was confirmed.')
end
def restart
if @event.transition_possible? :restart
begin
@event.restart
@event.save
rescue Transitions::InvalidTransition => e
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
alert: "Event was NOT restarted: #{e.message}")
return
end
# Success
authorize! :update, @event
@url = conference_proposal_path(@conference.short_title, params[:id])
begin
@event.restart
rescue Transitions::InvalidTransition
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!')
error: "The proposal can't be re-submitted.")
return
end
if !@event.save
flash[:error] = "Could not re-submit proposal: #{@event.errors.full_messages.join(', ')}"
render action: 'new'
return
end
redirect_to(conference_proposal_index_path(conference_id: @conference.short_title),
notice: "The proposal was re-submitted. The #{@conference.short_title} organizers will review it again.")
end
private
def set_conference
@conference = Conference.find_by(short_title: params[:conference_id])
end
def set_event
@event = Event.find(params[:id])
end
end