Sha256: ca5af63db41c34997e1113f4e71a0b6b71c032a6940487f5e0c43e94e1d9f3ea

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

class RailsEmailPreview::EmailsController < RailsEmailPreview::ApplicationController
  include ERB::Util
  around_filter :set_locale_for_mail, only: :show_raw
  before_filter :load_preview_class, except: :index

  def index
    @preview_class_names = (RailsEmailPreview.preview_classes || []).map { |klass| klass.is_a?(String) ? klass : klass.name }
  end

  def show
    @email_locale = (params[:email_locale].presence || I18n.locale).to_s
    I18n.with_locale(@email_locale) do
      @part_type = params[:part_type] || 'text/html'
      @mail      = @preview_class.new.send(params[:mail_action])
    end
  end

  def show_raw
    @mail = @preview_class.new.send(params[:mail_action])
    RailsEmailPreview.run_before_render(@mail)
    if @part_type == 'raw'
      body = "<pre id='raw_message'>#{html_escape(@mail.to_s)}</pre>"
    else
      if @mail.multipart?
        body_part = (@part_type =~ /html/ ? @mail.html_part : @mail.text_part)
      else
        body_part = @mail
      end
      body = body_part.body
      if body_part.content_type =~ /plain/
        body = "<pre id='message_body'>#{body}</body>"
      end
    end

    render :text => body, :layout => false
  end

  private

  def set_locale_for_mail
    I18n.with_locale(params[:locale]) {
      yield if block_given?
    }
  end

  def load_preview_class
    @preview_class = (RailsEmailPreview.preview_classes || []).find { |pc|
      (pc.is_a?(String) ? pc : pc.name).underscore == params[:mail_class]
    }
    @preview_class = @preview_class.constantize if @preview_class.is_a?(String)
    @part_type = params[:part_type] || 'text/html'
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_email_preview-0.0.8 app/controllers/rails_email_preview/emails_controller.rb