Files
railsgoat/app/controllers/schedule_controller.rb
T
Fred Nixon ea8e9901f4 On branch strong-params
Your branch is behind 'origin/strong-params' by 1 commit, and can be fast-forwarded.

I'll pull to catch up after this commit
Change code to whitelist params
Remove attr_accessible lines
Add strong_params to Gemfile, since this branch is still on Rails 3
Mixin to ActiveRecord::Base ActiveModel::ForbiddenAttributesProtection
Use an initializer for the mixin
2014-12-05 15:04:01 -05:00

66 lines
1.5 KiB
Ruby

class ScheduleController < ApplicationController
def create
message = false
if params[:schedule][:event_type] == "pto"
sched = Schedule.new(schedule_params)
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 => message ? "success" : "failure" }}
end
end
def get_pto_schedule
begin
schedules = current_user.paid_time_off.schedule
jfs = []
schedules.each do |s|
hash = Hash.new
hash[:id] = s[:id]
hash[:title] = s[:event_name]
hash[:start] = s[:date_begin]
hash[:end] = s[:date_end]
jfs << hash
end
rescue
end
respond_to do |format|
format.json do
render :json => jfs.to_json
end
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
private
def schedule_params
params.require(:schedule).permit(:date_begin, :date_end, :event_desc, :event_name, :event_type)
end
end