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