mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
565094a8b5
Rails 4.x and earlier didn't support `Mime::Type[:FOO]`, so libraries
that support multiple Rails versions would've had to feature-detect
whether to use `Mime::Type[:FOO]` or `Mime::FOO`.
`Mime[:foo]` has been around for ages to look up registered MIME types
by symbol / extension, though, so libraries and plugins can safely
switch to that without breaking backward- or forward-compatibility.
Note: `Mime::ALL` isn't a real MIME type and isn't registered for lookup
by type or extension, so it's not available as `Mime[:all]`. We use it
internally as a wildcard for `respond_to` negotiation. If you use this
internal constant, continue to reference it with `Mime::ALL`.
Ref. efc6dd550e
79 lines
2.3 KiB
Ruby
79 lines
2.3 KiB
Ruby
require 'rails/application_controller'
|
|
|
|
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
|
|
|
|
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}"
|
|
render action: 'mailer'
|
|
else
|
|
@email_action = File.basename(params[:path])
|
|
|
|
if @preview.email_exists?(@email_action)
|
|
@email = @preview.call(@email_action)
|
|
|
|
if params[:part]
|
|
part_type = Mime::Type.lookup(params[:part])
|
|
|
|
if part = find_part(part_type)
|
|
response.content_type = part_type
|
|
render plain: part.respond_to?(:decoded) ? part.decoded : part
|
|
else
|
|
raise AbstractController::ActionNotFound, "Email part '#{part_type}' not found in #{@preview.name}##{@email_action}"
|
|
end
|
|
else
|
|
@part = find_preferred_part(request.format, Mime[:html], Mime[:text])
|
|
render action: 'email', layout: false, formats: %w[html]
|
|
end
|
|
else
|
|
raise AbstractController::ActionNotFound, "Email '#{@email_action}' not found in #{@preview.name}"
|
|
end
|
|
end
|
|
end
|
|
|
|
protected
|
|
def show_previews?
|
|
ActionMailer::Base.show_previews
|
|
end
|
|
|
|
def find_preview
|
|
candidates = []
|
|
params[:path].to_s.scan(%r{/|$}){ candidates << $` }
|
|
preview = candidates.detect{ |candidate| ActionMailer::Preview.exists?(candidate) }
|
|
|
|
if preview
|
|
@preview = ActionMailer::Preview.find(preview)
|
|
else
|
|
raise AbstractController::ActionNotFound, "Mailer preview '#{params[:path]}' not found"
|
|
end
|
|
end
|
|
|
|
def find_preferred_part(*formats)
|
|
formats.each do |format|
|
|
if part = @email.find_first_mime_type(format)
|
|
return part
|
|
end
|
|
end
|
|
|
|
if formats.any?{ |f| @email.mime_type == f }
|
|
@email
|
|
end
|
|
end
|
|
|
|
def find_part(format)
|
|
if part = @email.find_first_mime_type(format)
|
|
part
|
|
elsif @email.mime_type == format
|
|
@email
|
|
end
|
|
end
|
|
end
|