forked from robotex82/markerb
-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cache to precompile mjml files in test env
- Loading branch information
Showing
6 changed files
with
92 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mjml | ||
class Cache | ||
attr_reader :template_path | ||
|
||
def initialize(template_path) | ||
@template_path = template_path | ||
end | ||
|
||
# @yield [] -> String | ||
# @return [String] | ||
def cache(&block) | ||
return yield if !Mjml.cache_mjml && block | ||
|
||
cached_path = cached_file_path | ||
if File.exist?(cached_path) | ||
File.read(cached_path) | ||
else | ||
html_content = yield if block | ||
File.write(cached_path, html_content) | ||
html_content | ||
end | ||
end | ||
|
||
private | ||
|
||
def cached_file_path | ||
File.join(cache_directory, "#{fingerprint}.html") | ||
end | ||
|
||
def fingerprint | ||
full_path = File.join(Dir.pwd, 'app', 'views', "#{template_path}.mjml") | ||
raise "Template file not found: #{full_path}" unless File.exist?(full_path) | ||
|
||
Digest::SHA256.hexdigest(File.read(full_path)) | ||
end | ||
|
||
def cache_directory | ||
dir = File.join(Dir.pwd, 'tmp', 'mjml_cache') | ||
FileUtils.mkdir_p(dir) unless Dir.exist?(dir) | ||
dir | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<mjml owa="desktop"> | ||
<mj-body> | ||
<mj-section> | ||
<%= render partial: "user_header", formats: [:html] %> | ||
</mj-section> | ||
|
||
<mj-section> | ||
<mj-column> | ||
<mj-text> | ||
<h2>We inform you about something</h2> | ||
<p> | ||
Please visit <a href="https://www.example.com">this link</a> | ||
</p> | ||
</mj-text> | ||
</mj-column> | ||
</mj-section> | ||
</mj-body> | ||
</mjml> |