From ff36b0fab5ce932d1739df45c7c25b1a7287b4fd Mon Sep 17 00:00:00 2001 From: Ken Johnson Date: Thu, 30 May 2013 12:11:50 -0400 Subject: [PATCH] working way to update your scheduled PTO --- app/controllers/schedule_controller.rb | 33 +++++++++++++++++++++++++- app/models/schedule.rb | 2 ++ app/views/paid_time_off/index.html.erb | 31 ++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/app/controllers/schedule_controller.rb b/app/controllers/schedule_controller.rb index f33cea5..34de39f 100644 --- a/app/controllers/schedule_controller.rb +++ b/app/controllers/schedule_controller.rb @@ -1,7 +1,38 @@ class ScheduleController < ApplicationController def create + message = false + + if params[:schedule][:event_type] == "pto" + sched = Schedule.new(params[:schedule]) + sched.date_begin, sched.date_end = format_schedule_date(params[:date_range1]) + sched.user_id = current_user.user_id + a = sched.date_end + if sched.save + message = true + end + end + respond_to do |format| - format.json {render :json => {:msg => "success"}} + format.json {render :json => {:msg => message ? "success" : "failure" }} end end + + private + + # Returns a two part array consisting of dates + # First value is the begin date and the second is the end date + def format_schedule_date(date_array) + begin + vals = [] + return vals if date_array.empty? + date_array.split('-').each do |s| + date = Date.strptime(s.strip, '%m/%d/%Y') + vals <<(date) + end + rescue ArgumentError + return [] + end + return vals + end + end diff --git a/app/models/schedule.rb b/app/models/schedule.rb index f410c15..b423446 100644 --- a/app/models/schedule.rb +++ b/app/models/schedule.rb @@ -1,4 +1,6 @@ class Schedule < ActiveRecord::Base attr_accessible :date_begin, :date_end, :event_desc, :event_name, :event_type, :user_id belongs_to :paid_time_off + + validates_presence_of :date_begin, :date_end end diff --git a/app/views/paid_time_off/index.html.erb b/app/views/paid_time_off/index.html.erb index 59685d0..4a20cd4 100644 --- a/app/views/paid_time_off/index.html.erb +++ b/app/views/paid_time_off/index.html.erb @@ -1,5 +1,29 @@
+
+
+ +
+
+
+
+ +
+
@@ -206,8 +230,11 @@ $("#cal_update_submit").click(function(event) { data: valuesToSubmit, type: "POST", success: function(response) { - $('#success').show(500).delay(1500).fadeOut(); - + if (response.msg == "failure") { + $('#failure').show(500).delay(1500).fadeOut(); + } else { + $('#success').show(500).delay(1500).fadeOut(); + } }, error: function(event) { $('#failure').show(500).delay(1500).fadeOut();