Sha256: 39bfd9d401445f11689981f9e8d99bd9c673dad1175f306b1ebe705c4671e6d0

Contents?: true

Size: 1.63 KB

Versions: 9

Compression:

Stored size: 1.63 KB

Contents

module ActiveMetadata::Persistence::ActiveRecord::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
      Watcher.create! :document_id => metadata_id, :label => field, :owner_id => owner.id 
    end                      

    def watchers_for(field)
      Watcher.all(:conditions => {:label => field, :document_id => metadata_id})   
    end

    def delete_watcher_for(field, owner)
      Watcher.where(:document_id => metadata_id, :label => field, :owner_id => owner.id).each do |watcher|
        watcher.destroy
      end   
    end    
    
    def is_watched_by(field,owner)
      Watcher.where(:document_id => metadata_id, :label => field, :owner_id => owner.id).empty? ? false : true 
    end                    

    def send_notification(field, old_value, new_value, type=:default_message, created_by=nil)   
      watchers_for(field).each { |watch| notify_changes(field, old_value, new_value, self.class.to_s, self.id, watch.owner_id, type, created_by) }
    end
          
    def notifier
      @notifier
    end
    
    private
    def notify_changes(matched_label, old_value, new_value, model_class, model_id, owner_id, type, created_by)    
      raise "A watcher notifier class must be implemented" unless WatcherNotifier

      @notifier = WatcherNotifier.new
      @notifier.notify(matched_label.to_s, old_value, new_value, model_class, model_id,owner_id,type, created_by)                                                                               
    end
    
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
active_metadata-0.3.2 lib/active_metadata/persistence/active_record/watcher.rb
active_metadata-0.3.1 lib/active_metadata/persistence/active_record/watcher.rb
active_metadata-0.3.0 lib/active_metadata/persistence/active_record/watcher.rb
active_metadata-0.2.4 lib/active_metadata/persistence/active_record/watcher.rb
active_metadata-0.2.3 lib/active_metadata/persistence/active_record/watcher.rb
active_metadata-0.2.2 lib/active_metadata/persistence/active_record/watcher.rb
active_metadata-0.2.1 lib/active_metadata/persistence/active_record/watcher.rb
active_metadata-0.2.0 lib/active_metadata/persistence/active_record/watcher.rb
active_metadata-0.1.9 lib/active_metadata/persistence/active_record/watcher.rb