mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #28244 from ixti/improve/action-mailer-preview-params
Pass request params to ActionMailer::Preview
This commit is contained in:
commit
f680664d4e
5 changed files with 85 additions and 6 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -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&part=text%2Fhtml">', last_response.body
|
||||||
|
assert_match '<option selected value="?name=Ruby&part=text%2Fhtml">', last_response.body
|
||||||
|
assert_match '<option value="?name=Ruby&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="
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue