Module: ActionView::Helpers::AssetTagHelper

Defined in:
lib/inline_attachment.rb

Instance Method Summary collapse

Instance Method Details

#image_tag(source, options = {}) ⇒ Object



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
# File 'lib/inline_attachment.rb', line 69

def image_tag(source, options = {})
  options.symbolize_keys!
  
  @part_container ||= @controller
     
  if @part_container.is_a?(ActionMailer::Base) or @part_container.is_a?(ActionMailer::Part)
    file_path = "#{RAILS_ROOT}/public#{image_path(source).split('?').first}"
    basename  = File.basename(file_path, '.*')
    ext       = basename.split('.').last
    cid       = Time.now.to_f.to_s + "#{basename}@inline_attachment"
    file      = File.open(file_path, 'rb')
    
    @part_container.inline_attachment(:content_type => "image/#{ext}",
                                  :body         => file.read,
                                  :filename     => basename,
                                  :cid          => "<#{cid}>",
                                  :disposition  => "inline")
    
    options[:src] = "cid:#{cid}"
    options[:alt] = basename.split('.').first.capitalize
  else
    options[:src] = image_path(source)
    options[:alt] ||= File.basename(options[:src], '.*').split('.').first.capitalize
  end
  
  if options[:size]
    options[:width], options[:height] = options[:size].split("x") if options[:size] =~ %r{^\d+x\d+$}
    options.delete(:size)
  end

  tag("img", options)
end