Sha256: 53ab8434e570f32ccc4f82a28c217e11dfa01af1cb1bfc4cb0656eb1aa14d253

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module BootstrapEmail
  module Rails
    class MailBuilder
      attr_reader :mail, :bootstrap_email

      def self.perform(mail)
        new(mail) if mail
      end

      private

      def initialize(mail)
        @mail = mail
        @bootstrap_email = BootstrapEmail::Compiler.new(html_part, type: :string)
        perform
      end

      def perform
        add_mail_parts
        mail
      end

      def html_part
        (mail.html_part || mail).body.raw_source
      end

      def add_mail_parts
        if BootstrapEmail.static_config.generate_rails_text_part
          mail.parts << build_alternative_part
        else
          html = bootstrap_email.perform_full_compile
          mail.parts << build_html_part(html)
        end
      end

      def build_alternative_part
        compiled = bootstrap_email.perform_multipart_compile

        part = Mail::Part.new(content_type: 'multipart/alternative')
        part.add_part(build_text_part(compiled[:text]))
        part.add_part(build_html_part(compiled[:html]))

        part
      end

      def build_html_part(html)
        Mail::Part.new do
          content_type "text/html; charset=#{html.encoding}"
          body html
        end
      end

      def build_text_part(text)
        Mail::Part.new do
          content_type "text/plain; charset=#{text.encoding}"
          body text
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bootstrap-email-1.3.0 lib/bootstrap-email/rails/mail_builder.rb