Sha256: 1e67a063bf600e305a702e54af401a8b633b9eea4a591f1aca1e4a958f934b00

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module Wisper
  module ActiveRecord
    module Publisher
      extend ActiveSupport::Concern
      included do

        include Wisper::Publisher

        after_commit :publish_creation, on: :create
        after_commit :publish_update,   on: :update
        after_commit :publish_destroy,  on: :destroy
      end

      def commit(_attributes = nil)
        _action = new_record? ? 'create' : 'update'
        assign_attributes(_attributes) if _attributes.present?
        result = save
        if result
          broadcast("#{_action}_#{self.class.model_name.param_key}_successful", self)
        else
          broadcast("#{_action}_#{self.class.model_name.param_key}_failed", self)
        end
        result
      end

      module ClassMethods
        def commit(_attributes = nil)
          new(_attributes).commit
        end
      end

      private

      def publish_creation
        broadcast(:after_create, self)
      end

      def publish_update
        broadcast(:after_update, self)
      end

      def publish_destroy
        broadcast(:after_destroy, self)
        broadcast("destroy_#{self.class.model_name.param_key}_successful", self)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wisper-activerecord-0.1.0 lib/wisper/active_record/publisher.rb