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 @@