Class: Nuntius::Template

Inherits:
ApplicationRecord show all
Includes:
Concerns::MetadataScoped, Concerns::Yamlify
Defined in:
app/models/nuntius/template.rb

Constant Summary collapse

LIQUID_TAGS =
/{%(?:(?!%}).)*%}|{{(?:(?!}}).)*}}/

Instance Method Summary collapse

Instance Method Details

#html=(html) ⇒ Object

Trix correctly escapes the HTML, but for liquid this is not what we need. This replaces html-entities within the liquid tags (%…% and {…})



58
59
60
61
62
63
# File 'app/models/nuntius/template.rb', line 58

def html=(html)
  html_unescaped_liquid = html.gsub(LIQUID_TAGS) do |m|
    CGI.unescape_html(m)
  end
  write_attribute :html, html_unescaped_liquid if html
end

#interval_durationObject



65
66
67
68
69
70
71
72
73
74
# File 'app/models/nuntius/template.rb', line 65

def interval_duration
  unless interval.blank?
    number, type = interval.split(" ")
    number = number.to_i

    return number.public_send(type) if number.respond_to?(type)
  end

  0.seconds
end

#interval_time_rangeObject



76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/nuntius/template.rb', line 76

def interval_time_range
  return 0.seconds..0.seconds if interval.blank?

  start = if event.start_with?("before")
    interval_duration.after
  else
    interval_duration.ago
  end

  (start - 1.hour)..start
end

#new_message(object, assigns = {}, params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/nuntius/template.rb', line 26

def new_message(object, assigns = {}, params = {})
  message = Nuntius::Message.new(template: self, transport: transport, metadata: )
  message.nuntiable = object unless object.is_a? Hash

  locale_proc = Nuntius::BaseMessenger.messenger_for_obj(object).locale
  locale = instance_exec(object, &locale_proc) if locale_proc
  locale = params[:locale].to_sym if params[:locale]

  options = {registers: {"template" => self, "message" => message}}

  message.to = render(:to, assigns, locale, options).split(",").reject(&:empty?).join(",")
  message.from = render(:from, assigns, locale, options).split(",").reject(&:empty?).join(",")
  message.subject = render(:subject, assigns, locale, options)
  message.html = render(:html, assigns, locale, options.merge(layout: layout&.data))
  message.text = render(:text, assigns, locale, options)
  message.payload = render(:payload, assigns, locale, options)

  message
end

#translation_scopeObject



46
47
48
49
50
51
52
53
54
# File 'app/models/nuntius/template.rb', line 46

def translation_scope
  scope = %w[]
  scope << layout.name.underscore.tr(" ", "_") if layout
  scope << klass.underscore.tr("/", "_")
  scope << event
  scope << transport
  scope << description.underscore.gsub(/[^a-z]+/, "_") if description
  scope.join(".")
end