Class: Mosaico::Template
- Inherits:
-
Object
- Object
- Mosaico::Template
- Defined in:
- lib/mosaico/template.rb
Direct Known Subclasses
Constant Summary collapse
- SRC_REGEX =
/src\s*=\s*(?:"([^"]*)"|'([^']*)')/.freeze
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#registered ⇒ Object
(also: #registered?)
readonly
Returns the value of attribute registered.
-
#subdirs ⇒ Object
readonly
Returns the value of attribute subdirs.
Instance Method Summary collapse
- #asset_paths ⇒ Object
-
#edres_path ⇒ Object
where the template’s thumbnails are stored.
- #full_name ⇒ Object
-
#initialize(name, dir, subdirs) ⇒ Template
constructor
A new instance of Template.
- #register! ⇒ Object
- #template_content ⇒ Object
- #template_path ⇒ Object
- #template_url ⇒ Object
- #url ⇒ Object
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
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
5 6 7 |
# File 'lib/mosaico/template.rb', line 5 def dir @dir end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/mosaico/template.rb', line 5 def name @name end |
#registered ⇒ Object (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 |
#subdirs ⇒ Object (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_paths ⇒ Object
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_path ⇒ Object
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_name ⇒ Object
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_content ⇒ Object
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_path ⇒ Object
23 24 25 |
# File 'lib/mosaico/template.rb', line 23 def template_path @template_path ||= File.join(dir, "#{full_name}.html") end |
#template_url ⇒ Object
27 28 29 |
# File 'lib/mosaico/template.rb', line 27 def template_url Mosaico::Engine.routes.url_helpers.template_path(name) end |
#url ⇒ Object
63 64 65 |
# File 'lib/mosaico/template.rb', line 63 def url Mosaico::Engine.routes.url_helpers.template_path(name) end |