Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add locale selector to email preview #19923

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions railties/lib/rails/mailers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Rails::MailersController < Rails::ApplicationController # :nodoc:

before_action :require_local!
before_action :find_preview, only: :preview
before_action :set_locale

def index
@previews = ActionMailer::Preview.all
Expand Down Expand Up @@ -72,4 +73,8 @@ def find_part(format)
@email
end
end

def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end
30 changes: 26 additions & 4 deletions railties/lib/rails/templates/rails/mailers/email.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@
width: 100%;
}
</style>
<script type="text/javascript">
function refreshMailBody() {
part_select = document.querySelectorAll('select#part')[0];
locale_select = document.querySelectorAll('select#locale')[0];

part_param = 'part=' + part_select.options[part_select.selectedIndex].value;
locale_param = 'locale=' + locale_select.options[locale_select.selectedIndex].value;

document.getElementsByName('mailBody')[0].src = '?' + part_param + '&' + locale_param;
}
</script>
</head>

<body>
Expand Down Expand Up @@ -94,17 +105,28 @@

<% if @email.multipart? %>
<dd>
<select onchange="document.getElementsByName('messageBody')[0].src=this.options[this.selectedIndex].value;">
<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>
<select id="part" onchange="refreshMailBody();">
<option <%= request.format == Mime::HTML ? 'selected' : '' %> value="text%2Fhtml">View as HTML email</option>
<option <%= request.format == Mime::TEXT ? 'selected' : '' %> value="text%2Fplain">View as plain-text email</option>
</select>
</dd>
<% end %>

<% if I18n.available_locales.count > 1 %>
<dt>Locale:</dt>
<dd>
<select id="locale" onchange="refreshMailBody();">
<% I18n.available_locales.each do |locale| %>
<option <%= I18n.locale == locale ? 'selected' : '' %> value="<%= locale %>"><%= locale %></option>
<% end %>
</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="mailBody" src="?part=<%= Rack::Utils.escape(@part.mime_type) %>&locale=<%= I18n.locale %>"></iframe>
<% else %>
<p>
You are trying to preview an email that does not have any content.
Expand Down
12 changes: 6 additions & 6 deletions railties/test/application/mailer_previews_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,11 @@ def foo

get "/rails/mailers/notifier/foo.html"
assert_equal 200, last_response.status
assert_match '<option selected value="?part=text%2Fhtml">View as HTML email</option>', last_response.body
assert_match '<option selected value="text%2Fhtml">View as HTML email</option>', last_response.body

get "/rails/mailers/notifier/foo.txt"
assert_equal 200, last_response.status
assert_match '<option selected value="?part=text%2Fplain">View as plain-text email</option>', last_response.body
assert_match '<option selected value="text%2Fplain">View as plain-text email</option>', last_response.body
end

test "mailer previews create correct links when loaded on a subdirectory" do
Expand Down Expand Up @@ -516,7 +516,7 @@ def foo

get "/rails/mailers/notifier/foo"
assert_equal 200, last_response.status
assert_match %r[<iframe seamless name="messageBody"], last_response.body
assert_match %r[<iframe seamless name="mailBody"], last_response.body

get "/rails/mailers/notifier/foo?part=text/plain"
assert_equal 200, last_response.status
Expand Down Expand Up @@ -557,7 +557,7 @@ def foo

get "/rails/mailers/notifier/foo"
assert_equal 200, last_response.status
assert_match %r[<iframe seamless name="messageBody"], last_response.body
assert_match %r[<iframe seamless name="mailBody"], last_response.body

get "/rails/mailers/notifier/foo?part=text/plain"
assert_equal 200, last_response.status
Expand Down Expand Up @@ -603,7 +603,7 @@ def foo

get "/rails/mailers/notifier/foo"
assert_equal 200, last_response.status
assert_match %r[<iframe seamless name="messageBody"], last_response.body
assert_match %r[<iframe seamless name="mailBody"], last_response.body

get "/rails/mailers/notifier/foo?part=text/plain"
assert_equal 200, last_response.status
Expand Down Expand Up @@ -661,7 +661,7 @@ def foo

get "/rails/mailers/notifier/foo"
assert_equal 200, last_response.status
assert_match %r[<iframe seamless name="messageBody"], last_response.body
assert_match %r[<iframe seamless name="mailBody"], last_response.body

get "/rails/mailers/notifier/foo?part=text/plain"
assert_equal 200, last_response.status
Expand Down