Class: Caboose::EventsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/events_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_index, #admin_json, #admin_json_single, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject

POST /admin/calendars/:calendar_id/events



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/caboose/events_controller.rb', line 28

def admin_add
  return unless user_is_allowed('calendars', 'edit')
  
  resp = StdClass.new      
  event = CalendarEvent.new
  event.calendar_id = params[:calendar_id]
  event.name = params[:name]
  event.begin_date = DateTime.iso8601("#{params[:begin_date]}T10:00:00-05:00") 
  event.end_date   = DateTime.iso8601("#{params[:begin_date]}T10:00:00-05:00")      
  event.all_day = true
        
  if event.name.nil? || event.name.strip.length == 0
    resp.error = "Please enter an event name."
  else
    event.save
    resp.redirect = "/admin/calendars/#{event.calendar_id}/events/#{event.id}"
  end
  render :json => resp
end

#admin_deleteObject

DELETE /admin/calendars/:calendar_id/events/:id



91
92
93
94
95
96
# File 'app/controllers/caboose/events_controller.rb', line 91

def admin_delete
  return unless user_is_allowed('calendars', 'delete')
  CalendarEvent.find(params[:id]).destroy      
  resp = StdClass.new({ 'redirect' => "/admin/calendars" })                  
  render :json => resp
end

#admin_editObject

GET /admin/calendars/:calendar_id/events/:id



8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/caboose/events_controller.rb', line 8

def admin_edit
  return unless user_is_allowed('calendars', 'edit')
  @event = CalendarEvent.find(params[:id])
  if @event.calendar_event_group_id.nil?
    g = CalendarEventGroup.create
    @event.calendar_event_group_id = g.id
    @event.save
  end
  render :layout => 'caboose/modal'
end

#admin_newObject

GET /admin/calendars/:calendar_id/events/new



20
21
22
23
24
25
# File 'app/controllers/caboose/events_controller.rb', line 20

def admin_new
  return unless user_is_allowed('calendars', 'edit')
  @calendar = Calendar.find(params[:calendar_id])
  @begin_date = DateTime.iso8601(params[:begin_date])
  render :layout => 'caboose/modal'
end

#admin_updateObject

PUT /admin/calendars/:calendar_id/events/:id



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/caboose/events_controller.rb', line 49

def admin_update
  return unless user_is_allowed('calendars', 'edit')
  
  resp = StdClass.new({ 'attributes' => {} })
  event = CalendarEvent.find(params[:id])
  
  save = true      
  params.each do |name, value|
    case name
      when 'name'         then event.name         = value
      when 'location'     then event.location     = value
      when 'description'  then event.description  = value
      when 'all_day'      then event.all_day      = value
      when 'begin_date'              
        t = event.begin_date ? event.begin_date.strftime('%H:%M %z') : '10:00 -0500'
        event.begin_date = DateTime.strptime("#{value} #{t}", '%m/%d/%Y %H:%M %z')                        
      when 'begin_time'
        d = event.begin_date ? event.begin_date.strftime('%Y-%m-%d') : DateTime.now.strftime('%Y-%m-%d')
        event.begin_date = DateTime.strptime("#{d} #{value}", '%Y-%m-%d %I:%M %P')
      when 'end_date'
        t = event.end_date ? event.end_date.strftime('%H:%M %z') : '10:00 -0500'
        event.end_date = DateTime.strptime("#{value} #{t}", '%m/%d/%Y %H:%M %z')
      when 'end_time'
        d = event.end_date ? event.end_date.strftime('%Y-%m-%d') : DateTime.now.strftime('%Y-%m-%d')
        event.end_date = DateTime.strptime("#{d} #{value}", '%Y-%m-%d %I:%M %P')
      when 'repeats'
        g = event.calendar_event_group
        if value == true || value.to_i == 1
          g.date_start = event.begin_date.to_date if g.date_start.nil?
          g.date_end   = event.end_date.to_date   if g.date_end.nil?
          g.save              
        end
        event.repeats = value              
      
    end
  end

  resp.success = save && event.save
  render :json => resp
end