summaryrefslogtreecommitdiff
path: root/app/models/occurrence.rb
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-03-16 20:58:49 +0100
committerhukl <contact@smyck.org>2009-03-18 12:16:59 +0100
commitd2bfbfd2810fbee673e43b2515db8bac527b3441 (patch)
tree1b46e8af44c871290a7a74ab17a8c7201e22f7a9 /app/models/occurrence.rb
parentd957a33a0d50f00c1968c5d12e728bd73ea186b3 (diff)
Refactored Chaos Calendar by wrapping libical and introducing event and occurrence model. More improvements to come. Enables us to create events with reoccurrence and intervals etc.
Diffstat (limited to 'app/models/occurrence.rb')
-rw-r--r--app/models/occurrence.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/app/models/occurrence.rb b/app/models/occurrence.rb
new file mode 100644
index 0000000..6205618
--- /dev/null
+++ b/app/models/occurrence.rb
@@ -0,0 +1,60 @@
1# TODO Make a gem out of the c wrapper
2require 'lib/chaos_calendar/ical_occurrences'
3
4class Occurrence < ActiveRecord::Base
5
6 # Associations
7
8 belongs_to :node
9 belongs_to :event
10
11 # Class Methods
12
13 def self.find_in_range start_time, end_time
14 find(
15 :all, :conditions => [
16 "start_time > ? AND end_time < ?", start_time, end_time
17 ]
18 )
19 end
20
21 # Deletes all Occurrences which belong to the given event. Afterwards a few
22 # variables are set to save repetitive queries. The occurrences of the given
23 # event are then calculated and created.
24 def self.generate event
25 self.delete_all(:event_id => event.id)
26
27 node = event.node
28 summary = node.head.title
29 duration = (event.end_time - event.start_time)
30 occurrences = self.generate_dates(event)
31
32 occurrences.each do |occurrence|
33 self.create(
34 :summary => summary,
35 :start_time => occurrence,
36 :end_time => (occurrence + duration),
37 :node_id => node.id,
38 :event_id => event.id
39 )
40 end
41 end
42
43 # Calculates the start_time of all occurrences for a given event if a proper
44 # RRule is provided. An ArgumentError is thrown from within the libical
45 # wrapper if the RRule is malformed. If the rrule attribute of an event is
46 # nil, it simply returns the event start_time as only occurrence.
47 # Return value is always an array of Time objects.
48 def self.generate_dates event
49 if event.rrule
50 Ical_occurrences::occurrences(
51 event.start_time.utc.iso8601,
52 (Time.now + 5.years).utc.iso8601,
53 event.rrule
54 )
55 else
56 [event.start_time]
57 end
58
59 end
60end