Class: Mosaico::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/mosaico/template.rb

Direct Known Subclasses

VersafixTemplate

Constant Summary collapse

SRC_REGEX =
/src\s*=\s*(?:"([^"]*)"|'([^']*)')/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, dir, subdirs) ⇒ Template

Returns a new instance of Template.



7
8
9
10
11
12
# File 'lib/mosaico/template.rb', line 7

def initialize(name, dir, subdirs)
  @name = name
  @dir = dir
  @subdirs = subdirs
  @registered = false
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



5
6
7
# File 'lib/mosaico/template.rb', line 5

def dir
  @dir
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/mosaico/template.rb', line 5

def name
  @name
end

#registeredObject (readonly) Also known as: registered?

Returns the value of attribute registered.



5
6
7
# File 'lib/mosaico/template.rb', line 5

def registered
  @registered
end

#subdirsObject (readonly)

Returns the value of attribute subdirs.



5
6
7
# File 'lib/mosaico/template.rb', line 5

def subdirs
  @subdirs
end

Instance Method Details

#asset_pathsObject



51
52
53
54
55
56
57
# File 'lib/mosaico/template.rb', line 51

def asset_paths
  @asset_paths ||= list_assets.each_with_object({}) do |asset_path, ret|
    asset_path = replacement_asset_url(asset_path)
    short_path = asset_path.sub(/\A#{dir}#{File::SEPARATOR}?/, '')
    ret[short_path] = Mosaico.resolve_asset(asset_path)
  end
end

#edres_pathObject

where the template’s thumbnails are stored



32
33
34
# File 'lib/mosaico/template.rb', line 32

def edres_path
  File.join(dir, 'edres')
end

#full_nameObject



59
60
61
# File 'lib/mosaico/template.rb', line 59

def full_name
  "template-#{name}"
end

#register!Object



14
15
16
17
18
19
# File 'lib/mosaico/template.rb', line 14

def register!
  return if registered?
  before_register
  @registered = true
  after_register
end

#template_contentObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mosaico/template.rb', line 36

def template_content
  @template_content ||= begin
    File.read(template_path).gsub(SRC_REGEX) do
      if subdirs.any? { |subdir| $1.start_with?("#{subdir}/") }

        if replacement_url = replacement_asset_url($1)
          next "src='#{Mosaico.url_join(prefix, Mosaico.resolve_asset(replacement_url))}'"
        end
      end

      "src='#{$1}'"
    end
  end
end

#template_pathObject



23
24
25
# File 'lib/mosaico/template.rb', line 23

def template_path
  @template_path ||= File.join(dir, "#{full_name}.html")
end

#template_urlObject



27
28
29
# File 'lib/mosaico/template.rb', line 27

def template_url
  Mosaico::Engine.routes.url_helpers.template_path(name)
end

#urlObject



63
64
65
# File 'lib/mosaico/template.rb', line 63

def url
  Mosaico::Engine.routes.url_helpers.template_path(name)
end