Sha256: ebb703801d1f54b2af9ea3fcd54e8c414ea582950b349f4f8a702fdfd4171281

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module Signal
  def self.active_record
    Extensions::ActiveRecord
  end

  module Extensions
    module ActiveRecord
      def self.included(base)
        base.class_eval do
          include Signal

          around_create     :around_create_signal
          around_save       :around_save_signal
          around_destroy    :around_destroy_signal
          before_validation :before_validation_signal
          after_validation  :after_validation_signal
        end
      end

      private
      def around_create_signal
        emit_signal(:before, :create, self)
        yield
        return unless persisted?
        emit_signal(:on, :create, self)
        emit_signal(:after, :create, self)
      end

      def around_save_signal
        if new_record?
          yield
          return
        end

        emit_signal(:before, :update, self)
        yield
        emit_signal(:on, :update, self)
        emit_signal(:after, :update, self)
      end

      def around_destroy_signal
        emit_signal(:before, :remove, self)
        yield
        emit_signal(:on, :remove, self)
        emit_signal(:after, :remove, self)
      end

      def before_validation_signal
        emit_signal(:before, :validation, self)
      end

      def after_validation_signal
        emit_signal(:on, :validation, self) if errors.any?
        emit_signal(:after, :validation, self)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
signal-1.1.0 lib/signal/extensions/active_record.rb
signal-1.0.0 lib/signal/extensions/active_record.rb