mjml-rails 4.5.0 → 4.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 128c814314ffe1c725a9d46d0537c161a45098650f67cf063cf0e27325cd11d2
4
- data.tar.gz: b23564a1cf6b824359019f60bf08cb1f97d6d7584f1e58a2220fc672a4dea77c
3
+ metadata.gz: e001eb3b8f9abfa49b6fbf3e0e10d655fdeb38e68da79f55b348977f4a7082b8
4
+ data.tar.gz: 7d78f4466486513af5d364c7ca3e5146724a6e9fc785b26cdf66ae5c626138fb
5
5
  SHA512:
6
- metadata.gz: 9c56d2b1b80bcb2f7ef36cf8e927d135fbdc26fd67f7ac2db52ece28065d1cb98010c89f9abae9176c7861ef3ab65ae099c8cb98ecabf371d06c42cff53f141d
7
- data.tar.gz: 1eea404a1e315166fd132ec37b76257d4789f030af4853992067230cda800875861b68fde85a358e803ff283d15cb1d14c88ae778dd49e5f27f01109faf0e787
6
+ metadata.gz: 534c1c2975ff87a7e5bfed79bcbf19ec0d00ca16cf4a871faac3c9c89a348452aed2afb6197629598f473fd84943d67b64581a75139ca7e608ebcf4878004615
7
+ data.tar.gz: 6b58532184a1d750fc2d4ae76508a52a3b86578b82fa501a6c4a01c7e9f03856fa2ac48522b0744536e573468233e7f6f54e6a8bd234dff6b58f58e02879ebd5
Binary file
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MJML-Rails
2
2
 
3
- [![Build Status](https://api.travis-ci.org/sighmon/mjml-rails.svg?branch=master)](http://travis-ci.org/sighmon/mjml-rails) [![Gem Version](https://badge.fury.io/rb/mjml-rails.svg)](https://badge.fury.io/rb/mjml-rails)
3
+ [![Build Status](https://github.com/sighmon/mjml-rails/workflows/test/badge.svg?branch=master)](https://github.com/sighmon/mjml-rails/actions?query=workflow%3Atest+branch%3Amaster) [![Gem Version](https://badge.fury.io/rb/mjml-rails.svg)](https://badge.fury.io/rb/mjml-rails)
4
4
 
5
5
  **MJML-Rails** allows you to render HTML e-mails from an [MJML](https://mjml.io) template.
6
6
 
@@ -112,18 +112,23 @@ Mjml.setup do |config|
112
112
  end
113
113
  ```
114
114
 
115
- If you’d like to specify a different MJML binary to run other than `4.`:
115
+ You can optionally provide a custom MJML binary if MJML-Rails cannot find it:
116
116
 
117
117
  ```ruby
118
118
  # config/initializers/mjml.rb
119
119
  Mjml.setup do |config|
120
- config.mjml_binary_version_supported = "3.3.5"
120
+ config.mjml_binary = "/path/to/custom/mjml"
121
121
  end
122
- # If you set a different MJML binary version, you need to re-discover the binary
123
- Mjml::BIN = Mjml.discover_mjml_bin
124
122
  ```
125
123
 
126
- **Note:** If you set a different MJML binary you’ll see warnings in your logs of `already initialized constant Mjml::BIN`. Read more: [#39](https://github.com/sighmon/mjml-rails/issues/39#issuecomment-429151908)
124
+ If you’d like to specify a different MJML version, set it like this:
125
+
126
+ ```ruby
127
+ # config/initializers/mjml.rb
128
+ Mjml.setup do |config|
129
+ config.mjml_binary_version_supported = "3.3.5"
130
+ end
131
+ ```
127
132
 
128
133
  ### MJML v3.x & v4.x support
129
134
 
@@ -1 +1 @@
1
- require "mjml"
1
+ require "mjml"
@@ -7,7 +7,17 @@ require "rubygems"
7
7
  require "open3"
8
8
 
9
9
  module Mjml
10
- mattr_accessor :template_language, :raise_render_exception, :mjml_binary_version_supported, :mjml_binary_error_string, :beautify, :minify, :validation_level
10
+ mattr_accessor \
11
+ :beautify,
12
+ :minify,
13
+ :mjml_binary,
14
+ :mjml_binary_error_string,
15
+ :mjml_binary_version_supported,
16
+ :raise_render_exception,
17
+ :template_language,
18
+ :validation_level
19
+
20
+ mattr_writer :valid_mjml_binary
11
21
 
12
22
  @@template_language = :erb
13
23
  @@raise_render_exception = true
@@ -24,33 +34,55 @@ module Mjml
24
34
  false
25
35
  end
26
36
 
27
- def self.run_mjml(args, mjml_bin: nil)
28
- mjml_bin ||= BIN
29
-
37
+ def self.run_mjml(args, mjml_bin: valid_mjml_binary)
30
38
  Open3.capture3("#{mjml_bin} #{args}")
31
39
  end
32
40
 
33
- def self.discover_mjml_bin
34
- # Check for local install of MJML with yarn
35
- yarn_bin = `which yarn`.chomp
36
- if yarn_bin.present?
37
- mjml_bin = "#{yarn_bin} run mjml"
38
- return mjml_bin if check_version(mjml_bin)
41
+ def self.valid_mjml_binary
42
+ @@valid_mjml_binary ||=
43
+ check_for_custom_mjml_binary ||
44
+ check_for_yarn_mjml_binary ||
45
+ check_for_npm_mjml_binary ||
46
+ check_for_global_mjml_binary
47
+
48
+ return @@valid_mjml_binary if @@valid_mjml_binary
49
+
50
+ puts Mjml.mjml_binary_error_string
51
+ end
52
+
53
+ def self.check_for_custom_mjml_binary
54
+ if const_defined?('BIN') && Mjml::BIN.present?
55
+ logger.warn('Setting `Mjml::BIN` is deprecated and will be removed in a future version! Please use `Mjml.mjml_binary=` instead.')
56
+ self.mjml_binary = Mjml::BIN
57
+ remove_const 'BIN'
39
58
  end
40
59
 
41
- # Check for a local install of MJML with npm
60
+ return unless mjml_binary.present?
61
+
62
+ return mjml_binary if check_version(mjml_binary)
63
+
64
+ raise "MJML.mjml_binary is set to '#{mjml_binary}' but MJML-Rails could not validate that it is a valid MJML binary. Please check your configuration."
65
+ end
66
+
67
+ def self.check_for_yarn_mjml_binary
68
+ yarn_bin = `which yarn`.chomp
69
+ return unless yarn_bin.present?
70
+
71
+ mjml_bin = "#{yarn_bin} run mjml"
72
+ return mjml_bin if check_version(mjml_bin)
73
+ end
74
+
75
+ def self.check_for_npm_mjml_binary
42
76
  npm_bin = `which npm`.chomp
43
- if npm_bin.present? && (installer_path = bin_path_from(npm_bin)).present?
44
- mjml_bin = File.join(installer_path, 'mjml')
45
- return mjml_bin if check_version(mjml_bin)
46
- end
77
+ return unless npm_bin.present? && (installer_path = bin_path_from(npm_bin)).present?
78
+
79
+ mjml_bin = File.join(installer_path, 'mjml')
80
+ return mjml_bin if check_version(mjml_bin)
81
+ end
47
82
 
48
- # Check for a global install of MJML
83
+ def self.check_for_global_mjml_binary
49
84
  mjml_bin = `which mjml`.chomp
50
85
  return mjml_bin if mjml_bin.present? && check_version(mjml_bin)
51
-
52
- puts Mjml.mjml_binary_error_string
53
- nil
54
86
  end
55
87
 
56
88
  def self.bin_path_from(package_manager)
@@ -63,7 +95,9 @@ module Mjml
63
95
  nil
64
96
  end
65
97
 
66
- BIN = discover_mjml_bin
98
+ def self.discover_mjml_bin
99
+ logger.warn('`Mjml.discover_mjml_bin` is deprecated and has no effect anymore! Please use `Mjml.mjml_binary=` to set a custom MJML binary.')
100
+ end
67
101
 
68
102
  class Handler
69
103
  def template_handler
@@ -8,7 +8,7 @@ module Mjml
8
8
  #
9
9
  # @param input [String] The string to transform in html
10
10
  def initialize input
11
- raise Mjml.mjml_binary_error_string unless mjml_bin
11
+ raise Mjml.mjml_binary_error_string unless Mjml.valid_mjml_binary
12
12
  @input = input
13
13
  end
14
14
 
@@ -40,14 +40,5 @@ module Mjml
40
40
  out_tmp_file.read
41
41
  end
42
42
  end
43
-
44
- private
45
-
46
- # Get mjml bin path
47
- #
48
- # @return [String]
49
- def mjml_bin
50
- Mjml::BIN
51
- end
52
43
  end
53
44
  end
@@ -7,5 +7,10 @@ module Mjml
7
7
  ActionView::Template.register_template_handler :mjml, Mjml::Handler.new
8
8
  Mime::Type.register_alias "text/html", :mjml
9
9
  end
10
+
11
+ config.to_prepare do
12
+ # make sure we have a valid mjml binary
13
+ Mjml.valid_mjml_binary
14
+ end
10
15
  end
11
16
  end
@@ -1,4 +1,4 @@
1
1
  module Mjml
2
2
  # Version number no longer matches MJML.io version
3
- VERSION = "4.5.0"
3
+ VERSION = "4.6.0"
4
4
  end
@@ -112,3 +112,44 @@ class NotifierMailerTest < ActiveSupport::TestCase
112
112
  refute email.body.match(/tracking-code-123/)
113
113
  end
114
114
  end
115
+
116
+ describe Mjml do
117
+ describe '#valid_mjml_binary' do
118
+ before do
119
+ Mjml.mjml_binary = nil
120
+ Mjml.valid_mjml_binary = nil
121
+ end
122
+
123
+ after do
124
+ Mjml.mjml_binary = nil
125
+ Mjml.valid_mjml_binary = nil
126
+ end
127
+
128
+ it 'can be set to a custom value with mjml_binary if version is correct' do
129
+ Mjml.mjml_binary = 'some custom value'
130
+ Mjml.stub :check_version, true do
131
+ expect(Mjml.valid_mjml_binary).must_equal 'some custom value'
132
+ end
133
+ end
134
+
135
+ it 'raises an error if mjml_binary is invalid' do
136
+ Mjml.mjml_binary = 'some custom value'
137
+ err = expect { Mjml.valid_mjml_binary }.must_raise(StandardError)
138
+ expect(err.message).must_match(/MJML\.mjml_binary is set to 'some custom value' but MJML-Rails could not validate that it is a valid MJML binary/)
139
+ end
140
+
141
+ it 'honors old Mjml::BIN way of setting custom binary' do
142
+ Mjml::BIN = 'set by old way'
143
+ err = expect { Mjml.valid_mjml_binary }.must_raise(StandardError)
144
+ expect(err.message).must_match(/MJML\.mjml_binary is set to 'set by old way' but MJML-Rails could not validate that it is a valid MJML binary/)
145
+ end
146
+
147
+ it 'ignores empty Mjml::BIN' do
148
+ Mjml::BIN = ''
149
+ Mjml.mjml_binary = 'set by mjml_binary'
150
+
151
+ err = expect { Mjml.valid_mjml_binary }.must_raise(StandardError)
152
+ expect(err.message).must_match(/MJML\.mjml_binary is set to 'set by mjml_binary' but MJML-Rails could not validate that it is a valid MJML binary/)
153
+ end
154
+ end
155
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mjml-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.5.0
4
+ version: 4.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Loffler
metadata.gz.sig CHANGED
Binary file