Sha256: aae463cf536b55e74625bd1f614909c1c5050fa702ae635c3f1c08d6ba140910

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module Canmoia::Notification

  def notify entity_name = :responsible, on: -> { raise "on is required" }, via: :email
    raise "Recipient named as '#{entity_name}' not found for #{self.name}" unless instance_methods.include? entity_name.to_sym

    on.each do |event|
      add_notification_method event, entity_name, via
    end
  end

  private

  # TODO use method missing
  def add_notification_method event, entity_name, via
    mail_method = "#{event}_notification_to_#{entity_name}"

    define_method "#{event}!" do |*args|
      returned = super *args
      # TODO better error message when mailer is not found
      # TODO allow specification of custom mailer
      mailer = "#{self.class.name}Mailer".classify.constantize

      # TODO better error message when mail method is not found
      entity = send entity_name
      mail   = mailer.send mail_method, self, entity

      mail.deliver or returned
    end
  end

  # - on :open   , close: :reviewing, cancel: :canceled, notify: :responsible
  # V on :review, reject: :rejected, accept: :accepted
  # - on :accept, notify: :buyer
  # - on :reject, notify: :buyer
  # - on :cancel, notify: [:buyer, via: :email] }
  # - on :finish, notify: [:buyer, via: :email] }

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
canmoia-0.0.2 lib/canmoia/concerns/notification.rb~
canmoia-0.0.1 lib/canmoia/concerns/notification.rb~