File: mailers_controller.rb

package info (click to toggle)
rails 2:4.1.8-1+deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 22,132 kB
  • ctags: 27,642
  • sloc: ruby: 172,886; sql: 43; yacc: 43; sh: 14; makefile: 12
file content (73 lines) | stat: -rw-r--r-- 2,175 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'rails/application_controller'

class Rails::MailersController < Rails::ApplicationController # :nodoc:
  prepend_view_path ActionDispatch::DebugExceptions::RESCUES_TEMPLATE_PATH

  before_filter :require_local!
  before_filter :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 = File.basename(params[:path])

      if @preview.email_exists?(email)
        @email = @preview.call(email)

        if params[:part]
          part_type = Mime::Type.lookup(params[:part])

          if part = find_part(part_type)
            response.content_type = part_type
            render text: part.respond_to?(:decoded) ? part.decoded : part
          else
            raise AbstractController::ActionNotFound, "Email part '#{part_type}' not found in #{@preview.name}##{email}"
          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}' not found in #{@preview.name}"
      end
    end
  end

  protected
    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)
      if @email.multipart?
        formats.each do |format|
          return find_part(format) if @email.parts.any?{ |p| p.mime_type == format }
        end
      else
        @email
      end
    end

    def find_part(format)
      if @email.multipart?
        @email.parts.find{ |p| p.mime_type == format }
      elsif @email.mime_type == format
        @email
      end
    end
end