Sha256: b72c67f647efc89626c5cce48a40e23c67594d871ff2ed9fb39db1126e1d8ba8

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

class << ActiveRecord::Base

  def notify_on(action, options = {})

    include NotifyOn::Creator

    ([self] + self.descendants).each do |klass|
      klass.class_eval do
        has_many :notifications, -> { preloaded },
                 :class_name => NotifyOn::Notification, :as => :trigger,
                 :dependent => :destroy
      end
    end

    attr_accessor :skip_notifications

    method_to_s = NotifyOn::Utilities.callback_method_name(action, options)
    method_sym = method_to_s.to_sym

    send("after_#{(action.to_s == 'create') ? 'create' : 'update'}", method_sym)

    define_method(method_to_s) do
      # The action trigger needs to be create, save, or a true condition.
      return unless %w(create update).include?(action.to_s) || send(action.to_sym)
      # An optional if condition must be missing or true.
      return unless options[:if].blank? || send(options[:if])
      # An optional unless condition must be missing or false.
      return unless options[:unless].blank? || !send(options[:unless])
      # Create the notification if we get past all our checks.
      create_notify_on_notifications(options)
    end

    private method_to_s.to_sym

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
notify_on-1.0.1 lib/notify_on/notify_on.rb