Initial models.
[ecs.git] / app / models / event.rb
blob4885f8f4efafc75e43e7e37640dff235a6525532
1 # Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de).
2
3 # This file is part of ECS.
4
5 # ECS is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of
8 # the License, or (at your option) any later version.
9
10 # ECS is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Affero General Public License for more details.
14
15 # You should have received a copy of the GNU Affero General Public
16 # License along with ECS. If not, see <http://www.gnu.org/licenses/>.
19 class Event < ActiveRecord::Base
20   belongs_to  :ev_type
21   belongs_to  :participant
22   belongs_to  :message
24   def ===(y)
25     participant_id == y.participant_id and message_id == y.message_id and ev_type_id == y.ev_type_id
26   end
28 private
30   # if count <0 then list all events otherwise maximal count events
31   named_scope :for_participant, lambda { |participant_id,count| {
32     :conditions => { :participant_id => participant_id },
33     :order => "id ASC",
34     count<0 ? :readonly : :limit => count }}
35   named_scope :for_participant_and_message_desc_order, lambda { |participant_id,message_id| {
36     :conditions => { :participant_id => participant_id, :message_id => message_id },
37     :order => "id DESC",
38     :limit => 1 }}
40   def self.make(options)
41     options.assert_valid_keys(:event_type_name, :membership_message, :participant, :message)
42     message = options[:membership_message] ? options[:membership_message].message : options[:message]
43     participant= options[:membership_message] ? options[:membership_message].membership.participant : options[:participant]
44     return if not (message.ressource.events? and participant.events?)
45     event = Event.new
46     event.participant_id = participant.id
47     event.message_id = message.id
48     case options[:event_type_name]
49       when "created"
50         event.ev_type_id = EvType.find_by_name("created").id
51       when "destroyed"
52         event.ev_type_id = EvType.find_by_name("destroyed").id
53       when "updated"
54         event.ev_type_id = EvType.find_by_name("updated").id
55       else event.ev_type_id = 7777
56     end
57     if unique_or_notlast?(event) and event.save
58       event
59     else
60       # There is already a pending event (the last one) describing a change of
61       # the message. So don't create another one. Instead only touch the
62       # "updated_at" attribute of the event.
63       iev= Event.for_participant_and_message_desc_order(event.participant_id, event.message_id)[0]
64       iev.updated_at= Time.now.to_s(:db)
65       iev.save
66       nil
67     end
68   end
70   def self.unique_or_notlast?(event)
71     # Normally there should/could only be multiple update events more than
72     # once. The testing code would also handle all other events correctly.
73     mid= event.message_id
74     pid= event.participant_id
75     etid= event.ev_type_id
76     case initial_event(pid, mid, etid)
77     when nil
78       # its a unique event
79       return true
80     when Event.for_participant_and_message_desc_order(pid, mid)[0]
81       # there is already such an event for that message and its the last in the queue
82       # for case equality see also overridden === case operator
83       return false
84     else
85       # there is such an event but it's not the last in the queue so create a new one
86       return true
87     end
88   end
90   def self.initial_event(pid, mid, etid)
91     Event.find_by_participant_id_and_message_id_and_ev_type_id(pid, mid, etid)
92   end
94 end