2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-21 09:08:33 -04:00
|
|
|
require "rails/application_controller"
|
2013-12-16 00:52:58 -05:00
|
|
|
|
|
|
|
class Rails::MailersController < Rails::ApplicationController # :nodoc:
|
2018-06-14 04:09:00 -04:00
|
|
|
prepend_view_path ActionDispatch::DebugView::RESCUES_TEMPLATE_PATH
|
2013-12-16 00:52:58 -05:00
|
|
|
|
2019-04-16 14:54:51 -04:00
|
|
|
around_action :set_locale, only: :preview
|
|
|
|
before_action :find_preview, only: :preview
|
2015-07-15 15:04:21 -04:00
|
|
|
before_action :require_local!, unless: :show_previews?
|
2013-12-16 00:52:58 -05:00
|
|
|
|
2018-01-18 15:22:10 -05:00
|
|
|
helper_method :part_query, :locale_query
|
2016-05-29 20:15:18 -04:00
|
|
|
|
2018-07-31 01:37:34 -04:00
|
|
|
content_security_policy(false)
|
|
|
|
|
2013-12-16 00:52:58 -05:00
|
|
|
def index
|
|
|
|
@previews = ActionMailer::Preview.all
|
|
|
|
@page_title = "Mailer Previews"
|
|
|
|
end
|
|
|
|
|
|
|
|
def preview
|
|
|
|
if params[:path] == @preview.preview_name
|
|
|
|
@page_title = "Mailer Previews for #{@preview.preview_name}"
|
2016-08-06 13:15:47 -04:00
|
|
|
render action: "mailer"
|
2013-12-16 00:52:58 -05:00
|
|
|
else
|
2015-04-27 04:15:52 -04:00
|
|
|
@email_action = File.basename(params[:path])
|
2013-12-16 00:52:58 -05:00
|
|
|
|
2015-04-27 04:15:52 -04:00
|
|
|
if @preview.email_exists?(@email_action)
|
2019-09-02 17:44:46 -04:00
|
|
|
@page_title = "Mailer Preview for #{@preview.preview_name}##{@email_action}"
|
2016-05-29 20:15:18 -04:00
|
|
|
@email = @preview.call(@email_action, params)
|
2013-12-16 00:52:58 -05:00
|
|
|
|
|
|
|
if params[:part]
|
|
|
|
part_type = Mime::Type.lookup(params[:part])
|
|
|
|
|
|
|
|
if part = find_part(part_type)
|
|
|
|
response.content_type = part_type
|
2015-08-06 02:50:28 -04:00
|
|
|
render plain: part.respond_to?(:decoded) ? part.decoded : part
|
2013-12-16 00:52:58 -05:00
|
|
|
else
|
2015-04-27 04:15:52 -04:00
|
|
|
raise AbstractController::ActionNotFound, "Email part '#{part_type}' not found in #{@preview.name}##{@email_action}"
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
else
|
2015-10-05 01:14:04 -04:00
|
|
|
@part = find_preferred_part(request.format, Mime[:html], Mime[:text])
|
2019-03-18 19:06:22 -04:00
|
|
|
render action: "email", layout: false, formats: [:html]
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
else
|
2015-04-27 04:15:52 -04:00
|
|
|
raise AbstractController::ActionNotFound, "Email '#{@email_action}' not found in #{@preview.name}"
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
private
|
|
|
|
def show_previews? # :doc:
|
2015-07-15 15:04:21 -04:00
|
|
|
ActionMailer::Base.show_previews
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def find_preview # :doc:
|
2013-12-16 00:52:58 -05:00
|
|
|
candidates = []
|
2016-08-16 03:30:11 -04:00
|
|
|
params[:path].to_s.scan(%r{/|$}) { candidates << $` }
|
|
|
|
preview = candidates.detect { |candidate| ActionMailer::Preview.exists?(candidate) }
|
2013-12-16 00:52:58 -05:00
|
|
|
|
|
|
|
if preview
|
|
|
|
@preview = ActionMailer::Preview.find(preview)
|
|
|
|
else
|
|
|
|
raise AbstractController::ActionNotFound, "Mailer preview '#{params[:path]}' not found"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def find_preferred_part(*formats) # :doc:
|
2015-05-04 03:02:36 -04:00
|
|
|
formats.each do |format|
|
|
|
|
if part = @email.find_first_mime_type(format)
|
|
|
|
return part
|
2013-12-16 00:52:58 -05:00
|
|
|
end
|
2015-05-04 03:02:36 -04:00
|
|
|
end
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
if formats.any? { |f| @email.mime_type == f }
|
2013-12-16 00:52:58 -05:00
|
|
|
@email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def find_part(format) # :doc:
|
2015-05-04 03:02:36 -04:00
|
|
|
if part = @email.find_first_mime_type(format)
|
|
|
|
part
|
2013-12-16 00:52:58 -05:00
|
|
|
elsif @email.mime_type == format
|
|
|
|
@email
|
|
|
|
end
|
|
|
|
end
|
2016-05-29 20:15:18 -04:00
|
|
|
|
|
|
|
def part_query(mime_type)
|
|
|
|
request.query_parameters.merge(part: mime_type).to_query
|
|
|
|
end
|
2018-01-18 15:22:10 -05:00
|
|
|
|
|
|
|
def locale_query(locale)
|
|
|
|
request.query_parameters.merge(locale: locale).to_query
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_locale
|
2019-04-16 14:54:51 -04:00
|
|
|
I18n.with_locale(params[:locale] || I18n.default_locale) do
|
|
|
|
yield
|
|
|
|
end
|
2018-01-18 15:22:10 -05:00
|
|
|
end
|
2014-05-27 18:08:23 -04:00
|
|
|
end
|