Module: InlineStylesMailer

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

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"1.0.1"

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

#call_layoutObject

Hack to call _layout the right way depending on the Rails version. This is a code smell telling us that we shouldn’t be doing this at all…



106
107
108
109
110
111
112
113
114
# File 'lib/inline_styles_mailer.rb', line 106

def call_layout
  if method(:_layout).arity == 1
    # Rails 5?
    _layout([:html])
  else
    # < Rails 5?
    _layout
  end
end

#layout_to_useObject



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

def layout_to_use
  case call_layout
  when ActionView::Template
    call_layout.inspect.split("/").last.split(".").first
  when String
    call_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
93
# 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
        # e.g. template = app/views/namespace/user_mailer/welcome.html.erb
        template_path = template.inspect.split("views")[1][1..-1] # 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