Sha256: a5eacdbcaf164667f70d605a648e5d1e3129dc9e25777782a2feb10a96beb6aa

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

module LetterOpenerWebS3::MessageExtension
  extend ActiveSupport::Concern

  included do
    def initialize(location, mail, part = nil)
      @location = location.gsub("#{Rails.root.to_s}/", '')
      @mail = mail
      @part = part
      @attachments = []
    end

    def render
      if mail.attachments.any?
        attachments_dir = File.join(@location, 'attachments')
        # TODO: need check render with attachment
        mail.attachments.each do |attachment|
          filename = attachment.filename.gsub(/[^\w.]/, '_')
          path = File.join(attachments_dir, filename)

          object(path).put(body: attachment.body.raw_source, content_length: attachment.body.raw_source.size)

          @attachments << [attachment.filename, "attachments/#{URI.escape(filename)}"]
        end
      end

      str = ERB.new(template).result(binding)
      object(filepath).put(body: str, content_length: str.size)
    end

    def template
      letter_opener_path = $".select{|f| f.match(/letter_opener\/message.rb/)}.first
      File.read(File.join(letter_opener_path.gsub('message.rb', ''), 'message.html.erb'))
    end

    def body
      @body ||= begin
        body = (@part || @mail).decoded

        mail.attachments.each do |attachment|
          body.gsub!(attachment.url, "attachments/#{attachment.filename}")
        end

        body.gsub!(/=\"\/assets/, "=\"#{URI::join(LetterOpenerWebS3.domain, 'assets')}") if LetterOpenerWebS3.domain

        body
      end
    end

    private

    def object(filepath)
      LetterOpenerWebS3.bucket.object(filepath)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
letter_opener_web_s3-0.1.0 lib/letter_opener_web_s3/message_extension.rb