Pass request params to ActionMailer::Preview

This commit is contained in:
Alexey Zapparov 2016-05-30 02:15:18 +02:00
parent b61a56541a
commit 8e6c6d854c
No known key found for this signature in database
GPG Key ID: AFBF6FD0DC380A8D
5 changed files with 85 additions and 6 deletions

View File

@ -52,6 +52,12 @@ module ActionMailer
class Preview class Preview
extend ActiveSupport::DescendantsTracker extend ActiveSupport::DescendantsTracker
attr_reader :params
def initialize(params = {})
@params = params
end
class << self class << self
# Returns all mailer preview classes. # Returns all mailer preview classes.
def all def all
@ -62,8 +68,8 @@ module ActionMailer
# Returns the mail object for the given email name. The registered preview # Returns the mail object for the given email name. The registered preview
# interceptors will be informed so that they can transform the message # interceptors will be informed so that they can transform the message
# as they would if the mail was actually being delivered. # as they would if the mail was actually being delivered.
def call(email) def call(email, params = {})
preview = new preview = new(params)
message = preview.public_send(email) message = preview.public_send(email)
inform_preview_interceptors(message) inform_preview_interceptors(message)
message message

View File

@ -968,3 +968,19 @@ class BasePreviewInterceptorsTest < ActiveSupport::TestCase
end end
end end
end end
class BasePreviewTest < ActiveSupport::TestCase
class BaseMailerPreview < ActionMailer::Preview
def welcome
BaseMailer.welcome(params)
end
end
test "has access to params" do
params = { name: "World" }
assert_called_with(BaseMailer, :welcome, [params]) do
BaseMailerPreview.call(:welcome, params)
end
end
end

View File

@ -6,6 +6,8 @@ class Rails::MailersController < Rails::ApplicationController # :nodoc:
before_action :require_local!, unless: :show_previews? before_action :require_local!, unless: :show_previews?
before_action :find_preview, only: :preview before_action :find_preview, only: :preview
helper_method :part_query
def index def index
@previews = ActionMailer::Preview.all @previews = ActionMailer::Preview.all
@page_title = "Mailer Previews" @page_title = "Mailer Previews"
@ -19,7 +21,7 @@ class Rails::MailersController < Rails::ApplicationController # :nodoc:
@email_action = File.basename(params[:path]) @email_action = File.basename(params[:path])
if @preview.email_exists?(@email_action) if @preview.email_exists?(@email_action)
@email = @preview.call(@email_action) @email = @preview.call(@email_action, params)
if params[:part] if params[:part]
part_type = Mime::Type.lookup(params[:part]) part_type = Mime::Type.lookup(params[:part])
@ -76,4 +78,8 @@ class Rails::MailersController < Rails::ApplicationController # :nodoc:
@email @email
end end
end end
def part_query(mime_type)
request.query_parameters.merge(part: mime_type).to_query
end
end end

View File

@ -98,8 +98,8 @@
<% if @email.multipart? %> <% if @email.multipart? %>
<dd> <dd>
<select onchange="formatChanged(this);"> <select onchange="formatChanged(this);">
<option <%= request.format == Mime[:html] ? 'selected' : '' %> value="?part=text%2Fhtml">View as HTML email</option> <option <%= request.format == Mime[:html] ? 'selected' : '' %> value="?<%= part_query('text/html') %>">View as HTML email</option>
<option <%= request.format == Mime[:text] ? 'selected' : '' %> value="?part=text%2Fplain">View as plain-text email</option> <option <%= request.format == Mime[:text] ? 'selected' : '' %> value="?<%= part_query('text/plain') %>">View as plain-text email</option>
</select> </select>
</dd> </dd>
<% end %> <% end %>
@ -107,7 +107,7 @@
</header> </header>
<% if @part && @part.mime_type %> <% if @part && @part.mime_type %>
<iframe seamless name="messageBody" src="?part=<%= Rack::Utils.escape(@part.mime_type) %>"></iframe> <iframe seamless name="messageBody" src="?<%= part_query(@part.mime_type) %>"></iframe>
<% else %> <% else %>
<p> <p>
You are trying to preview an email that does not have any content. You are trying to preview an email that does not have any content.

View File

@ -485,6 +485,57 @@ module ApplicationTests
assert_match '<li><a href="/my_app/rails/mailers/notifier/foo">foo</a></li>', last_response.body assert_match '<li><a href="/my_app/rails/mailers/notifier/foo">foo</a></li>', last_response.body
end end
test "mailer preview receives query params" do
mailer "notifier", <<-RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo(name)
@name = name
mail to: "to@example.org"
end
end
RUBY
html_template "notifier/foo", <<-RUBY
<p>Hello, <%= @name %>!</p>
RUBY
text_template "notifier/foo", <<-RUBY
Hello, <%= @name %>!
RUBY
mailer_preview "notifier", <<-RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo(params[:name] || "World")
end
end
RUBY
app("development")
get "/rails/mailers/notifier/foo.txt"
assert_equal 200, last_response.status
assert_match '<iframe seamless name="messageBody" src="?part=text%2Fplain">', last_response.body
assert_match '<option selected value="?part=text%2Fplain">', last_response.body
assert_match '<option value="?part=text%2Fhtml">', last_response.body
get "/rails/mailers/notifier/foo?part=text%2Fplain"
assert_equal 200, last_response.status
assert_match %r[Hello, World!], last_response.body
get "/rails/mailers/notifier/foo.html?name=Ruby"
assert_equal 200, last_response.status
assert_match '<iframe seamless name="messageBody" src="?name=Ruby&amp;part=text%2Fhtml">', last_response.body
assert_match '<option selected value="?name=Ruby&amp;part=text%2Fhtml">', last_response.body
assert_match '<option value="?name=Ruby&amp;part=text%2Fplain">', last_response.body
get "/rails/mailers/notifier/foo?name=Ruby&part=text%2Fhtml"
assert_equal 200, last_response.status
assert_match %r[<p>Hello, Ruby!</p>], last_response.body
end
test "plain text mailer preview with attachment" do test "plain text mailer preview with attachment" do
image_file "pixel.png", "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEWzIioca/JlAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJgggo=" image_file "pixel.png", "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEWzIioca/JlAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJgggo="