From 5fe603ac288d4432be77cfa851e86ad66fd48d53 Mon Sep 17 00:00:00 2001 From: Hitoshi Nakashima Date: Fri, 19 Jan 2018 05:22:10 +0900 Subject: [PATCH] Add locale selector to email preview (#31596) - Add set_locale to detect suitable locale - Make feature compatible with Rails 5.x --- railties/lib/rails/mailers_controller.rb | 12 +++- .../templates/rails/mailers/email.html.erb | 35 +++++++--- .../test/application/mailer_previews_test.rb | 68 +++++++++++++++++-- 3 files changed, 97 insertions(+), 18 deletions(-) diff --git a/railties/lib/rails/mailers_controller.rb b/railties/lib/rails/mailers_controller.rb index 66636e5d6b..0b0e802358 100644 --- a/railties/lib/rails/mailers_controller.rb +++ b/railties/lib/rails/mailers_controller.rb @@ -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 @@ -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 diff --git a/railties/lib/rails/templates/rails/mailers/email.html.erb b/railties/lib/rails/templates/rails/mailers/email.html.erb index 89c1129f90..44c400a7e6 100644 --- a/railties/lib/rails/templates/rails/mailers/email.html.erb +++ b/railties/lib/rails/templates/rails/mailers/email.html.erb @@ -96,10 +96,22 @@ <% end %> <% if @email.multipart? %> +
Format:
- + + + +
+ <% end %> + + <% if I18n.available_locales.count > 1 %> +
Locale:
+
+
<% end %> @@ -116,15 +128,18 @@ <% end %> diff --git a/railties/test/application/mailer_previews_test.rb b/railties/test/application/mailer_previews_test.rb index 4e77cece1b..288968484c 100644 --- a/railties/test/application/mailer_previews_test.rb +++ b/railties/test/application/mailer_previews_test.rb @@ -450,11 +450,67 @@ def foo get "/rails/mailers/notifier/foo.html" assert_equal 200, last_response.status - assert_match '', last_response.body + assert_match '', last_response.body get "/rails/mailers/notifier/foo.txt" assert_equal 200, last_response.status - assert_match '', last_response.body + assert_match '', 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 +

Hello, World!

+ 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 '