Sha256: ca96e9a5b61befa82e3e41f9b2419d11a9898c66e7894cc1534bcc566ba6195b

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

# require 'active_support/concern'

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

    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}"

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

        mail.deliver or returned
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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