Sha256: 5d83672b10d0a40aaf1c6be4ca85196c0480886ea900b387a3d66aa4bb6dbe97

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

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

        include Wisper::Publisher

        after_validation :after_validation_broadcast
        after_create     :after_create_broadcast,  on: :create
        after_update     :after_update_broadcast,  on: :update
        after_destroy    :after_destroy_broadcast, on: :destroy
      end

      def commit(_attributes = nil)
        warn "[DEPRECATED] use save, create, update_attributes as usual"
        assign_attributes(_attributes) if _attributes.present?
        save
      end

      module ClassMethods
        def commit(_attributes = nil)
          warn "[DEPRECATED] use save, create, update_attributes as usual"
          new(_attributes).save
        end
      end

      private

      def after_validation_broadcast
        action = new_record? ? 'create' : 'update'
        broadcast("#{action}_#{broadcast_model_name_key}_failed", self) unless errors.empty?
      end

      def after_create_broadcast
        broadcast(:after_create, self)
        broadcast("create_#{broadcast_model_name_key}_successful", self)
      end

      def after_update_broadcast
        broadcast(:after_update, self)
        broadcast("update_#{broadcast_model_name_key}_successful", self)
      end

      def after_destroy_broadcast
        broadcast(:after_destroy, self)
        broadcast("destroy_#{broadcast_model_name_key}_successful", self)
      end

      def broadcast_model_name_key
        self.class.model_name.param_key
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wisper-mongoid-0.1.0 lib/wisper/mongoid/publisher.rb