Refactoring event states

This commit is contained in:
Chrisbr 2014-06-06 10:23:01 +02:00
parent a95a03c7e5
commit e5b588df81
12 changed files with 148 additions and 87 deletions

View file

@ -119,13 +119,24 @@ class Admin::EventsController < ApplicationController
def create
end
def update_state
event = Event.find(params[:id])
if params[:send_mail] == "true" and (event.conference.email_settings.accepted_email_template.nil? or event.conference.email_settings.rejected_email_template.nil?)
redirect_to(admin_conference_events_path(:conference_id => @conference.short_title), :notice => "Update Email Template before Sending Mails") and return
end
event.send(:"#{params[:transition]}!", :send_mail => params[:send_mail])
redirect_to(admin_conference_events_path(:conference_id => @conference.short_title), :notice => "Updated state")
def accept
update_state(params[:id], :accept, 'Event accepted!', true)
end
def confirm
update_state(params[:id], :confirm, 'Event confirmed!')
end
def cancel
update_state(params[:id], :cancel, 'Event canceled!')
end
def reject
update_state(params[:id], :reject, 'Event rejected!', true)
end
def restart
update_state(params[:id], :restart, 'Review started!')
end
def vote
@ -146,4 +157,42 @@ class Admin::EventsController < ApplicationController
format.js
end
end
private
def update_state(id, transition, notice, mail = false)
event = Event.find(id)
if mail
check_mail_settings
end
if event
begin
if mail
event.send(transition,
send_mail: params[:send_mail])
else
event.send(transition)
end
event.save
rescue Transitions::InvalidTransition => e
redirect_to(
admin_conference_events_path(conference_id: @conference.short_title),
notice: "Update state failed. #{e.message}") && return
end
redirect_to(admin_conference_events_path(conference_id: @conference.short_title),
notice: notice)
else
redirect_to(admin_conference_events_path(conference_id: @conference.short_title),
notice: 'Error! Could not find event!')
end
end
def check_mail_settings
if !params[:send_mail].blank? && event &&
event.conference.email_settings.rejected_email_template.nil? &&
event.conference.email_settings.accepted_email_template.nil?
redirect_to(admin_conference_events_path(conference_id: @conference.short_title),
notice: 'Update Email Template before Sending Mails') && return
end
end
end

View file

@ -1,7 +1,7 @@
class ProposalController < ApplicationController
before_filter :verify_user, :except => [:show]
before_filter :setup
before_filter :verify_access, :only => [:edit, :update, :destroy, :confirm]
before_filter :verify_access, only: [:edit, :update, :destroy, :confirm, :restart]
def setup
@person = current_user.person if current_user
@ -34,8 +34,16 @@ class ProposalController < ApplicationController
end
def destroy
@person.withdraw_proposal(params[:id])
redirect_to(conference_proposal_index_path(:conference_id => @conference.short_title), :alert => 'Proposal withdrawn.')
proposal = @person.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
end
def new
@ -144,8 +152,8 @@ class ProposalController < ApplicationController
def confirm
if @event.transition_possible? :confirm
begin
@event.confirm!(:send_mail => params[:send_mail])
rescue Exception => e
@event.confirm!
rescue InvalidTransition => e
redirect_to(conference_proposal_index_path(:conference_id => @conference.short_title), :alert => "Event was NOT confirmed: #{e.message}")
return
end
@ -160,4 +168,24 @@ class ProposalController < ApplicationController
end
end
def restart
@event
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
end