Improvements in proposal controller

`if..else` is better than unless with return.
On failed update render `edit` template and not render `new` template.
On failed save redirect to index path and not render `new` template.
Change `destory` to `withdraw`, cause `destroy` makes DELETE request when
we are updating the event record.
Check for success for save in withdraw (save can fail for reasons other than validation).
This commit is contained in:
Aditya Prakash 2016-03-24 00:24:52 +05:30
parent 10ef898a0d
commit dcb98c49f9
3 changed files with 36 additions and 34 deletions

View file

@ -24,13 +24,14 @@ class ProposalController < ApplicationController
end
def edit
authorize! :edit, @event
@url = conference_program_proposal_path(@conference.short_title, params[:id])
end
def create
@url = conference_program_proposal_index_path(@conference.short_title)
# We allow proposal submission and sign up on same page.
# If user is not signed in then first create new user and then sign them in
unless current_user
@user = User.new(user_params)
if @user.save
@ -47,39 +48,38 @@ class ProposalController < ApplicationController
@event = Event.new(event_params)
@event.program = @program
# User which creates the proposal is both `submitter` and `speaker` of proposal
# by default.
# TODO: Allow submitter to add speakers to proposals
@event.event_users.new(user: current_user,
event_role: 'submitter')
@event.event_users.new(user: current_user,
event_role: 'speaker')
authorize! :new, @event
unless @event.save
if @event.save
ahoy.track 'Event submission', title: 'New submission'
redirect_to conference_program_proposal_index_path(@conference.short_title), notice: 'Proposal was successfully submitted.'
else
flash[:error] = "Could not submit proposal: #{@event.errors.full_messages.join(', ')}"
render action: 'new'
return
end
ahoy.track 'Event submission', title: 'New submission'
redirect_to conference_program_proposal_index_path(@conference.short_title), notice: 'Proposal was successfully submitted.'
end
def update
authorize! :update, @event
@url = conference_program_proposal_path(@conference.short_title, params[:id])
if !@event.update(event_params)
if @event.update(event_params)
redirect_to(conference_program_proposal_index_path(conference_id: @conference.short_title),
notice: 'Proposal was successfully updated.')
else
flash[:error] = "Could not update proposal: #{@event.errors.full_messages.join(', ')}"
render action: 'new'
return
render action: 'edit'
end
redirect_to conference_program_proposal_index_path(conference_id: @conference.short_title),
notice: 'Proposal was successfully updated.'
end
def destroy
authorize! :destroy, @event
def withdraw
authorize! :update, @event
@url = conference_program_proposal_path(@conference.short_title, params[:id])
begin
@ -89,9 +89,13 @@ class ProposalController < ApplicationController
return
end
@event.save(validate: false)
redirect_to conference_program_proposal_index_path(conference_id: @conference.short_title),
notice: 'Proposal was successfully withdrawn.'
if @event.save
redirect_to conference_program_proposal_index_path(conference_id: @conference.short_title),
notice: 'Proposal was successfully withdrawn.'
else
redirect_to conference_program_proposal_index_path(conference_id: @conference.short_title),
error: "Could not withdraw proposal: #{@event.errors.full_messages.join(', ')}"
end
end
def confirm
@ -99,15 +103,15 @@ class ProposalController < ApplicationController
@url = conference_program_proposal_path(@conference.short_title, params[:id])
begin
@event.confirm!
@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'
unless @event.save
redirect_to conference_program_proposal_index_path(conference_id: @conference.short_title),
error: "Could not confirm proposal: #{@event.errors.full_messages.join(', ')}"
return
end
@ -132,14 +136,13 @@ class ProposalController < ApplicationController
return
end
if !@event.save
flash[:error] = "Could not re-submit proposal: #{@event.errors.full_messages.join(', ')}"
render action: 'new'
return
if @event.save
redirect_to(conference_program_proposal_index_path(conference_id: @conference.short_title),
notice: "The proposal was re-submitted. The #{@conference.short_title} organizers will review it again.")
else
redirect_to conference_program_proposal_index_path(conference_id: @conference.short_title),
error: "Could not re-submit proposal: #{@event.errors.full_messages.join(', ')}"
end
redirect_to conference_program_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
@ -152,5 +155,3 @@ class ProposalController < ApplicationController
params.require(:user).permit(:email, :password, :password_confirmation, :username)
end
end
# FIXME: Introduce strong_parameters pronto!