Class: ActivityNotification::OptionalTarget::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/activity_notification/optional_targets/base.rb

Overview

Abstract optional target class to develop optional notification target class.

Direct Known Subclasses

AmazonSNS, Slack

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Initialize method to create view context in this OptionalTarget instance

Parameters:

  • options (Hash) (defaults to: {})

    Options for initializing target

Options Hash (options):

  • :skip_initializing_target (Boolean) — default: false

    Whether skip calling initialize_target method

  • others (Hash)

    Options for initializing target



14
15
16
17
18
19
20
21
22
23
# File 'lib/activity_notification/optional_targets/base.rb', line 14

def initialize(options = {})
  @view_context = ActionView::Base.new(ActionController::Base.view_paths, {})
  @view_context.class_eval do 
    include Rails.application.routes.url_helpers
    def default_url_options
      ActionMailer::Base.default_url_options
    end
  end
  initialize_target(options) unless options.delete(:skip_initializing_target)
end

Instance Attribute Details

#view_contextObject

View context to render notification message

Returns:

  • View context to render notification message



8
9
10
# File 'lib/activity_notification/optional_targets/base.rb', line 8

def view_context
  @view_context
end

Instance Method Details

#initialize_target(_options = {}) ⇒ Object

Initialize method to be overriden in user implementation class

Parameters:

  • _options (Hash) (defaults to: {})

    Options for initializing

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/activity_notification/optional_targets/base.rb', line 33

def initialize_target(_options = {})
  raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
end

#notify(_notification, _options = {}) ⇒ Object

Publishing notification method to be overriden in user implementation class

Parameters:

  • _notification (Notification)

    Notification instance

  • _options (Hash) (defaults to: {})

    Options for publishing

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/activity_notification/optional_targets/base.rb', line 40

def notify(_notification, _options = {})
  raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
end

#to_optional_target_nameObject

Returns demodulized symbol class name as optional target name

Returns:

  • Demodulized symbol class name as optional target name



27
28
29
# File 'lib/activity_notification/optional_targets/base.rb', line 27

def to_optional_target_name
  self.class.name.demodulize.underscore.to_sym
end