Sha256: 7c402a5ebe639bb0997cfeb7a76f8f8e0e7920a9ebd58a917208e89b6db8ff1f

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module ActiveMetadata::Persistence::Mongoid::Watcher

  def self.included(receiver)
    receiver.send :include, InstanceMethods
  end

  module InstanceMethods
    def create_watcher_for(field, owner)
      raise RuntimeError, "The object id MUST be valued" unless self.id

      label_path(field).watchers.create!(:owner_id => owner.id, :created_at => Time.now.utc, :updated_at => Time.now.utc)

      owner.create_inbox unless owner.inbox # ensure that an inbox is present
    end                      

    def watchers_for(field)
      label_path(field).watchers.asc(:created_at).to_a
    end

    def delete_watcher_for(field, owner)
      #TODO
    end    
    
    def is_watched_by(field,owner)
      #TODO
    end                    

    # This is a callback method of the after_save of the ActiveRecord
    # object. 
    # TODO: It should definetly be decoupled in time from the save of
    # the alerting system
    def on_save_watcher_callback                          
      self.changes.each do |field, values|
        send_notification(field, values.first, values.last)
      end
    end

    def send_notification(field, old_value, new_value)   
      watchers_for(field).each { |watch| watch.notify_changes(field, old_value, new_value, self.class, self.id) }
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_metadata-0.1.9 lib/active_metadata/persistence/mongoid/watcher.rb