Skip to content

Commit 8f5669a

Browse files
committed
Ability to send params to action.
Allows methods of MailView subclasses to access params, as in a typical Rails controller.
1 parent bc6ab46 commit 8f5669a

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

lib/mail_view.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def call(env)
2323

2424
def call(env)
2525
request = Rack::Request.new(env)
26+
@params = request.params || {}
2627

2728
if request.path_info == "" || request.path_info == "/"
2829
links = self.actions.map do |action|
@@ -110,7 +111,12 @@ def find_preferred_part(mail, formats)
110111
end
111112

112113
def part_body_url(part)
113-
'?part=%s' % Rack::Utils.escape([part.main_type, part.sub_type].compact.join('/'))
114+
part_query_string = '?part=%s' % Rack::Utils.escape([part.main_type, part.sub_type].compact.join('/'))
115+
if @params.any?
116+
part_query_string + '&' + Rack::Utils.build_query(@params)
117+
else
118+
part_query_string
119+
end
114120
end
115121

116122
def find_part(mail, matching_content_type)
@@ -126,4 +132,9 @@ def find_part(mail, matching_content_type)
126132
mail
127133
end
128134
end
135+
136+
def params
137+
@params
138+
end
139+
129140
end

test/test_mail_view.rb

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ def nested_multipart_message
107107
mail.add_part container
108108
mail
109109
end
110+
111+
def message_with_params
112+
greeting = params['greeting']
113+
email = params['email']
114+
Mail.new do
115+
to "User <#{email}>"
116+
from 'Test Peek <[email protected]>'
117+
body "#{greeting}"
118+
end
119+
end
120+
121+
def multipart_alternative_with_params
122+
greeting = params['greeting']
123+
Mail.new do
124+
125+
126+
yield self if block_given?
127+
128+
text_part do
129+
body "Plain greeting #{greeting}"
130+
end
131+
132+
html_part do
133+
content_type 'text/html; charset=UTF-8'
134+
body "<h1>Html greeting #{greeting}</h1>"
135+
end
136+
end
137+
end
110138
end
111139

112140
class ISayHelloAndYouSayGoodbyeInterceptor
@@ -131,8 +159,9 @@ def app
131159
Preview
132160
end
133161

134-
def iframe_src_match(content_type)
135-
/<iframe[^>]* src="\?part=#{Regexp.escape(Rack::Utils.escape(content_type))}"[^>]*><\/iframe>/
162+
def iframe_src_match(content_type, params = '')
163+
params = '&' + Rack::Utils.build_query(Rack::Utils.parse_query(params)) if params != ''
164+
/<iframe[^>]* src="\?part=#{Regexp.escape(Rack::Utils.escape(content_type)) + params}"[^>]*><\/iframe>/
136165
end
137166

138167
def unescaped_body
@@ -306,6 +335,31 @@ def test_interceptors
306335
assert_equal 'Goodbye', last_response.body
307336
end
308337

338+
def test_message_with_params
339+
get '/message_with_params?greeting=Bonjour&[email protected]'
340+
assert last_response.ok?
341+
assert_match '[email protected]', unescaped_body, "Email should be in body"
342+
assert_match 'Bonjour', unescaped_body, "Greeting should be in body"
343+
end
344+
345+
def test_message_with_params_no_part_url
346+
get '/message_with_params?greeting=Bonjour&[email protected]'
347+
assert last_response.ok?
348+
assert_match iframe_src_match('', '&greeting=Bonjour&[email protected]'), last_response.body, "Iframe src should include params"
349+
end
350+
351+
def test_message_multipart_alternative_plain_with_params
352+
get '/multipart_alternative_with_params?part=text%2Fplain&greeting=Bonjour'
353+
assert last_response.ok?
354+
assert_match 'Plain greeting Bonjour', unescaped_body, "Email should be in body"
355+
end
356+
357+
def test_message_multipart_alternative_html_with_params
358+
get '/multipart_alternative_with_params?part=text%2Fhtml&greeting=Bonjour'
359+
assert last_response.ok?
360+
assert_match 'Html greeting Bonjour', unescaped_body, "Email should be in body"
361+
end
362+
309363
unless RUBY_VERSION >= '1.9'
310364
def test_tmail_html_message
311365
get '/tmail_html_message'

0 commit comments

Comments
 (0)