Module: Waves::Helpers::AssetHelper

Included in:
Erubis::Context
Defined in:
lib/helpers/asset_helper.rb

Instance Method Summary collapse

Instance Method Details

#image_path(source) ⇒ Object

Computes the path to an image asset in the public images directory. Full paths from the document root will be passed through. Used internally by image_tag to build the image path. Passing a filename without an extension is deprecated.

image_path("edit.png")  # => /images/edit.png
image_path("icons/edit.png")  # => /images/icons/edit.png
image_path("/icons/edit.png")  # => /icons/edit.png


44
45
46
# File 'lib/helpers/asset_helper.rb', line 44

def image_path(source)
  compute_public_path(source, 'images', 'png')
end

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

Returns an html image tag for the source. The source can be a full path or a file that exists in your public images directory. Note that specifying a filename without the extension is now deprecated in Rails. You can add html attributes using the options. The options supports two additional keys for convienence and conformance:

  • :alt - If no alt text is given, the file name part of the source is used (capitalized and without the extension)

  • :size - Supplied as “WidthxHeight”, so “30x45” becomes width=“30” and height=“45”. :size will be ignored if the value is not in the correct format.

image_tag("icon.png")  # =>
  <img src="/images/icon.png" alt="Icon" />
image_tag("icon.png", :size => "16x10", :alt => "Edit Entry")  # =>
  <img src="/images/icon.png" width="16" height="10" alt="Edit Entry" />
image_tag("/icons/icon.gif", :size => "16x16")  # =>
  <img src="/icons/icon.gif" width="16" height="16" alt="Icon" />


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/helpers/asset_helper.rb', line 22

def image_tag(source, options = {})
  options.symbolize_keys!
    
  options[:src] = image_path(source)
  options[:alt] ||= File.basename(options[:src], '.*').split('.').first.capitalize
  
  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