Module: InlineStylesMailer

Defined in:
lib/inline_styles_mailer.rb,
lib/inline_styles_mailer/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.0.8"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/inline_styles_mailer.rb', line 7

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#layout_to_useObject



94
95
96
97
98
99
100
101
# File 'lib/inline_styles_mailer.rb', line 94

def layout_to_use
  case _layout
  when ActionView::Template
    _layout.inspect.split("/").last.split(".").first
  when String
    _layout.split("/").last.split(".").first
  end
end

#mail(options, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/inline_styles_mailer.rb', line 56

def mail(options, &block)
  if block
    super(mail, &block) # We'll just let this pass through
  else
    super(options) do |format|
      # Rails 3.1 takes an array, while Rails 3.0 takes a string.
      # See https://github.com/billhorsman/inline_styles_mailer/issues/1
      prefixes = options[:template_path] || self.class.name.underscore
      prefixes = [prefixes] unless Rails.version =~ /^3\.0/
      templates = lookup_context.find_all(options[:template_name] || action_name, prefixes)
      options.reverse_merge!(:mime_version => "1.0", :charset => "UTF-8", :content_type => "text/plain", :parts_order => [ "text/plain", "text/enriched", "text/html"])
      templates.sort_by {|t|
        # Rails 4.1 use #type but earlier versions use #mime_type
        mime_type = t.respond_to?(:mime_type) ? t.mime_type : t.type
        i = options[:parts_order].index(mime_type)
        i > -1 ? i : 99
      }.each do |template|
      # templates.each do |template|
        # e.g. template = app/views/user_mailer/welcome.html.erb
        template_path = template.inspect.split("/").slice(-2, 2).join("/") # e.g. user_mailer/welcome.html.erb
        parts = template_path.split('.')
        handler = parts.pop.to_sym # e.g. erb
        extension = parts.pop.to_sym # e.g. html
        file = parts.join('.') # e.g. user_mailer/welcome (you get a deprecation warning if you don't strip off the format and handler)
        format.send(extension) do
          case extension
          when :html
            html = render_to_string :file => file, :layout => layout_to_use, handlers: [handler]
            render :text => self.class.page.with_html(html).apply
          else
            render
          end
        end
      end
    end
  end
end