Sha256: c9c81bd144907faf103e216d6e71595cfbdc80b0ec4f033c8d1c7184f9d1ffe7

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

module Maily
  class EmailsController < ApplicationController

    before_filter :allowed_action?, only: [:edit, :update, :deliver]
    before_filter :load_mailer_and_email, except: [:index, :edit, :update]
    around_filter :perform_with_locale, only: [:show, :raw, :deliver]

    def index
      @mailers = Maily::Mailer.all
    end

    def show
    end

    def raw
      content = if @email.parts.present?
        params[:part] == 'text' ? @email.text_part.body : @email.html_part.body
      else
        @email.body
      end

      render text: content, layout: false
    end

    def attachment
      attachment = @email.attachments.find { |elem| elem.filename == params[:attachment] }
      send_data attachment.body, filename: attachment.filename, type: attachment.content_type
    end

    def edit
      @email = File.read("#{Rails.root}/app/views/#{params[:mailer]}/#{params[:method]}.html.erb")
    end

    def update
      @email = File.open("#{Rails.root}/app/views/#{params[:mailer]}/#{params[:method]}.html.erb", 'w') do |f|
        f.write(params[:body])
      end

      redirect_to maily_email_path(mailer: params[:mailer], method: params[:method])
    end

    def deliver
      @email.to = params[:to]

      @email.deliver

      redirect_to maily_email_path(mailer: params[:mailer], method: params[:method])
    end

    private

    def allowed_action?
      Maily.allowed_action?(action_name) || raise("Maily: action #{action_name} not allowed!")
    end

    def load_mailer_and_email
      @mailer = Maily::Mailer.find(params[:mailer])
      @email = @mailer.find_email(params[:method]).call
    end

    def perform_with_locale
      I18n.with_locale(params[:locale]) do
        yield
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
maily-0.1.0 app/controllers/maily/emails_controller.rb