Class: Event

Inherits:
ActiveRecord::Base show all
Defined in:
app/models/event.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveRecord::Base

#readonly?

Class Method Details

.find_all_by_group(group) ⇒ Object

Finder Methods



133
134
135
136
137
138
139
140
# File 'app/models/event.rb', line 133

def self.find_all_by_group( group )
  ancestor_id = group.id if group
  self.includes( :links_as_descendant )
    .where( :dag_links => { 
              :ancestor_type => "Group", :ancestor_id => ancestor_id
            } )
    .order('start_at')
end

.find_all_by_groups(groups) ⇒ Object



142
143
144
145
146
147
148
149
# File 'app/models/event.rb', line 142

def self.find_all_by_groups( groups )
  group_ids = groups.collect { |g| g.id }
  self.includes( :links_as_descendant )
    .where( :dag_links => { 
              :ancestor_type => "Group", :ancestor_id => group_ids
            } )
    .order('start_at')
end

.find_all_by_user(user) ⇒ Object



151
152
153
# File 'app/models/event.rb', line 151

def self.find_all_by_user(user)
  self.find_all_by_groups(user.groups).direct
end

.to_icalObject



208
209
210
# File 'app/models/event.rb', line 208

def self.to_ical
  self.to_ics
end

.to_icalendarObject



199
200
201
202
203
204
205
206
# File 'app/models/event.rb', line 199

def self.to_icalendar
  cal = Icalendar::Calendar.new
  where(true).each do |event|
    cal.add_event event.to_icalendar_event
  end
  cal.publish
  return cal
end

.to_icsObject

Example:

Group.find(12).events.to_ics


195
196
197
# File 'app/models/event.rb', line 195

def self.to_ics
  self.to_icalendar.to_ical
end

Instance Method Details

#attendeesObject



90
91
92
# File 'app/models/event.rb', line 90

def attendees
  attendees_group.members
end

#attendees_groupObject



87
88
89
# File 'app/models/event.rb', line 87

def attendees_group
  find_attendees_group || create_attendees_group
end

#contact_peopleObject



77
78
79
# File 'app/models/event.rb', line 77

def contact_people
  contact_people_group.members
end

#contact_people_groupObject



74
75
76
# File 'app/models/event.rb', line 74

def contact_people_group
  find_contact_people_group || create_contact_people_group
end

#create_attendees_groupObject



84
85
86
# File 'app/models/event.rb', line 84

def create_attendees_group
  create_special_group :attendees
end

#create_contact_people_groupObject



71
72
73
# File 'app/models/event.rb', line 71

def create_contact_people_group
  create_special_group :contact_people
end

#destroyObject



94
95
96
97
98
# File 'app/models/event.rb', line 94

def destroy
  find_attendees_group.try(:destroy)
  find_contact_people_group.try(:destroy)
  super
end

#find_attendees_groupObject



81
82
83
# File 'app/models/event.rb', line 81

def find_attendees_group
  find_special_group :attendees
end

#find_contact_people_groupObject

Contact People and Attendees



68
69
70
# File 'app/models/event.rb', line 68

def find_contact_people_group
  find_special_group :contact_people
end

#groupObject

Eeach event is assigned to zero, one or several groups. Internally, this is modelled using the DAG structure, i.e. one can use the ‘event.parent_groups` association. But for convenience, here are a few more accessor methods:



31
32
33
# File 'app/models/event.rb', line 31

def group
  self.parent_groups.first
end

#group=(group) ⇒ Object



34
35
36
37
# File 'app/models/event.rb', line 34

def group=( group )
  self.destroy_dag_links
  self.parent_groups << group
end

#groupsObject



38
39
40
# File 'app/models/event.rb', line 38

def groups
  self.parent_groups
end

#localized_end_atObject



54
55
56
# File 'app/models/event.rb', line 54

def localized_end_at
  I18n.localize end_at.to_time if end_at.present?
end

#localized_end_at=(string) ⇒ Object



57
58
59
60
61
# File 'app/models/event.rb', line 57

def localized_end_at=(string)
  attribute_will_change! :end_at
  #self.end_at = string.present? ? Time.parse(string) : nil
  self.end_at = string  # conversion handled by the delocalize gem
end

#localized_start_atObject

Times



45
46
47
# File 'app/models/event.rb', line 45

def localized_start_at
  I18n.localize start_at.to_time if start_at.present?
end

#localized_start_at=(string) ⇒ Object



48
49
50
51
52
# File 'app/models/event.rb', line 48

def localized_start_at=(string)
  attribute_will_change! :start_at
  #self.start_at = string.present? ? Time.parse(string) : nil
  self.start_at = string  # conversion handled by the delocalize gem
end

#titleObject

The title, i.e. the caption of the event is its name.



14
15
16
# File 'app/models/event.rb', line 14

def title
  name
end

#to_icalObject



188
189
190
# File 'app/models/event.rb', line 188

def to_ical
  self.to_ics
end

#to_icalendarObject



177
178
179
180
181
182
# File 'app/models/event.rb', line 177

def to_icalendar
  cal = Icalendar::Calendar.new
  cal.add_event self.to_icalendar_event
  cal.publish
  return cal
end

#to_icalendar_eventObject

Calendar Export



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/event.rb', line 159

def to_icalendar_event
  e = Icalendar::Event.new
  e.dtstart = Icalendar::Values::DateTime.new(self.start_at)
  e.dtend = Icalendar::Values::DateTime.new(self.end_at || self.start_at + 1.hour)
  e.summary = self.name
  e.description = self.description
  e.location = self.location
  if self.contact_people.first
    e.organizer = self.contact_people.first.email
    e.organizer.ical_params = {'CN' => self.contact_people.first.title}
  end
  e.url = self.url
  e.uid = e.url
  e.created = Icalendar::Values::DateTime.new(self.created_at)
  e.last_modified = Icalendar::Values::DateTime.new(self.updated_at)
  return e
end

#to_icsObject



184
185
186
# File 'app/models/event.rb', line 184

def to_ics
  self.to_icalendar.to_ical
end

#to_paramObject



18
19
20
# File 'app/models/event.rb', line 18

def to_param
  "#{id} #{name} #{start_at.year}-#{start_at.month}-#{start_at.day}".parameterize
end

#upcoming?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'app/models/event.rb', line 123

def upcoming?
  Event.upcoming.pluck(:id).include? self.id
end

#wait_for_me_to_existObject

For some strange reason, the callbacks of the creation of an event appear to prevent the event form being found in the database after its creation.

In the EventsController and in specs, we need to make sure that the event exists before continuing. Otherwise ‘ActiveRecord::RecordNotFound` is raised in the controller or when redirecting to the event.

TODO: Check if this is still necessary when migrating to Rails 4.



225
226
227
228
229
230
231
232
233
# File 'app/models/event.rb', line 225

def wait_for_me_to_exist
  raise 'The event has no id, yet!' unless id
  begin
    Event.find self.id
  rescue ActiveRecord::RecordNotFound => e
    sleep 1
    retry
  end
end