Sha256: 0d1d34226334c05cf056e625ae371bdbdc4bd07b95ec572e62e246c5d62b2e0d

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

require "mongoid/publishable/callback"
require "mongoid/publishable/callback_collection"

module Mongoid
  module Publishable

    module Callbacks
      def self.included(base)
        base.extend ClassMethods
        base.send :include, InstanceMethods
        base.class_eval do
          # handle after_publish callbacks
          after_save :process_after_publish_callbacks, if: :run_after_publish_callbacks?
        end
      end
      
      module ClassMethods
        # returns the list of callbacks
        def after_publish_callbacks
          @after_publish_callbacks ||= CallbackCollection.new
        end
        
        # adds a callback to the list
        def after_publish(*args, &block)
          Callback.new(*args, &block).tap do |callback|
            after_publish_callbacks << callback
          end
        end
      end
      
      module InstanceMethods        
        # process the callbacks
        def process_after_publish_callbacks
          self.class.after_publish_callbacks.process(self)
        end
    
        # set to run the callbacks after save
        def run_after_publish_callbacks
          @run_after_publish_callbacks = true
        end
    
        # returns whether the object has just been published
        def run_after_publish_callbacks?
          !!@run_after_publish_callbacks
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-publishable-0.0.1 lib/mongoid/publishable/callbacks.rb