summaryrefslogtreecommitdiff
path: root/app/models
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
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')
-rw-r--r--app/models/event.rb27
-rw-r--r--app/models/node.rb1
-rw-r--r--app/models/occurrence.rb60
3 files changed, 88 insertions, 0 deletions
diff --git a/app/models/event.rb b/app/models/event.rb
new file mode 100644
index 0000000..60b8521
--- /dev/null
+++ b/app/models/event.rb
@@ -0,0 +1,27 @@
1class Event < ActiveRecord::Base
2
3 # Associations
4
5 has_many :occurrences
6 belongs_to :node
7
8 # Callbacks
9
10 after_save :generate_occurences
11
12 # Instance Methods
13
14 def occurrences_in_range start_time, end_time
15 self.occurrences.find(
16 :all, :conditions => [
17 "start_time > ? AND end_time < ?",
18 start_time, end_time
19 ]
20 )
21 end
22
23 private
24 def generate_occurences
25 Occurrence.generate self
26 end
27end
diff --git a/app/models/node.rb b/app/models/node.rb
index d2db4ba..da26164 100644
--- a/app/models/node.rb
+++ b/app/models/node.rb
@@ -7,6 +7,7 @@ class Node < ActiveRecord::Base
7 belongs_to :head, :class_name => "Page", :foreign_key => :head_id 7 belongs_to :head, :class_name => "Page", :foreign_key => :head_id
8 belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id 8 belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id
9 has_many :permissions 9 has_many :permissions
10 has_one :event
10 11
11 # Callbacks 12 # Callbacks
12 after_create :initialize_empty_page 13 after_create :initialize_empty_page
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