1 # Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de).
3 # This file is part of ECS.
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.
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.
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
21 belongs_to :participant
25 participant_id == y.participant_id and message_id == y.message_id and ev_type_id == y.ev_type_id
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 },
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 },
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?)
46 event.participant_id = participant.id
47 event.message_id = message.id
48 case options[:event_type_name]
50 event.ev_type_id = EvType.find_by_name("created").id
52 event.ev_type_id = EvType.find_by_name("destroyed").id
54 event.ev_type_id = EvType.find_by_name("updated").id
55 else event.ev_type_id = 7777
57 if unique_or_notlast?(event) and event.save
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)
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.
74 pid= event.participant_id
75 etid= event.ev_type_id
76 case initial_event(pid, mid, etid)
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
85 # there is such an event but it's not the last in the queue so create a new one
90 def self.initial_event(pid, mid, etid)
91 Event.find_by_participant_id_and_message_id_and_ev_type_id(pid, mid, etid)