Class: Nuntius::Template
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Nuntius::Template
- Includes:
- Concerns::MetadataScoped, Concerns::Yamlify
- Defined in:
- app/models/nuntius/template.rb
Constant Summary collapse
- LIQUID_TAGS =
/{%(?:(?!%}).)*%}|{{(?:(?!}}).)*}}/
Instance Method Summary collapse
-
#html=(html) ⇒ Object
Trix correctly escapes the HTML, but for liquid this is not what we need.
- #interval_duration ⇒ Object
- #interval_time_range ⇒ Object
- #new_message(object, assigns = {}, params = {}) ⇒ Object
- #translation_scope ⇒ Object
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_duration ⇒ Object
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_range ⇒ Object
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 (object, assigns = {}, params = {}) = Nuntius::Message.new(template: self, transport: transport, metadata: ) .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] = {registers: {"template" => self, "message" => }} .to = render(:to, assigns, locale, ).split(",").reject(&:empty?).join(",") .from = render(:from, assigns, locale, ).split(",").reject(&:empty?).join(",") .subject = render(:subject, assigns, locale, ) .html = render(:html, assigns, locale, .merge(layout: layout&.data)) .text = render(:text, assigns, locale, ) .payload = render(:payload, assigns, locale, ) end |
#translation_scope ⇒ Object
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 |