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.3"

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…



114
115
116
117
118
119
120
121
122
# File 'lib/inline_styles_mailer.rb', line 114

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

#layout_to_useObject



103
104
105
106
107
108
109
110
# File 'lib/inline_styles_mailer.rb', line 103

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



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
94
95
96
97
98
99
100
101
# File 'lib/inline_styles_mailer.rb', line 59

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].flatten 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]
            # Rails 5.1 removed render :text
            if Gem.loaded_specs['rails'].version >= Gem::Version.create('5.0')
              render :plain => self.class.page.with_html(html).apply
            else
              render :text => self.class.page.with_html(html).apply
            end
          else
            render
          end
        end
      end
    end
  end
end