Skip to content

Commit

Permalink
Pass request params to ActionMailer::Preview
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Feb 7, 2017
1 parent 383c4de commit 28e388a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 6 deletions.
10 changes: 8 additions & 2 deletions actionmailer/lib/action_mailer/preview.rb
Expand Up @@ -52,6 +52,12 @@ def register_preview_interceptor(interceptor)
class Preview
extend ActiveSupport::DescendantsTracker

attr_reader :params

def initialize(params = {})
@params = params
end

class << self
# Returns all mailer preview classes.
def all
Expand All @@ -62,8 +68,8 @@ def all
# Returns the mail object for the given email name. The registered preview
# interceptors will be informed so that they can transform the message
# as they would if the mail was actually being delivered.
def call(email)
preview = new
def call(email, params = {})
preview = self.new(params)
message = preview.public_send(email)
inform_preview_interceptors(message)
message
Expand Down
16 changes: 16 additions & 0 deletions actionmailer/test/base_test.rb
Expand Up @@ -968,3 +968,19 @@ def self.previewing_email(mail); 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
8 changes: 7 additions & 1 deletion railties/lib/rails/mailers_controller.rb
Expand Up @@ -6,6 +6,8 @@ class Rails::MailersController < Rails::ApplicationController # :nodoc:
before_action :require_local!, unless: :show_previews?
before_action :find_preview, only: :preview

helper_method :part_query

def index
@previews = ActionMailer::Preview.all
@page_title = "Mailer Previews"
Expand All @@ -19,7 +21,7 @@ def preview
@email_action = File.basename(params[:path])

if @preview.email_exists?(@email_action)
@email = @preview.call(@email_action)
@email = @preview.call(@email_action, params)

if params[:part]
part_type = Mime::Type.lookup(params[:part])
Expand Down Expand Up @@ -76,4 +78,8 @@ def find_part(format) # :doc:
@email
end
end

def part_query(mime_type)
request.query_parameters.merge(part: mime_type).to_query
end
end
6 changes: 3 additions & 3 deletions railties/lib/rails/templates/rails/mailers/email.html.erb
Expand Up @@ -98,16 +98,16 @@
<% if @email.multipart? %>
<dd>
<select onchange="formatChanged(this);">
<option <%= request.format == Mime[:html] ? 'selected' : '' %> value="?part=text%2Fhtml">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[:html] ? 'selected' : '' %> value="?<%= part_query('text/html') %>">View as HTML email</option>
<option <%= request.format == Mime[:text] ? 'selected' : '' %> value="?<%= part_query('text/plain') %>">View as plain-text email</option>
</select>
</dd>
<% end %>
</dl>
</header>

<% 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 %>
<p>
You are trying to preview an email that does not have any content.
Expand Down
51 changes: 51 additions & 0 deletions railties/test/application/mailer_previews_test.rb
Expand Up @@ -485,6 +485,57 @@ def foo
assert_match '<li><a href="/my_app/rails/mailers/notifier/foo">foo</a></li>', last_response.body
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
image_file "pixel.png", "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEWzIioca/JlAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJgggo="

Expand Down

0 comments on commit 28e388a

Please sign in to comment.