Sha256: 3e80df8ad17e0ed4aad947a8fcc7e94c0a67de5d583b807b2ee9a1c54d7fc7bb

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

# require 'active_support/concern'

module Canmoia
  module Notification
    # TODO add when needed extend ActiveSupport::Concern

    attr_accessor :notifications_declared

    def self.extended base
      base.notifications_declared = []
    end

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

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

    private

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

      notification = "#{event}_#{recipient_attribute}".to_sym
      unless notifications_declared.include? notification
        notifications_declared << notification
      else
        raise "Notification for event '#{event}' to recipient '#{recipient_attribute}' already declared!"
      end

      # TODO only define notification method if method is present on mailer
      # TODO create task to generate / update mailer with notification settings

      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 recipient_attribute
        mail   = mailer.send mail_method, self, entity

        mail.deliver or returned
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
canmoia-0.0.3 lib/canmoia/features/notification.rb
canmoia-0.0.2 lib/canmoia/features/notification.rb
canmoia-0.0.1 lib/canmoia/features/notification.rb