Class: EmailStylist::StyledErbTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/email_stylist/styled_erb_template.rb

Constant Summary collapse

MUTEX =

GVL может переключить поток между чтением и записью в любой момент — без мьютекса потоки гонятся компилировать одновременно

Mutex.new

Class Method Summary collapse

Class Method Details

.render(template_path:, layout_path: nil, compiled_path: nil, css_string: nil, disable_caching: false, view_context: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/email_stylist/styled_erb_template.rb', line 14

def self.render( # rubocop:disable Metrics/ParameterLists
  template_path:,
  layout_path: nil,
  compiled_path: nil,
  css_string: nil,
  disable_caching: false,
  view_context: nil
)
  template_id = "#{layout_path}#{template_path}"

  @cache ||= {}

  erb_template =
    MUTEX.synchronize do
      @cache[template_id] = nil if disable_caching

      @cache[template_id] ||=
        begin
          html = compile(template_path, layout_path).gsub(/<%.*?%>/m) { |s| "base64___#{Base64.strict_encode64(s)}___base64" }

          html = Inky::Core.new.release_the_kraken(html)

          pm = Premailer.new(
            html,
            css_string:         css_string,
            with_html_string:   true,
            include_link_tags:  true,
            adapter:            :nokogiri,
            generate_text_part: false,
            css_to_attributes:  false,
            remove_scripts:     false,
          )

          html = pm.to_inline_css.force_encoding('ASCII-8BIT').gsub(/base64___(.+?)___base64/) { Base64.strict_decode64(Regexp.last_match(1)) }

          if compiled_path
            write_compiled_erb(html, compiled_path)
            Tilt::ERBTemplate.new(compiled_path)
          else
            Tilt::ERBTemplate.new { html }
          end
        end
    end

  erb_template.render(view_context)
end