Sha256: 9563b640c49019a4cdec55f45eb504b4e6fd74cb2d65ab21c8d58574634c1bcd

Contents?: true

Size: 1.17 KB

Versions: 4

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

4 entries across 4 versions & 1 rubygems

Version Path
notify_on-1.0.5 lib/notify_on/notify_on.rb
notify_on-1.0.4 lib/notify_on/notify_on.rb
notify_on-1.0.3 lib/notify_on/notify_on.rb
notify_on-1.0.2 lib/notify_on/notify_on.rb