inky-rails 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +28 -0
- data/README.md +67 -0
- data/lib/generators/inky/mailer/mailer_generator.rb +23 -0
- data/lib/generators/inky/mailer/templates/layout.inky +1 -0
- data/lib/generators/inky/mailer/templates/view.inky +3 -0
- data/lib/inky-rails.rb +1 -0
- data/lib/inky.rb +47 -0
- data/lib/inky/inky_template.rb +13 -0
- data/lib/inky/parser.rb +80 -0
- data/lib/inky/railtie.rb +11 -0
- data/lib/inky/version.rb +4 -0
- data/test/generator_test.rb +16 -0
- data/test/inky_test.rb +63 -0
- data/test/test_helper.rb +30 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c8f0d12f941bb56b5bab40e1e446b47be95f605c
|
4
|
+
data.tar.gz: 6ed2012d1cd83af8cc4f8d32780f56fa03ef9832
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e1861dea45d4140cd39ec3ec8cc20040e6652e55eaf7051854cc5cbbd4038c813f3791e17c9fc0197938151706a5589bf0b9e95c35d2d17aa73d01d64340be56
|
7
|
+
data.tar.gz: 8d7d957ffafb1829f4214ac1a39db4adbb3a2761c16c60c8a5c935b0ac8f1579d9d9caf854ec24f72b6dba6bfb8b54951bca9ead289f4c34803c307f359628ee
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
Copyright 2016 Whitesmith (https://whitesmith.co)
|
2
|
+
|
3
|
+
Based on Mjml-Rails gem:
|
4
|
+
|
5
|
+
Copyright 2016 Simon Loffler (https://sighmon.com)
|
6
|
+
|
7
|
+
Based on Markerb Gem:
|
8
|
+
|
9
|
+
Copyright 2011-2015 Plataformatec (http://blog.plataformatec.com.br)
|
10
|
+
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
12
|
+
a copy of this software and associated documentation files (the
|
13
|
+
"Software"), to deal in the Software without restriction, including
|
14
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
15
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
16
|
+
permit persons to whom the Software is furnished to do so, subject to
|
17
|
+
the following conditions:
|
18
|
+
|
19
|
+
The above copyright notice and this permission notice shall be
|
20
|
+
included in all copies or substantial portions of the Software.
|
21
|
+
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
23
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
24
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
25
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
26
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
27
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
28
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Inky-Rails
|
2
|
+
|
3
|
+
**Inky-Rails** allows you to render HTML e-mails from an
|
4
|
+
[Inky]https://github.com/zurb/inky) template.
|
5
|
+
|
6
|
+
An example template might look like:
|
7
|
+
|
8
|
+
```erb
|
9
|
+
<!-- TODO -->
|
10
|
+
```
|
11
|
+
|
12
|
+
And the partial `_info.inky`:
|
13
|
+
|
14
|
+
```erb
|
15
|
+
<!-- TODO -->
|
16
|
+
```
|
17
|
+
|
18
|
+
* Notice you can use ERb and partials inside the template.
|
19
|
+
|
20
|
+
Your `user_mailer.rb` might look like this::
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
# ./app/mailers/user_mailer.rb
|
24
|
+
class UserMailer < ActionMailer::Base
|
25
|
+
def user_signup_confirmation()
|
26
|
+
mail(to: 'test@example.com', subject: 'test') do |format|
|
27
|
+
format.text
|
28
|
+
format.inky
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
## Installation
|
35
|
+
|
36
|
+
Add it to your Gemfile.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
gem 'inky-rails'
|
40
|
+
```
|
41
|
+
|
42
|
+
Run the following command to install it:
|
43
|
+
|
44
|
+
```console
|
45
|
+
bundle install
|
46
|
+
```
|
47
|
+
|
48
|
+
Install the Inky CLI parser (optional -g to install it globally):
|
49
|
+
|
50
|
+
```console
|
51
|
+
npm install -g inky-cli@^1.0
|
52
|
+
```
|
53
|
+
|
54
|
+
## Bug reports
|
55
|
+
|
56
|
+
If you discover any bugs, feel free to create an issue on GitHub. Please add as
|
57
|
+
much information as possible to help us fixing the possible bug. We also
|
58
|
+
encourage you to help even more by forking and sending us a pull request.
|
59
|
+
|
60
|
+
[github.com/whitesmith/inky-rails/issues](https://github.com/whitesmith/inky-rails/issues)
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
MIT License. Copyright 2016 Whitesmith. [whitesmith.co](http://www.whitesmith.co)
|
65
|
+
|
66
|
+
Lovingly built on [github.com/sighmon/mjml-rails](https://github.com/sighmon/mjml-rails)
|
67
|
+
which was previously based on [github.com/plataformatec/markerb](https://github.com/plataformatec/markerb)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails/generators/erb/mailer/mailer_generator'
|
2
|
+
|
3
|
+
module Inky
|
4
|
+
module Generators
|
5
|
+
class MailerGenerator < Erb::Generators::MailerGenerator
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
def format
|
11
|
+
nil # Our templates have no format
|
12
|
+
end
|
13
|
+
|
14
|
+
def formats
|
15
|
+
[format]
|
16
|
+
end
|
17
|
+
|
18
|
+
def handler
|
19
|
+
:inky
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<%%= yield %>
|
data/lib/inky-rails.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "inky"
|
data/lib/inky.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "action_view"
|
2
|
+
require "action_view/template"
|
3
|
+
require "inky/inky_template"
|
4
|
+
require "inky/railtie"
|
5
|
+
require "rubygems"
|
6
|
+
|
7
|
+
module Inky
|
8
|
+
|
9
|
+
def self.check_version(bin)
|
10
|
+
begin
|
11
|
+
# TODO: Extract inky-cli version to an explicit constant
|
12
|
+
Gem::Dependency.new('','~> 1.0.1').match?('',`#{bin} --version`)
|
13
|
+
rescue
|
14
|
+
false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.discover_inky_bin
|
19
|
+
default_bin_name = 'inky'
|
20
|
+
inky_bin = default_bin_name
|
21
|
+
# Check for a global install of MJML binary
|
22
|
+
return inky_bin if check_version(default_bin_name)
|
23
|
+
|
24
|
+
# Check for a local install of MJML binary
|
25
|
+
inky_bin = File.join(`npm bin`.chomp, default_bin_name)
|
26
|
+
return inky_bin if check_version(inky_bin)
|
27
|
+
|
28
|
+
raise RuntimeError, "Couldn't find the MJML binary.. have you run $ npm install #{default_bin_name}?"
|
29
|
+
end
|
30
|
+
|
31
|
+
BIN = discover_inky_bin
|
32
|
+
|
33
|
+
class Handler
|
34
|
+
def erb_handler
|
35
|
+
@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
|
36
|
+
end
|
37
|
+
|
38
|
+
def call(template)
|
39
|
+
compiled_source = erb_handler.call(template)
|
40
|
+
if template.formats.include?(:inky)
|
41
|
+
"Inky::InkyTemplate.to_html(begin;#{compiled_source};end).html_safe"
|
42
|
+
else
|
43
|
+
compiled_source
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/inky/parser.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
module Inky
|
2
|
+
class Parser
|
3
|
+
|
4
|
+
# Create new parser
|
5
|
+
#
|
6
|
+
# @param input [String] The string to transform in html
|
7
|
+
def initialize input
|
8
|
+
file = File.open(in_tmp_file, 'w')
|
9
|
+
file.write(input)
|
10
|
+
file.close
|
11
|
+
end
|
12
|
+
|
13
|
+
# Render inky template
|
14
|
+
#
|
15
|
+
# @return [String]
|
16
|
+
def render
|
17
|
+
result = run
|
18
|
+
remove_tmp_files
|
19
|
+
result
|
20
|
+
rescue => error
|
21
|
+
""
|
22
|
+
end
|
23
|
+
|
24
|
+
# Exec inky command
|
25
|
+
#
|
26
|
+
# @return [String] The result as string
|
27
|
+
def run
|
28
|
+
# TODO: Refactor this
|
29
|
+
command = "#{inky_bin} #{in_tmp_file} #{out_tmp_file}"
|
30
|
+
`#{command}`
|
31
|
+
file = File.open("#{out_tmp_file}/#{in_tmp_file.split('/').last}", 'r')
|
32
|
+
str = file.read
|
33
|
+
file.close
|
34
|
+
str
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
# Remove tmp files
|
40
|
+
#
|
41
|
+
# @return nil
|
42
|
+
def remove_tmp_files
|
43
|
+
FileUtils.rm(in_tmp_file)
|
44
|
+
FileUtils.rmdir(out_tmp_file)
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
|
48
|
+
# Return tmp dir
|
49
|
+
#
|
50
|
+
# @return [String]
|
51
|
+
def tmp_dir
|
52
|
+
'/tmp'
|
53
|
+
end
|
54
|
+
|
55
|
+
# Get parser tpm file to store result
|
56
|
+
#
|
57
|
+
# @return [String]
|
58
|
+
def out_tmp_file
|
59
|
+
|
60
|
+
@_out_tmp_file ||= "#{tmp_dir}/out_#{(0...8).map { (65 + rand(26)).chr }.join}"
|
61
|
+
end
|
62
|
+
|
63
|
+
# Get parser tpm file to get result
|
64
|
+
#
|
65
|
+
# @return [String]
|
66
|
+
def in_tmp_file
|
67
|
+
|
68
|
+
@_in_tmp_file ||= "#{tmp_dir}/in_#{(0...8).map { (65 + rand(26)).chr }.join}.inky"
|
69
|
+
# puts @_in_tmp_file
|
70
|
+
return @_in_tmp_file
|
71
|
+
end
|
72
|
+
|
73
|
+
# Get inky-cli bin path
|
74
|
+
#
|
75
|
+
# @return [String]
|
76
|
+
def inky_bin
|
77
|
+
Inky::BIN
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/lib/inky/railtie.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module Inky
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
config.inky = Inky
|
4
|
+
config.app_generators.mailer template_engine: :inky
|
5
|
+
|
6
|
+
initializer 'inky-rails.register_template_handler' do
|
7
|
+
ActionView::Template.register_template_handler :inky, Inky::Handler.new
|
8
|
+
Mime::Type.register 'text/html', :inky
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/inky/version.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'generators/inky/mailer/mailer_generator'
|
3
|
+
|
4
|
+
class GeneratorTest < Rails::Generators::TestCase
|
5
|
+
tests Inky::Generators::MailerGenerator
|
6
|
+
destination File.expand_path('../tmp', __FILE__)
|
7
|
+
setup :prepare_destination
|
8
|
+
|
9
|
+
test 'assert all views are properly created with given name' do
|
10
|
+
run_generator %w(notifier foo bar baz)
|
11
|
+
|
12
|
+
assert_file 'app/views/notifier/foo.inky'
|
13
|
+
assert_file 'app/views/notifier/bar.inky'
|
14
|
+
assert_file 'app/views/notifier/baz.inky'
|
15
|
+
end
|
16
|
+
end
|
data/test/inky_test.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Notifier < ActionMailer::Base
|
4
|
+
self.view_paths = File.expand_path('../views', __FILE__)
|
5
|
+
|
6
|
+
layout false
|
7
|
+
|
8
|
+
def contact(recipient, format_type)
|
9
|
+
@recipient = recipient
|
10
|
+
mail(to: @recipient, from: 'john.doe@example.com') do |format|
|
11
|
+
format.send(format_type)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def link(format_type)
|
16
|
+
mail(to: 'foo@bar.com', from: 'john.doe@example.com') do |format|
|
17
|
+
format.send(format_type)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def user(format_type)
|
22
|
+
mail(to: 'foo@bar.com', from: 'john.doe@example.com') do |format|
|
23
|
+
format.send(format_type)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def no_partial(format_type)
|
28
|
+
mail(to: 'foo@bar.com', from: 'john.doe@example.com') do |format|
|
29
|
+
format.send(format_type)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def multiple_format_contact(recipient)
|
34
|
+
@recipient = recipient
|
35
|
+
mail(to: @recipient, from: 'john.doe@example.com', template: 'contact') do |format|
|
36
|
+
format.text { render 'contact' }
|
37
|
+
format.html { render 'contact' }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class InkyTest < ActiveSupport::TestCase
|
43
|
+
test 'html should be sent as html' do
|
44
|
+
email = Notifier.contact('you@example.com', :inky)
|
45
|
+
assert_equal 'text/html', email.mime_type
|
46
|
+
assert_no_match(/<row>/, email.body.encoded.strip)
|
47
|
+
assert_no_match(/<columns>/, email.body.encoded.strip)
|
48
|
+
assert_match(/<table/, email.body.encoded.strip)
|
49
|
+
assert_match(/This is a number: \d\.\d/, email.body.encoded.strip)
|
50
|
+
end
|
51
|
+
|
52
|
+
test 'with partial' do
|
53
|
+
email = Notifier.user(:inky)
|
54
|
+
assert_equal 'text/html', email.mime_type
|
55
|
+
assert_match(/Hello Partial/, email.body.encoded.strip)
|
56
|
+
end
|
57
|
+
|
58
|
+
test 'without a partial' do
|
59
|
+
email = Notifier.no_partial(:inky)
|
60
|
+
assert_equal 'text/html', email.mime_type
|
61
|
+
assert_match(/Hello World/, email.body.encoded.strip)
|
62
|
+
end
|
63
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.setup
|
4
|
+
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'active_support/test_case'
|
7
|
+
|
8
|
+
require 'action_mailer'
|
9
|
+
require 'rails/railtie'
|
10
|
+
require 'rails/generators'
|
11
|
+
require 'rails/generators/test_case'
|
12
|
+
|
13
|
+
# require "minitest/reporters"
|
14
|
+
# Minitest::Reporters.use!
|
15
|
+
|
16
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
17
|
+
|
18
|
+
require 'inky'
|
19
|
+
|
20
|
+
Inky::Railtie.run_initializers
|
21
|
+
|
22
|
+
if ActiveSupport::TestCase.respond_to? :test_order=
|
23
|
+
ActiveSupport::TestCase.test_order = :sorted
|
24
|
+
end
|
25
|
+
|
26
|
+
# Avoid annoying warning from I18n.
|
27
|
+
I18n.enforce_available_locales = false
|
28
|
+
|
29
|
+
ActionMailer::Base.delivery_method = :test
|
30
|
+
ActionMailer::Base.perform_deliveries = true
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inky-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rafael Jegundo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Render Inky + ERb template views in Rails
|
14
|
+
email: rafael@whitesmith.co
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- MIT-LICENSE
|
20
|
+
- README.md
|
21
|
+
- lib/generators/inky/mailer/mailer_generator.rb
|
22
|
+
- lib/generators/inky/mailer/templates/layout.inky
|
23
|
+
- lib/generators/inky/mailer/templates/view.inky
|
24
|
+
- lib/inky-rails.rb
|
25
|
+
- lib/inky.rb
|
26
|
+
- lib/inky/inky_template.rb
|
27
|
+
- lib/inky/parser.rb
|
28
|
+
- lib/inky/railtie.rb
|
29
|
+
- lib/inky/version.rb
|
30
|
+
- test/generator_test.rb
|
31
|
+
- test/inky_test.rb
|
32
|
+
- test/test_helper.rb
|
33
|
+
homepage: https://github.com/whitesmith/inky-rails
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata: {}
|
37
|
+
post_install_message: "Don't forget to install Inky e.g. \n $
|
38
|
+
npm install -g inky-cli"
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.2.2
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Inky + ERb templates
|
58
|
+
test_files:
|
59
|
+
- test/generator_test.rb
|
60
|
+
- test/inky_test.rb
|
61
|
+
- test/test_helper.rb
|