From 1a636d2c97368bc4c4019f4ddcff991ec5eccbce Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Tue, 23 Aug 2011 18:35:48 +0100 Subject: Added events viewer * Fixes #1286 * Version bump --- lib/mauve/history.rb | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'lib/mauve/history.rb') diff --git a/lib/mauve/history.rb b/lib/mauve/history.rb index 91a6fdc..74c5e3a 100644 --- a/lib/mauve/history.rb +++ b/lib/mauve/history.rb @@ -10,8 +10,8 @@ module Mauve property :alert_id, Integer, :key => true property :history_id, Integer, :key => true - belongs_to :alert - belongs_to :history + belongs_to :alert + belongs_to :history def self.migrate! # @@ -53,8 +53,8 @@ module Mauve # If these properties change.. then the migration above will break horribly. FIXME. # property :id, Serial - property :type, String, :required => true, :default => "unknown" - property :event, Text, :required => true, :default => "Nothing set" + property :type, String, :required => true, :default => "unknown", :lazy => false + property :event, Text, :required => true, :default => "Nothing set", :lazy => false property :created_at, Time, :required => true has n, :alerts, :through => :alerthistory @@ -88,9 +88,32 @@ module Mauve def set_created_at(context = :default) self.created_at = Time.now unless self.created_at.is_a?(Time) end - + public + # + # Blasted datamapper not eager-loading my model. + # + def add_to_cached_alerts(a) + @cached_alerts ||= [] + if a.is_a?(Array) and a.all?{|m| m.is_a?(Alert)} + @cached_alerts += a + elsif a.is_a?(Alert) + @cached_alerts << a + else + raise ArgumentError, "#{a.inspect} not an Alert" + end + end + + def alerts + @cached_alerts ||= super + end + + def reload + @cached_alerts = nil + super + end + def logger Log4r::Logger.new self.class.to_s end -- cgit v1.2.1