aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/history.rb
blob: 096e648c123b9df3192733855a7f43f844619833 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# encoding: UTF-8
require 'mauve/datamapper'
require 'log4r'

module Mauve
  class AlertHistory
    include DataMapper::Resource

    property :alert_id,   Integer, :key => true
    property :history_id, Integer, :key => true

    belongs_to :alert  
    belongs_to :history 
  end

  class History
    include DataMapper::Resource
    
    # so .first always returns the most recent update
    default_scope(:default).update(:order => [:created_at.desc, :id.desc])
    
    property :id, Serial
#    property :alert_id, String, :required  => true
    property :type,  String, :required => true, :default => "unknown"
    property :event, Text, :required => true, :default => "Nothing set"
    property :created_at, Time, :required => true

    has n, :alerts, :through => :alerthistory

    before :valid?, :set_created_at

    def set_created_at(context = :default)
      self.created_at = Time.now unless self.created_at.is_a?(Time) or self.created_at.is_a?(DateTime)
    end
    
    def logger
     Log4r::Logger.new self.class.to_s
    end

  end


end