Class: EventsController

Inherits:
SiteController
  • Object
show all
Includes:
Radiant::Pagination::Controller
Defined in:
app/controllers/events_controller.rb

Instance Method Summary collapse

Instance Method Details

#all_eventsObject



83
84
85
# File 'app/controllers/events_controller.rb', line 83

def all_events
  @all_events ||= event_finder.all
end

#calendar_parameter_namesObject



172
173
174
# File 'app/controllers/events_controller.rb', line 172

def calendar_parameter_names
  [:year, :month, :mday, :category, :slug, :calendar_id]
end

#calendar_parametersObject

this is broken down into minimal parts to provide chain points for other extensions that add more ways to filter. eg, to start with, taggable_events



163
164
165
# File 'app/controllers/events_controller.rb', line 163

def calendar_parameters
  url_parts
end

#calendarsObject



68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/events_controller.rb', line 68

def calendars
  return @calendars if @calendars
  if params[:calendar_id]
    @calendars = [Calendar.find(params[:calendar_id])]
  elsif params[:slug]
    @calendars = Calendar.with_slugs(params[:slug])
  elsif params[:category]
    @calendars = Calendar.in_category(params[:category])
  end
end

#continuing_eventsObject



105
106
107
108
109
110
111
112
# File 'app/controllers/events_controller.rb', line 105

def continuing_events
  return @continuing_events if @continuing_events
  if period && period.start
    @continuing_events = Event.unfinished(period.start).by_end_date
  else
    @continuing_events = Event.unfinished(Time.now).by_end_date
  end
end

#day_namesObject



184
185
186
187
188
189
# File 'app/controllers/events_controller.rb', line 184

def day_names
  return @day_names if @day_names
  @day_names ||= (I18n.t 'date.day_names').dup
  @day_names.push(@day_names.shift) # Class::Date and ActiveSupport::CoreExtensions::Time::Calculations have different ideas of when is the start of the week. We've gone for the rails standard.  
  @day_names
end

#event_finderObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/events_controller.rb', line 87

def event_finder
  ef = Event.scoped({})
  if period
    if period.bounded?
      ef = ef.between(period.start, period.finish) 
    elsif period.start
      ef = ef.after(period.start) 
    else
      ef = ef.before(period.finish) 
    end
  else
    ef = ef.future
  end
  ef = ef.approved if Radiant::Config['event_calendar.require_approval']
  ef = ef.in_calendars(calendars) if calendars
  ef
end

#eventsObject



79
80
81
# File 'app/controllers/events_controller.rb', line 79

def events
  @events ||= event_finder.paginate(pagination_parameters)
end

#filenameObject



156
157
158
# File 'app/controllers/events_controller.rb', line 156

def filename
  url_parts.map{|p| params[p] }.join("_")
end

#indexObject

delivers designated lists of events in minimal formats



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/events_controller.rb', line 15

def index
  @seen_events = {}
  respond_to do |format|
    format.html {
      timeout = (Radiant::Config['event_calendar:cache_duration'] || self.class.cache_timeout || 3600).seconds
      expires_in timeout.to_i, :public => true, :private => false
    }
    format.js {
      render :json => events.to_json
    }
    format.rss {
      render :layout => false
    }
    format.ics {
      ical = RiCal.Calendar do |cal| 
        events.each { |event| cal.add_subcomponent(event.to_ri_cal) } 
      end
      send_data ical.to_s, :filename => "#{filename}.ics", :type => "text/calendar"
    }
  end
end

#list_descriptionObject



114
115
116
117
118
119
120
# File 'app/controllers/events_controller.rb', line 114

def list_description
  return @description if @description
  parts = []
  parts << (period ? period.description : t('event_page.coming_up'))
  parts << I18n.t('event_page.in_calendars', :calendars => calendars.to_sentence) if calendars
  @description = parts.join(' ')
end

#month_name(month) ⇒ Object



176
177
178
# File 'app/controllers/events_controller.rb', line 176

def month_name(month)
  month_names[month]
end

#periodObject

helper methods



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/events_controller.rb', line 53

def period
  return @period if @period
  this = Date.today
  if params[:mday]
    start = Date.civil(params[:year] || this.year, params[:month] || this.month, params[:mday])
    @period = CalendarPeriod.between(start, start.to_datetime.end_of_day)
  elsif params[:month]
    start = Date.civil(params[:year] || this.year, params[:month])
    @period = CalendarPeriod.between(start, start.to_datetime.end_of_month)
  elsif params[:year]
    start = Date.civil(params[:year])
    @period = CalendarPeriod.between(start, start.to_datetime.end_of_year)
  end
end

#query_stringObject



152
153
154
# File 'app/controllers/events_controller.rb', line 152

def query_string
  url_parts.map{|p| "#{p}=params[p]" }.join("&amp;")
end

#short_month_name(month) ⇒ Object



180
181
182
# File 'app/controllers/events_controller.rb', line 180

def short_month_name(month)
  short_month_names[month]
end

#showObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/events_controller.rb', line 37

def show
  @event = Event.find(params[:id])
  respond_to do |format|
    format.html {
      timeout = (Radiant::Config['event_calendar:cache_duration'] || self.class.cache_timeout || 3600).seconds
      expires_in timeout.to_i, :public => true, :private => false
    }
    format.ics {
      ical = RiCal.Calendar { |cal| cal.add_subcomponent(@event.to_ri_cal) }
      send_data ical.to_s, :filename => "#{@event.title.slugify}.ics", :type => "text/calendar"
    }
  end
end

#url_for_date(date) ⇒ Object

this whole mechanism should probably be moved into a helper.



128
129
130
131
132
133
134
# File 'app/controllers/events_controller.rb', line 128

def url_for_date(date)
  url_for(url_parts({
    :mday => date.mday,
    :month => month_name(date.month).downcase,
    :year => date.year
  }))
end

#url_for_month(date) ⇒ Object



136
137
138
139
140
141
142
# File 'app/controllers/events_controller.rb', line 136

def url_for_month(date)
  url_for(url_parts({
    :mday => nil,
    :month => month_name(date.month).downcase,
    :year => date.year
  }))
end

#url_parts(amendments = {}) ⇒ Object



167
168
169
170
# File 'app/controllers/events_controller.rb', line 167

def url_parts(amendments={})
  parts = params.slice(*calendar_parameter_names)   # Hash#slice is defined in will_paginate/lib/core_ext
  parts.merge(amendments)
end

#url_without_periodObject



144
145
146
147
148
149
150
# File 'app/controllers/events_controller.rb', line 144

def url_without_period
  url_for(url_parts({
    :mday => nil,
    :month => nil,
    :year => nil
  }))
end