Introduce an Admin::EventScheduleController

- move SchedulesController#update action
- split it into an #create/#update and #destroy action and use these actions in the JS code
This commit is contained in:
Ana 2016-08-01 00:52:52 +02:00
parent a394293500
commit 5001848af7
10 changed files with 108 additions and 81 deletions

View file

@ -8,32 +8,39 @@ var Schedule = {
},
remove: function(element) {
var e = $("#" + element);
var unscheduled = $(".unscheduled-events");
var url = '/admin/conference/' + conference + '/schedule/' + schedule_id;
var params = {
event: e.attr("guid"),
room: "none",
date: "none",
time: "none",
schedule: schedule_id
};
var callback = function(data) {
console.log(data);
e.appendTo(unscheduled);
e.find(".schedule-event-delete-button").hide();
var event_schedule_id = e.attr("event_schedule_id");
if(event_schedule_id != null){
var url = '/admin/conference/' + conference + '/event_schedule/' + event_schedule_id;
var params = {
event: e.attr("guid"),
schedule: schedule_id
};
var callback = function(data) {
console.log(data);
e.attr("event_schedule_id", null);
}
$.ajax({
url: url,
type: 'DELETE',
data: params,
success: callback,
dataType : 'json'
});
}
$.ajax({
url: url,
type: 'PUT',
data: params,
success: callback,
dataType : 'json'
});
var unscheduled = $(".unscheduled-events");
e.appendTo(unscheduled);
e.find(".schedule-event-delete-button").hide();
},
add: function (event_id, room_id, date, time) {
var url = '/admin/conference/' + conference + '/schedule/' + schedule_id;
add: function (event_id, room_id, date, time, event_schedule_id) {
var url = '/admin/conference/' + conference + '/event_schedule';
var type = 'POST'
if(event_schedule_id != null){
type = 'PUT';
url += ('/' + event_schedule_id);
}
var params = {
event: event_id,
schedule: schedule_id,
room: room_id,
date: date,
time: time,
@ -41,11 +48,13 @@ var Schedule = {
};
var callback = function(data) {
console.log(data);
$("#event-" + event_id).find(".schedule-event-delete-button").show();
var e = $("#event-" + event_id);
e.attr("event_schedule_id", data.event_schedule_id);
e.find(".schedule-event-delete-button").show();
}
$.ajax({
url: url,
type: 'PUT',
type: type,
data: params,
success: callback,
dataType : 'json'
@ -84,10 +93,11 @@ $(document).ready( function() {
var myRoom = $(this).attr("room-guid")
var myDate = $(this).attr("date");
var myTime = $(this).attr("hour");
var myEventSchedule = $(ui.draggable).attr("event_schedule_id");
$(ui.draggable).css("left", 0);
$(ui.draggable).css("top", 0);
$(this).css("background-color", "#ffffff");
Schedule.add(myId, myRoom, myDate, myTime);
Schedule.add(myId, myRoom, myDate, myTime, myEventSchedule);
},
over: function(event, ui) {
$(this).css("background-color", "#009ED8");

View file

@ -0,0 +1,47 @@
module Admin
class EventScheduleController < Admin::BaseController
load_and_authorize_resource :event_schedule
def create
event_schedule = EventSchedule.create(get_event_schedule_params(params))
render json: { 'status' => 'ok', event_schedule_id: event_schedule.id }
end
def update
@event_schedule.update(get_event_schedule_params(params))
render json: { 'status' => 'ok', event_schedule_id: @event_schedule.id }
end
def destroy
@event_schedule.destroy if @event_schedule
render json: { 'status' => 'ok' }
end
private
def get_event_schedule_params(params)
error_message = nil
event = Event.where(guid: params[:event]).first
error_message = "Could not find event GUID: #{params[:event]}" if event.nil?
schedule = Schedule.where(id: params[:schedule]).first
error_message = "Could not find schedule: #{params[:schedule]}" if schedule.nil?
room = Room.where(guid: params[:room]).first
error_message = "Could not find room GUID: #{params[:room]}" if room.nil?
error_message = 'Date and time must be present' if params[:date].eql?('') || params[:time].eql?('')
unless error_message.nil?
render json: { 'status' => 'error', 'message' => error_message }, status: 500
return
end
time = "#{params[:date]} #{params[:time]}"
Rails.logger.debug("Loading #{time}")
start_time = DateTime.strptime(time, '%Y-%m-%d %k:%M')
{ schedule: schedule, event: event, room: room, start_time: start_time }
end
end
end

View file

@ -37,48 +37,7 @@ module Admin
@program.selected_schedule_id = nil
end
@program.save!
render json: { 'status' => 'ok' }
return
end
event = Event.where(guid: params[:event]).first
error_message = nil
if event.nil?
error_message = "Could not find event GUID: #{params[:event]}"
end
event_schedule = event.event_schedules.find_by(schedule_id: params[:schedule])
if params[:date] == 'none'
event_schedule.destroy if event_schedule.present?
render json: { 'status' => 'ok' }
return
end
Rails.logger.debug(event_schedule.present?.to_s)
event_schedule = event.event_schedules.new(schedule_id: params[:schedule]) unless event_schedule.present?
room = Room.where(guid: room_params).first
if room.nil?
error_message = "Could not find room GUID: #{params[:room]}"
end
unless error_message.nil?
render json: { 'status' => 'error', 'message' => error_message }, status: 500
return
end
event_schedule.room = room
time = "#{params[:date]} #{params[:time]}"
Rails.logger.debug("Loading #{time}")
# FIXME: Same here as in events_controller.rb. Event timezone should be applied
# only on output
# zone = ActiveSupport::TimeZone::new(@conference.timezone)
# start_time = DateTime.strptime(time + zone.formatted_offset, "%Y-%m-%d %k:%M %Z")
start_time = DateTime.strptime(time, '%Y-%m-%d %k:%M')
event_schedule.start_time = start_time
event_schedule.save!
render json: { 'status' => 'ok' }
end
@ -91,11 +50,5 @@ module Admin
error: "Schedule couldn't be deleted. #{@schedule.errors.full_messages.join('. ')}."
end
end
private
def room_params
params.require(:room)
end
end
end

View file

@ -144,6 +144,7 @@ class Ability
can :manage, Vday, conference_id: conf_ids_for_organizer
can :manage, Program, conference_id: conf_ids_for_organizer
can :manage, Schedule, program: { conference_id: conf_ids_for_organizer }
can :manage, EventSchedule, schedule: { program: { conference_id: conf_ids_for_organizer } }
can :manage, Cfp, program: { conference_id: conf_ids_for_organizer}
can :manage, Event, program: { conference_id: conf_ids_for_organizer}
can :manage, EventType, program: { conference_id: conf_ids_for_organizer}

View file

@ -17,4 +17,5 @@
= time
- event_schedules = room_date_event_schedules.select{ |e| (e.start_time.hour.to_s + e.start_time.strftime(':%M')).eql? time }
- if event_schedules.any?
= render partial: 'event', locals: { event: event_schedules.first.event }
- event_schedule = event_schedules.first
= render partial: 'event', locals: { event: event_schedule.event, event_schedule_id: event_schedule.id}

View file

@ -7,7 +7,8 @@
.schedule-event{ style: "height: #{height}px; background-color: #{color}; color: #{contrast_color(color)}", |
id: "event-#{event.guid}", |
guid: event.guid, |
length: cells_length }
length: cells_length, |
event_schedule_id: event_schedule_id }
.schedule-event-text{ style: "-webkit-line-clamp: #{lines}; height: #{lines * 23}px;"}
%span.schedule-event-delete-button{ onclick: "Schedule.remove(\'event-#{event.guid}\');" } X
= event.title

View file

@ -18,7 +18,7 @@
Unscheduled events
.unscheduled-events
- @unscheduled_events.each do |e|
= render partial: 'event', locals: { event: e }
= render partial: 'event', locals: { event: e, event_schedule_id: nil }
.col-md-10
%ul.nav.nav-tabs
- @dates.each do |date|

View file

@ -23,6 +23,7 @@ Osem::Application.routes.draw do
resources :conference do
resource :contact, except: [:index, :new, :create, :show, :destroy]
resources :schedule, only: [:index, :create, :show, :update, :destroy]
resources :event_schedule, only: [:create, :update, :destroy]
get 'commercials/render_commercial' => 'commercials#render_commercial'
resources :commercials, only: [:index, :create, :update, :destroy]
get '/volunteers_list' => 'volunteers#show'

View file

@ -10,13 +10,15 @@ FactoryGirl.define do
end
(event_schedule.room = create(:room, venue: venue)) unless event_schedule.room.present?
(event_schedule.start_time = program.conference.start_date.to_time) unless event_schedule.start_time.present?
unless program.selected_schedule.present?
schedule = create(:schedule, program: program)
program.schedules << schedule
program.selected_schedule = schedule
program.save!
unless event_schedule.schedule.present?
unless program.selected_schedule.present?
schedule = create(:schedule, program: program)
program.schedules << schedule
program.selected_schedule = schedule
program.save!
end
event_schedule.schedule = program.selected_schedule
end
event_schedule.schedule = program.selected_schedule
end
end
end

View file

@ -41,6 +41,9 @@ describe 'User' do
let!(:my_schedule) { create(:schedule, program: my_conference.program) }
let!(:other_schedule) { create(:schedule, program: conference_public.program) }
let!(:my_event_schedule) { create(:event_schedule, schedule: my_schedule) }
let!(:other_event_schedule) { create(:event_schedule, schedule: other_schedule) }
# Test abilities for not signed in users
context 'when user is not signed in' do
it{ should be_able_to(:index, Conference)}
@ -200,6 +203,8 @@ describe 'User' do
it{ should_not be_able_to(:manage, conference_public.program.cfp) }
it{ should be_able_to(:manage, my_schedule) }
it{ should_not be_able_to(:manage, other_schedule) }
it{ should be_able_to(:manage, my_event_schedule) }
it{ should_not be_able_to(:manage, other_event_schedule) }
it{ should be_able_to(:manage, my_conference.venue) }
it{ should_not be_able_to(:manage, conference_public.venue) }
it{ should be_able_to(:manage, my_conference.lodgings.first) }
@ -266,6 +271,8 @@ describe 'User' do
it{ should_not be_able_to(:manage, conference_public.program.cfp) }
it{ should_not be_able_to(:manage, my_schedule) }
it{ should_not be_able_to(:manage, other_schedule) }
it{ should_not be_able_to(:manage, my_event_schedule) }
it{ should_not be_able_to(:manage, other_event_schedule) }
it{ should_not be_able_to(:manage, my_conference.venue) }
it{ should be_able_to(:show, my_conference.venue) }
it{ should_not be_able_to(:manage, conference_public.venue) }
@ -326,6 +333,8 @@ describe 'User' do
it{ should_not be_able_to(:manage, conference_public.program.cfp) }
it{ should_not be_able_to(:manage, my_schedule) }
it{ should_not be_able_to(:manage, other_schedule) }
it{ should_not be_able_to(:manage, my_event_schedule) }
it{ should_not be_able_to(:manage, other_event_schedule) }
it{ should_not be_able_to(:manage, my_conference.venue) }
it{ should_not be_able_to(:show, my_conference.venue) }
it{ should_not be_able_to(:manage, conference_public.venue) }
@ -386,6 +395,8 @@ describe 'User' do
it{ should_not be_able_to(:manage, conference_public.program.cfp) }
it{ should_not be_able_to(:manage, my_schedule) }
it{ should_not be_able_to(:manage, other_schedule) }
it{ should_not be_able_to(:manage, my_event_schedule) }
it{ should_not be_able_to(:manage, other_event_schedule) }
it{ should_not be_able_to(:manage, my_conference.venue) }
it{ should_not be_able_to(:show, my_conference.venue) }
it{ should_not be_able_to(:manage, conference_public.venue) }