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 {…})
60 61 62 63 64 65 |
# File 'app/models/nuntius/template.rb', line 60 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
67 68 69 70 71 72 73 74 75 76 |
# File 'app/models/nuntius/template.rb', line 67 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
78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/nuntius/template.rb', line 78 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 45 46 |
# 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(',').map(&:strip).reject{ |to| to == 'Liquid error internal' }.reject(&:empty?).join(',') return unless .to.present? .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
48 49 50 51 52 53 54 55 56 |
# File 'app/models/nuntius/template.rb', line 48 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 |