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:
parent
10ef898a0d
commit
dcb98c49f9
3 changed files with 36 additions and 34 deletions
|
|
@ -24,13 +24,14 @@ class ProposalController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
authorize! :edit, @event
|
|
||||||
@url = conference_program_proposal_path(@conference.short_title, params[:id])
|
@url = conference_program_proposal_path(@conference.short_title, params[:id])
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@url = conference_program_proposal_index_path(@conference.short_title)
|
@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
|
unless current_user
|
||||||
@user = User.new(user_params)
|
@user = User.new(user_params)
|
||||||
if @user.save
|
if @user.save
|
||||||
|
|
@ -47,39 +48,38 @@ class ProposalController < ApplicationController
|
||||||
@event = Event.new(event_params)
|
@event = Event.new(event_params)
|
||||||
@event.program = @program
|
@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.event_users.new(user: current_user,
|
||||||
event_role: 'submitter')
|
event_role: 'submitter')
|
||||||
@event.event_users.new(user: current_user,
|
@event.event_users.new(user: current_user,
|
||||||
event_role: 'speaker')
|
event_role: 'speaker')
|
||||||
authorize! :new, @event
|
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(', ')}"
|
flash[:error] = "Could not submit proposal: #{@event.errors.full_messages.join(', ')}"
|
||||||
render action: 'new'
|
render action: 'new'
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
ahoy.track 'Event submission', title: 'New submission'
|
|
||||||
|
|
||||||
redirect_to conference_program_proposal_index_path(@conference.short_title), notice: 'Proposal was successfully submitted.'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
authorize! :update, @event
|
|
||||||
@url = conference_program_proposal_path(@conference.short_title, params[:id])
|
@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(', ')}"
|
flash[:error] = "Could not update proposal: #{@event.errors.full_messages.join(', ')}"
|
||||||
render action: 'new'
|
render action: 'edit'
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
redirect_to conference_program_proposal_index_path(conference_id: @conference.short_title),
|
|
||||||
notice: 'Proposal was successfully updated.'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def withdraw
|
||||||
authorize! :destroy, @event
|
authorize! :update, @event
|
||||||
@url = conference_program_proposal_path(@conference.short_title, params[:id])
|
@url = conference_program_proposal_path(@conference.short_title, params[:id])
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
|
@ -89,9 +89,13 @@ class ProposalController < ApplicationController
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@event.save(validate: false)
|
if @event.save
|
||||||
redirect_to conference_program_proposal_index_path(conference_id: @conference.short_title),
|
redirect_to conference_program_proposal_index_path(conference_id: @conference.short_title),
|
||||||
notice: 'Proposal was successfully withdrawn.'
|
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
|
end
|
||||||
|
|
||||||
def confirm
|
def confirm
|
||||||
|
|
@ -99,15 +103,15 @@ class ProposalController < ApplicationController
|
||||||
@url = conference_program_proposal_path(@conference.short_title, params[:id])
|
@url = conference_program_proposal_path(@conference.short_title, params[:id])
|
||||||
|
|
||||||
begin
|
begin
|
||||||
@event.confirm!
|
@event.confirm
|
||||||
rescue Transitions::InvalidTransition
|
rescue Transitions::InvalidTransition
|
||||||
redirect_to :back, error: "Event can't be confirmed"
|
redirect_to :back, error: "Event can't be confirmed"
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if !@event.save
|
unless @event.save
|
||||||
flash[:error] = "Could not confirm proposal: #{@event.errors.full_messages.join(', ')}"
|
redirect_to conference_program_proposal_index_path(conference_id: @conference.short_title),
|
||||||
render action: 'new'
|
error: "Could not confirm proposal: #{@event.errors.full_messages.join(', ')}"
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -132,14 +136,13 @@ class ProposalController < ApplicationController
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if !@event.save
|
if @event.save
|
||||||
flash[:error] = "Could not re-submit proposal: #{@event.errors.full_messages.join(', ')}"
|
redirect_to(conference_program_proposal_index_path(conference_id: @conference.short_title),
|
||||||
render action: 'new'
|
notice: "The proposal was re-submitted. The #{@conference.short_title} organizers will review it again.")
|
||||||
return
|
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
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
@ -152,5 +155,3 @@ class ProposalController < ApplicationController
|
||||||
params.require(:user).permit(:email, :password, :password_confirmation, :username)
|
params.require(:user).permit(:email, :password, :password_confirmation, :username)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# FIXME: Introduce strong_parameters pronto!
|
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@
|
||||||
method: :patch, class: 'btn btn-mini btn-success', id: "confirm_proposal_#{event.id}"
|
method: :patch, class: 'btn btn-mini btn-success', id: "confirm_proposal_#{event.id}"
|
||||||
|
|
||||||
- if event.transition_possible? :withdraw
|
- if event.transition_possible? :withdraw
|
||||||
= link_to 'Withdraw', conference_program_proposal_path(@conference.short_title, event.id), method: :delete,
|
= link_to 'Withdraw', withdraw_conference_program_proposal_path(@conference.short_title, event.id), method: :patch,
|
||||||
data: { confirm: 'Are you sure you want to withdraw this proposal?' }, class: 'btn btn-mini btn-warning',
|
data: { confirm: 'Are you sure you want to withdraw this proposal?' }, class: 'btn btn-mini btn-warning',
|
||||||
id: "delete_proposal_#{event.id}"
|
id: "delete_proposal_#{event.id}"
|
||||||
- if event.state == 'withdrawn' || event.state == 'rejected'
|
- if event.state == 'withdrawn' || event.state == 'rejected'
|
||||||
|
|
|
||||||
|
|
@ -95,11 +95,12 @@ Osem::Application.routes.draw do
|
||||||
|
|
||||||
resources :conference, only: [:index, :show] do
|
resources :conference, only: [:index, :show] do
|
||||||
resource :program, except: :destroy do
|
resource :program, except: :destroy do
|
||||||
resources :proposal do
|
resources :proposal, except: :destroy do
|
||||||
get 'commercials/render_commercial' => 'commercials#render_commercial'
|
get 'commercials/render_commercial' => 'commercials#render_commercial'
|
||||||
resources :commercials, only: [:create, :update, :destroy]
|
resources :commercials, only: [:create, :update, :destroy]
|
||||||
resources :event_attachment, controller: 'event_attachments'
|
resources :event_attachment, controller: 'event_attachments'
|
||||||
member do
|
member do
|
||||||
|
patch '/withdraw' => 'proposal#withdraw'
|
||||||
patch '/confirm' => 'proposal#confirm'
|
patch '/confirm' => 'proposal#confirm'
|
||||||
patch '/restart' => 'proposal#restart'
|
patch '/restart' => 'proposal#restart'
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue