Skip to content

Commit

Permalink
Add locale selector to email preview (#31596)
Browse files Browse the repository at this point in the history
- Add set_locale to detect suitable locale
- Make feature compatible with Rails 5.x
  • Loading branch information
morygonzalez authored and rafaelfranca committed Jan 18, 2018
1 parent e0f0d71 commit 5fe603a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 18 deletions.
12 changes: 10 additions & 2 deletions railties/lib/rails/mailers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Rails::MailersController < Rails::ApplicationController # :nodoc:
prepend_view_path ActionDispatch::DebugExceptions::RESCUES_TEMPLATE_PATH

before_action :require_local!, unless: :show_previews?
before_action :find_preview, only: :preview
before_action :find_preview, :set_locale, only: :preview

helper_method :part_query
helper_method :part_query, :locale_query

def index
@previews = ActionMailer::Preview.all
Expand Down Expand Up @@ -84,4 +84,12 @@ def find_part(format) # :doc:
def part_query(mime_type)
request.query_parameters.merge(part: mime_type).to_query
end

def locale_query(locale)
request.query_parameters.merge(locale: locale).to_query
end

def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end
35 changes: 25 additions & 10 deletions railties/lib/rails/templates/rails/mailers/email.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,22 @@
<% end %>
<% if @email.multipart? %>
<dt>Format:</dt>
<dd>
<select onchange="formatChanged(this);">
<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 id="part" onchange="refreshBody();">
<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 %>
<% if I18n.available_locales.count > 1 %>
<dt>Locale:</dt>
<dd>
<select id="locale" onchange="refreshBody();">
<% I18n.available_locales.each do |locale| %>
<option <%= I18n.locale == locale ? 'selected' : '' %> value="<%= locale_query(locale) %>"><%= locale %></option>
<% end %>
</select>
</dd>
<% end %>
Expand All @@ -116,15 +128,18 @@
<% end %>

<script>
function formatChanged(form) {
var part_name = form.options[form.selectedIndex].value
var iframe =document.getElementsByName('messageBody')[0];
iframe.contentWindow.location.replace(part_name);
function refreshBody() {
var part_select = document.querySelector('select#part');
var locale_select = document.querySelector('select#locale');
var part_param = part_select.options[part_select.selectedIndex].value;
var locale_param = locale_select.options[locale_select.selectedIndex].value;
var iframe = document.getElementsByName('messageBody')[0];
iframe.contentWindow.location = '?' + part_param + '&' + locale_param;

if (history.replaceState) {
var url = location.pathname.replace(/\.(txt|html)$/, '');
var format = /html/.test(part_name) ? '.html' : '.txt';
window.history.replaceState({}, '', url + format);
var url = location.pathname.replace(/\.(txt|html)$/, '');
var format = /html/.test(part_param) ? '.html' : '.txt';
window.history.replaceState({}, '', url + format + '?' + locale_param);
}
}
</script>
Expand Down
68 changes: 62 additions & 6 deletions railties/test/application/mailer_previews_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,67 @@ 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="part=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="part=text%2Fplain">View as plain-text email</option>', last_response.body
end

test "locale menu selects correct option" do
app_file "config/initializers/available_locales.rb", <<-RUBY
Rails.application.configure do
config.i18n.available_locales = %i[en ja]
end
RUBY

mailer "notifier", <<-RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY

html_template "notifier/foo", <<-RUBY
<p>Hello, World!</p>
RUBY

text_template "notifier/foo", <<-RUBY
Hello, World!
RUBY

mailer_preview "notifier", <<-RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY

app("development")

get "/rails/mailers/notifier/foo.html"
assert_equal 200, last_response.status
assert_match '<option selected value="locale=en">en', last_response.body
assert_match '<option value="locale=ja">ja', last_response.body

get "/rails/mailers/notifier/foo.html?locale=ja"
assert_equal 200, last_response.status
assert_match '<option value="locale=en">en', last_response.body
assert_match '<option selected value="locale=ja">ja', last_response.body

get "/rails/mailers/notifier/foo.txt"
assert_equal 200, last_response.status
assert_match '<option selected value="locale=en">en', last_response.body
assert_match '<option value="locale=ja">ja', last_response.body

get "/rails/mailers/notifier/foo.txt?locale=ja"
assert_equal 200, last_response.status
assert_match '<option value="locale=en">en', last_response.body
assert_match '<option selected value="locale=ja">ja', last_response.body
end

test "mailer previews create correct links when loaded on a subdirectory" do
Expand Down Expand Up @@ -520,8 +576,8 @@ def foo
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
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
Expand All @@ -530,8 +586,8 @@ def foo
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
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
Expand Down

0 comments on commit 5fe603a

Please sign in to comment.