Added ability to download `.eml` file for the email preview.

This commit is contained in:
Igor Kasyanchuk 2022-09-10 00:01:06 +03:00 committed by admin
parent 37492e2569
commit 9f0a064fde
No known key found for this signature in database
GPG Key ID: 9C142908EF335A73
5 changed files with 38 additions and 3 deletions

View File

@ -1,3 +1,7 @@
* Added ability to download `.eml` file for the email preview.
*Igor Kasyanchuk*
* Support multiple preview paths for mailers.
Option `config.action_mailer.preview_path` is deprecated in favor of

View File

@ -80,7 +80,8 @@ module ActionMailer
if options.show_previews
app.routes.prepend do
get "/rails/mailers" => "rails/mailers#index", internal: true
get "/rails/mailers" => "rails/mailers#index", internal: true
get "/rails/mailers/download/*path" => "rails/mailers#download", internal: true
get "/rails/mailers/*path" => "rails/mailers#preview", internal: true
end
end

View File

@ -5,8 +5,8 @@ require "rails/application_controller"
class Rails::MailersController < Rails::ApplicationController # :nodoc:
prepend_view_path ActionDispatch::DebugView::RESCUES_TEMPLATE_PATH
around_action :set_locale, only: :preview
before_action :find_preview, only: :preview
around_action :set_locale, only: [:preview, :download]
before_action :find_preview, only: [:preview, :download]
before_action :require_local!, unless: :show_previews?
helper_method :part_query, :locale_query
@ -18,6 +18,16 @@ class Rails::MailersController < Rails::ApplicationController # :nodoc:
@page_title = "Mailer Previews"
end
def download
@email_action = File.basename(params[:path])
if @preview.email_exists?(@email_action)
@email = @preview.call(@email_action, params)
send_data @email.to_s, filename: "#{@email_action}.eml"
else
raise AbstractController::ActionNotFound, "Email '#{@email_action}' not found in #{@preview.name}"
end
end
def preview
if params[:path] == @preview.preview_name
@page_title = "Mailer Previews for #{@preview.preview_name}"

View File

@ -125,6 +125,8 @@
</select>
</dd>
<% end %>
<dt>EML File:</dt>
<dd><%= link_to "Download", action: :download %></dd>
</dl>
</header>

View File

@ -355,6 +355,10 @@ module ApplicationTests
get "/rails/mailers/notifier/bar"
assert_predicate last_response, :not_found?
assert_match "Email &#39;bar&#39; not found in NotifierPreview", h(last_response.body)
get "/rails/mailers/download/notifier/bar"
assert_predicate last_response, :not_found?
assert_match "Email &#39;bar&#39; not found in NotifierPreview", h(last_response.body)
end
test "mailer preview NullMail" do
@ -444,6 +448,13 @@ module ApplicationTests
assert_match "Ruby on Rails &lt;core@rubyonrails.org&gt;", last_response.body
assert_match "Andrew White &lt;andyw@pixeltrix.co.uk&gt;", last_response.body
assert_match "David Heinemeier Hansson &lt;david@heinemeierhansson.com&gt;", last_response.body
get "/rails/mailers/download/notifier/foo"
email = Mail.read_from_string(last_response.body)
assert_equal "attachment; filename=\"foo.eml\"; filename*=UTF-8''foo.eml", last_response.headers["Content-Disposition"]
assert_equal 200, last_response.status
assert_equal ["andyw@pixeltrix.co.uk"], email.to
assert_equal ["david@heinemeierhansson.com"], email.cc
end
test "part menu selects correct option" do
@ -663,6 +674,13 @@ module ApplicationTests
get "/rails/mailers/notifier/foo?part=text/plain"
assert_equal 200, last_response.status
assert_match %r[Hello, World!], last_response.body
get "/rails/mailers/download/notifier/foo"
assert_equal 200, last_response.status
email = Mail.read_from_string(last_response.body)
assert_equal 2, email.parts.size
assert_equal "text/plain; charset=UTF-8", email.parts[0].content_type
assert_equal "image/png; filename=pixel.png", email.parts[1].content_type
end
test "multipart mailer preview with attachment" do