From 81108290e59f0448c364780b1b4ece2da5ba0a07 Mon Sep 17 00:00:00 2001 From: Ana Date: Thu, 4 Aug 2016 19:20:11 +0200 Subject: [PATCH] Schedule update moved to program update Schedule update updated a program attribute --- app/assets/javascripts/osem-switch.js | 23 ++++++++++++++++++ app/controllers/admin/programs_controller.rb | 24 ++++++++++++------- app/controllers/admin/schedules_controller.rb | 12 ---------- app/views/admin/schedules/show.html.haml | 4 ++-- 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/osem-switch.js b/app/assets/javascripts/osem-switch.js index 35393625..7b97589f 100644 --- a/app/assets/javascripts/osem-switch.js +++ b/app/assets/javascripts/osem-switch.js @@ -11,4 +11,27 @@ $(function () { dataType: 'script' }); }); + + $("[class='switch-checkbox-schedule']").bootstrapSwitch(); + + $('input[class="switch-checkbox-schedule"]').on('switchChange.bootstrapSwitch', function(event, state) { + var url = $(this).attr('url'); + var method = $(this).attr('method'); + + if(state){ + url += $(this).attr('value'); + } + + var callback = function(data) { + if(data.status != 'ok'){ + alert("The schedule couldn't been updated"); + } + } + $.ajax({ + url: url, + type: method, + success: callback, + dataType: 'json' + }); + }); }); diff --git a/app/controllers/admin/programs_controller.rb b/app/controllers/admin/programs_controller.rb index 2cb720b0..64abee65 100644 --- a/app/controllers/admin/programs_controller.rb +++ b/app/controllers/admin/programs_controller.rb @@ -13,20 +13,28 @@ module Admin @program.assign_attributes(program_params) send_mail_on_schedule_public = @program.notify_on_schedule_public? - if @program.update_attributes(program_params) - ConferenceScheduleUpdateMailJob.perform_later(@conference) if send_mail_on_schedule_public - redirect_to admin_conference_program_path(@conference.short_title), - notice: 'The program was successfully updated.' - else - flash[:error] = "Updating program failed. #{@program.errors.to_a.join('. ')}." - render :new + respond_to do |format| + if @program.update_attributes(program_params) + ConferenceScheduleUpdateMailJob.perform_later(@conference) if send_mail_on_schedule_public + format.html do + redirect_to admin_conference_program_path(@conference.short_title), + notice: 'The program was successfully updated.' + end + format.js { render json: { 'status' => 'ok' } } + else + format.html do + flash[:error] = "Updating program failed. #{@program.errors.to_a.join('. ')}." + render :new + end + format.js { render json: { 'status' => 'error' } } + end end end private def program_params - params.require(:program).permit(:rating, :schedule_public, :schedule_fluid, :languages, :blind_voting, :voting_start_date, :voting_end_date) + params.require(:program).permit(:rating, :schedule_public, :schedule_fluid, :languages, :blind_voting, :voting_start_date, :voting_end_date, :selected_schedule_id) end end end diff --git a/app/controllers/admin/schedules_controller.rb b/app/controllers/admin/schedules_controller.rb index ee3357f2..ffb4feb1 100644 --- a/app/controllers/admin/schedules_controller.rb +++ b/app/controllers/admin/schedules_controller.rb @@ -9,8 +9,6 @@ module Admin load_resource :selected_schedule, through: :program, singleton: true load_resource :venue, through: :conference, singleton: true - skip_before_action :verify_authenticity_token, only: [:update] - def index; end def create @@ -29,16 +27,6 @@ module Admin @rooms = (@venue && @venue.rooms.any?) ? @venue.rooms : [Room.new(name: 'No Rooms!', size: 0)] end - def update - if params[:selected_schedule] == 'true' - @program.selected_schedule_id = params[:id].to_i - elsif params[:selected_schedule] == 'false' && (@selected_schedule.id == params[:id].to_i) - @program.selected_schedule_id = nil - end - @program.save - render json: { 'status' => 'ok' } - end - def destroy if @schedule.destroy redirect_to admin_conference_schedules_path(conference_id: @conference.short_title), diff --git a/app/views/admin/schedules/show.html.haml b/app/views/admin/schedules/show.html.haml index a920ecef..70cf158e 100644 --- a/app/views/admin/schedules/show.html.haml +++ b/app/views/admin/schedules/show.html.haml @@ -9,8 +9,8 @@ .col-md-2 Selected schedule = check_box_tag @conference.short_title, @schedule.id, (@schedule.id == @selected_schedule.try(:id)), - method: :put, url: (admin_conference_schedule_path(@conference.short_title, @schedule) + '?selected_schedule='), - class: 'switch-checkbox', data: { size: 'small', + method: :patch, url: (admin_conference_program_path(@conference.short_title) + '?[program][selected_schedule_id]='), + class: 'switch-checkbox-schedule', data: { size: 'small', off_color: 'warning', on_text: 'Yes', off_text: 'No' }