multiparterb 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +7 -7
- data/lib/multiparterb/formatter.rb +20 -18
- data/lib/multiparterb/formatters/base_formatter.rb +2 -2
- data/lib/multiparterb/version.rb +1 -1
- data/lib/multiparterb.rb +3 -1
- data/test/formatters/my_html_formatter.rb +2 -2
- data/test/formatters/my_text_formatter.rb +2 -2
- data/test/generator_test.rb +0 -2
- data/test/multiparterb_test.rb +29 -17
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bc69aa4a1365d5167eaa5a564fecdcc2c1eb8ab9
|
|
4
|
+
data.tar.gz: 8903571b2e83db708a5178c9b050b355e305a465
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: da2c4c6b126b50a5fb802064c3af747e70bd3d5e0cf16a7079fb2cd1bbd1029d4fdd55ab40b178b5e3381d2ca60f8341d075618cb4a323f3aa47ae9e3034180a
|
|
7
|
+
data.tar.gz: 6f1436773cc3e515a462153d94ca6ba6f576a3a210a916c3b6e217b033bdce410ade1b3a86d0170a18bab020eab84ee6740ecd26ccea68cd6b813e9c5c63734a
|
data/README.md
CHANGED
|
@@ -44,11 +44,11 @@ This example formats the elements as it finds them, standard HTML output. _(TODO
|
|
|
44
44
|
|
|
45
45
|
```ruby
|
|
46
46
|
class MyHTMLFormatter < BaseFormatter
|
|
47
|
-
def
|
|
47
|
+
def heading(text)
|
|
48
48
|
content_tag :h1, text
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
-
def
|
|
51
|
+
def text(text=nil, &block)
|
|
52
52
|
content_tag :p, super
|
|
53
53
|
end
|
|
54
54
|
|
|
@@ -62,11 +62,11 @@ And here is an example text formatter.
|
|
|
62
62
|
|
|
63
63
|
```ruby
|
|
64
64
|
class MyTextFormatter < BaseFormatter
|
|
65
|
-
def
|
|
65
|
+
def heading(text)
|
|
66
66
|
"*** #{text} ***\n"
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
def
|
|
69
|
+
def text(text=nil, &block)
|
|
70
70
|
text + "\n"
|
|
71
71
|
end
|
|
72
72
|
|
|
@@ -87,9 +87,9 @@ It will then call the relevent method for each element it finds in the template.
|
|
|
87
87
|
|
|
88
88
|
The set of elements this currently supports, and the method that will get called are :
|
|
89
89
|
|
|
90
|
-
* `<h1></h1>` => `
|
|
91
|
-
* `<p></p>` => `
|
|
92
|
-
* `<a href="https://example.com">example</a>` => `
|
|
90
|
+
* `<h1></h1>` => `Formatter#heading`
|
|
91
|
+
* `<p></p>` => `Formatter#htext`
|
|
92
|
+
* `<a href="https://example.com">example</a>` => `Formatter#hanchor`
|
|
93
93
|
|
|
94
94
|
### Mailers
|
|
95
95
|
|
|
@@ -5,26 +5,28 @@ module MultipartErb
|
|
|
5
5
|
|
|
6
6
|
class Formatter
|
|
7
7
|
def self.parse(node, formatter)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
when 'h1'
|
|
13
|
-
formatter.email_heading(parse(child, formatter).html_safe)
|
|
14
|
-
when 'p'
|
|
15
|
-
formatter.email_text(parse(child, formatter).html_safe)
|
|
16
|
-
when 'a'
|
|
17
|
-
formatter.anchor(
|
|
18
|
-
parse(child, formatter).html_safe,
|
|
19
|
-
child.attributes['href'].content)
|
|
20
|
-
when 'text'
|
|
21
|
-
child.text
|
|
22
|
-
else
|
|
23
|
-
parse(child, formatter)
|
|
24
|
-
end
|
|
8
|
+
"".tap do |result|
|
|
9
|
+
node.children.each do |child|
|
|
10
|
+
result << lookup(child, formatter)
|
|
11
|
+
end
|
|
25
12
|
end
|
|
13
|
+
end
|
|
26
14
|
|
|
27
|
-
|
|
15
|
+
def self.lookup(child, formatter)
|
|
16
|
+
case child.name
|
|
17
|
+
when 'h1'
|
|
18
|
+
formatter.heading(parse(child, formatter).html_safe)
|
|
19
|
+
when 'p'
|
|
20
|
+
formatter.text(parse(child, formatter).html_safe)
|
|
21
|
+
when 'a'
|
|
22
|
+
formatter.anchor(
|
|
23
|
+
parse(child, formatter).html_safe,
|
|
24
|
+
child.attributes['href'].content)
|
|
25
|
+
when 'text'
|
|
26
|
+
child.text
|
|
27
|
+
else
|
|
28
|
+
parse(child, formatter)
|
|
29
|
+
end
|
|
28
30
|
end
|
|
29
31
|
|
|
30
32
|
def self.to_html(compiled_source)
|
|
@@ -2,11 +2,11 @@ class BaseFormatter
|
|
|
2
2
|
include ActionView::Helpers::TagHelper
|
|
3
3
|
include ActionView::Context
|
|
4
4
|
|
|
5
|
-
def
|
|
5
|
+
def heading(text)
|
|
6
6
|
raise NotImplementedError
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
def
|
|
9
|
+
def text(text=nil, &block)
|
|
10
10
|
text || capture(&block)
|
|
11
11
|
end
|
|
12
12
|
|
data/lib/multiparterb/version.rb
CHANGED
data/lib/multiparterb.rb
CHANGED
|
@@ -17,8 +17,10 @@ module MultipartErb
|
|
|
17
17
|
|
|
18
18
|
if template.formats.include?(:html)
|
|
19
19
|
"MultipartErb::Formatter.to_html(begin;#{compiled_source};end).html_safe"
|
|
20
|
-
|
|
20
|
+
elsif template.formats.include?(:text)
|
|
21
21
|
"MultipartErb::Formatter.to_text(begin;#{compiled_source};end).html_safe"
|
|
22
|
+
else
|
|
23
|
+
compiled_source
|
|
22
24
|
end
|
|
23
25
|
end
|
|
24
26
|
end
|
data/test/generator_test.rb
CHANGED
|
@@ -9,8 +9,6 @@ class GeneratorTest < Rails::Generators::TestCase
|
|
|
9
9
|
test "assert all views are properly created with given name" do
|
|
10
10
|
run_generator %w(notifier foo bar baz)
|
|
11
11
|
|
|
12
|
-
# TODO: would be nice to have .erb as the pre-processor in the file name
|
|
13
|
-
# assert_file "app/views/notifier/foo.multipart.erb"
|
|
14
12
|
assert_file "app/views/notifier/foo.multipart"
|
|
15
13
|
assert_file "app/views/notifier/bar.multipart"
|
|
16
14
|
assert_file "app/views/notifier/baz.multipart"
|
data/test/multiparterb_test.rb
CHANGED
|
@@ -5,9 +5,6 @@ class Notifier < ActionMailer::Base
|
|
|
5
5
|
|
|
6
6
|
layout false
|
|
7
7
|
|
|
8
|
-
# TODO: want this to output both html and text, unless gem option given to give preferance
|
|
9
|
-
# mail(:to => @recipient, :from => "john.doe@example.com")
|
|
10
|
-
|
|
11
8
|
def contact(recipient, format_type)
|
|
12
9
|
@recipient = recipient
|
|
13
10
|
mail(:to => @recipient, :from => "john.doe@example.com") do |format|
|
|
@@ -16,22 +13,28 @@ class Notifier < ActionMailer::Base
|
|
|
16
13
|
end
|
|
17
14
|
|
|
18
15
|
def link(format_type)
|
|
19
|
-
mail(:to =>
|
|
16
|
+
mail(:to => "foo@bar.com", :from => "john.doe@example.com") do |format|
|
|
20
17
|
format.send(format_type)
|
|
21
18
|
end
|
|
22
19
|
end
|
|
23
20
|
|
|
21
|
+
def welcome
|
|
22
|
+
mail do |format|
|
|
23
|
+
format.html
|
|
24
|
+
format.text
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
24
28
|
def user(format_type)
|
|
25
|
-
mail(:to =>
|
|
29
|
+
mail(:to => "foo@bar.com", :from => "john.doe@example.com") do |format|
|
|
26
30
|
format.send(format_type)
|
|
27
31
|
end
|
|
28
32
|
end
|
|
29
33
|
|
|
30
|
-
def multiple_format_contact
|
|
31
|
-
@
|
|
32
|
-
|
|
33
|
-
format.
|
|
34
|
-
format.html { render 'contact' }
|
|
34
|
+
def multiple_format_contact
|
|
35
|
+
mail(:to => "you@example.com", :from => "john.doe@example.com", :template => "contact") do |format|
|
|
36
|
+
format.text { render "contact" }
|
|
37
|
+
format.html { render "contact" }
|
|
35
38
|
end
|
|
36
39
|
end
|
|
37
40
|
end
|
|
@@ -41,7 +44,6 @@ class MultipartErbTest < ActiveSupport::TestCase
|
|
|
41
44
|
email = Notifier.contact("you@example.com", :text)
|
|
42
45
|
assert_equal "text/plain", email.mime_type
|
|
43
46
|
assert_equal false, email.multipart?
|
|
44
|
-
#assert_equal "Contact Heading\r\n---------------\r\n\r\n", email.body.encoded.strip
|
|
45
47
|
assert_equal "Contact Heading\n---------------\n\n", email.body.raw_source
|
|
46
48
|
end
|
|
47
49
|
|
|
@@ -49,12 +51,11 @@ class MultipartErbTest < ActiveSupport::TestCase
|
|
|
49
51
|
email = Notifier.contact("you@example.com", :html)
|
|
50
52
|
assert_equal "text/html", email.mime_type
|
|
51
53
|
assert_equal false, email.multipart?
|
|
52
|
-
#assert_equal "Contact Heading\r\n---------------\r\n\r\n", email.body.encoded.strip
|
|
53
54
|
assert_equal "<h1>Contact Heading</h1>", email.body.encoded.strip
|
|
54
55
|
end
|
|
55
56
|
|
|
56
|
-
test
|
|
57
|
-
email = Notifier.multiple_format_contact
|
|
57
|
+
test "dealing with multipart e-mails" do
|
|
58
|
+
email = Notifier.multiple_format_contact
|
|
58
59
|
assert_equal 2, email.parts.size
|
|
59
60
|
assert_equal true, email.multipart?
|
|
60
61
|
assert_equal "multipart/alternative", email.mime_type
|
|
@@ -64,15 +65,26 @@ class MultipartErbTest < ActiveSupport::TestCase
|
|
|
64
65
|
assert_equal "<h1>Contact Heading</h1>", email.parts[1].body.encoded.strip
|
|
65
66
|
end
|
|
66
67
|
|
|
67
|
-
test
|
|
68
|
+
test "format order is not important and default markup is text" do
|
|
69
|
+
email = Notifier.welcome
|
|
70
|
+
assert_equal 2, email.parts.size
|
|
71
|
+
assert_equal true, email.multipart?
|
|
72
|
+
assert_equal "multipart/alternative", email.mime_type
|
|
73
|
+
assert_equal "text/plain", email.parts[0].mime_type
|
|
74
|
+
assert_equal "Welcome\n\n", email.parts[0].body.raw_source
|
|
75
|
+
assert_equal "text/html", email.parts[1].mime_type
|
|
76
|
+
assert_equal "<p>Welcome\r\n</p>", email.parts[1].body.encoded.strip
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
test "with link" do
|
|
68
80
|
email = Notifier.link(:html)
|
|
69
81
|
assert_equal "text/html", email.mime_type
|
|
70
82
|
assert_equal "<p>A link to <a href=\"https://econsultancy.com\">Econsultancy</a></p>", email.body.encoded.strip
|
|
71
83
|
end
|
|
72
84
|
|
|
73
|
-
test
|
|
85
|
+
test "with partial" do
|
|
74
86
|
email = Notifier.user(:html)
|
|
75
87
|
assert_equal "text/html", email.mime_type
|
|
76
|
-
|
|
88
|
+
assert_equal "<p>User template rendering a partial </p><p>User Info Partial</p>", email.body.encoded.strip
|
|
77
89
|
end
|
|
78
90
|
end
|