class Noticent::Channel

Attributes

configuration[R]
data[RW]
payload[R]
recipients[R]

Public Class Methods

new(config, recipients, payload, configuration) click to toggle source
# File lib/noticent/channel.rb, line 10
def initialize(config, recipients, payload, configuration)
  @config = config
  @recipients = recipients
  @payload = payload
  @configuration = configuration
  @current_user = payload.current_user if payload.respond_to? :current_user
  @routes = Rails.application.routes.url_helpers
end

Public Instance Methods

render_within_context(template:, content:, context:) click to toggle source
# File lib/noticent/channel.rb, line 19
def render_within_context(template:, content:, context:)
  @content = ERB.new(content).result(context)
  template.nil? ? @content : ERB.new(template).result(binding)
end

Protected Instance Methods

current_user() click to toggle source
# File lib/noticent/channel.rb, line 30
def current_user
  raise Noticent::NoCurrentUser if @current_user.nil?

  @current_user
end
render(format: default_format, ext: default_ext, layout: "") click to toggle source
# File lib/noticent/channel.rb, line 36
def render(format: default_format, ext: default_ext, layout: "")
  alert_name = caller[0][/`.*'/][1..-2]
  channel_name = self.class.name.split("::").last.underscore
  view_filename, layout_filename = filenames(channel: channel_name, alert: alert_name, format: format, ext: ext, layout: layout)

  view = View.new(view_filename, template_filename: layout_filename, channel: self)
  view.process(binding)

  [view.data, view.content]
end

Private Instance Methods

filenames(channel:, alert:, format:, ext:, layout:) click to toggle source
# File lib/noticent/channel.rb, line 60
def filenames(channel:, alert:, format:, ext:, layout:)
  view_filename = view_file(channel: channel, alert: alert, format: format, ext: ext)
  layout_filename = ""
  layout_filename = File.join(@config.view_dir, "layouts", "#{layout}.#{format}.#{ext}") unless layout == ""

  return view_filename, layout_filename
end
view_file(channel:, alert:, format:, ext:) click to toggle source
# File lib/noticent/channel.rb, line 49
def view_file(channel:, alert:, format:, ext:)
  view_filename = File.join(@config.view_dir, channel, "#{alert}.#{format}.#{ext}")
  if !File.exist?(view_filename)
    # no specific file found, use a convention
    view_filename = File.join(@config.view_dir, channel, "default.#{format}.#{ext}")
    raise Noticent::ViewNotFound, "view #{view_filename} not found" unless File.exist?(view_filename)
  end

  return view_filename
end