Class: RailsEmailPreview::EmailsController

Inherits:
ApplicationController show all
Includes:
ERB::Util
Defined in:
app/controllers/rails_email_preview/emails_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject

List of emails



11
12
13
14
# File 'app/controllers/rails_email_preview/emails_controller.rb', line 11

def index
  @previews = Preview.all
  @list     = PreviewListPresenter.new(@previews)
end

#showObject

Preview an email



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/rails_email_preview/emails_controller.rb', line 17

def show
  prevent_browser_caching
  cms_edit_links!
  with_email_locale do
    if @preview.respond_to?(:preview_mail)
      @mail, body     = mail_and_body
      @mail_body_html = render_to_string(html: body, layout: 'rails_email_preview/email')
    else
      raise ArgumentError.new("#{@preview} is not a preview class, does not respond_to?(:preview_mail)")
    end
  end
end

#show_attachmentObject

Download attachment



50
51
52
53
54
55
56
# File 'app/controllers/rails_email_preview/emails_controller.rb', line 50

def show_attachment
  with_email_locale do
    filename   = "#{params[:filename]}.#{params[:format]}"
    attachment = preview_mail(false).attachments.find { |a| a.filename == filename }
    send_data attachment.body.raw_source, filename: filename
  end
end

#show_bodyObject

Render email body iframe HTML. Used by the CMS integration to provide a link back to Show from Edit.



65
66
67
68
69
70
71
72
# File 'app/controllers/rails_email_preview/emails_controller.rb', line 65

def show_body
  prevent_browser_caching
  cms_edit_links!
  with_email_locale do
    _, body = mail_and_body
    render html: body, layout: 'rails_email_preview/email'
  end
end

#show_headersObject

Render headers partial. Used by the CMS integration to refetch headers after editing.



59
60
61
62
# File 'app/controllers/rails_email_preview/emails_controller.rb', line 59

def show_headers
  mail = with_email_locale { mail_and_body.first }
  render partial: 'rails_email_preview/emails/headers', locals: {mail: mail}
end

#test_deliverObject

Really deliver an email



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/rails_email_preview/emails_controller.rb', line 31

def test_deliver
  redirect_url = rails_email_preview.rep_email_url(preview_params.except(:recipient_email))
  if (address = params[:recipient_email]).blank? || address !~ /@/
    redirect_to redirect_url, alert: t('rep.test_deliver.provide_email')
    return
  end
  with_email_locale do
    delivery_handler = RailsEmailPreview::DeliveryHandler.new(preview_mail, to: address, cc: nil, bcc: nil)
    deliver_email!(delivery_handler.mail)
  end
  delivery_method = Rails.application.config.action_mailer.delivery_method
  if delivery_method
    redirect_to redirect_url, notice: t('rep.test_deliver.sent_notice', address: address, delivery_method: delivery_method)
  else
    redirect_to redirect_url, alert: t('rep.test_deliver.no_delivery_method', environment: Rails.env)
  end
end