Module: Jammit::Helper

Defined in:
lib/jammit/helper.rb

Overview

The Jammit::Helper module, which is made available to every view, provides helpers for writing out HTML tags for asset packages. In development you get the ordered list of source files — in any other environment, a link to the cached packages.

Constant Summary

DATA_URI_START =
"<!--[if (!IE)|(gte IE 8)]><!-->"
DATA_URI_END =
"<!--<![endif]-->"
MHTML_START =
"<!--[if lte IE 7]>"
MHTML_END =
"<![endif]-->"

Method Summary

Method Details

- (Object) include_javascripts(*packages)

Writes out the URL to the bundled and compressed javascript package, except in development, where it references the individual scripts.



27
28
29
30
31
32
# File 'lib/jammit/helper.rb', line 27

def include_javascripts(*packages)
  tags = packages.map do |pack|
    Jammit.package_assets ? Jammit.asset_url(pack, :js) : Jammit.packager.individual_urls(pack.to_sym, :js)
  end
  javascript_include_tag(tags.flatten)
end

- (Object) include_stylesheets(*packages)

If embed_assets is turned on, writes out links to the Data-URI and MHTML versions of the stylesheet package, otherwise the package is regular compressed CSS, and in development the stylesheet URLs are passed verbatim.



17
18
19
20
21
22
23
# File 'lib/jammit/helper.rb', line 17

def include_stylesheets(*packages)
  options = packages.extract_options!
  return individual_stylesheets(packages, options) unless Jammit.package_assets
  disabled = (options.delete(:embed_assets) == false) || (options.delete(:embed_images) == false)
  return packaged_stylesheets(packages, options) if disabled || !Jammit.embed_assets
  return embedded_image_stylesheets(packages, options)
end

- (Object) include_templates(*packages)

Writes out the URL to the concatenated and compiled JST file — we always have to pre-process it, even in development.



36
37
38
# File 'lib/jammit/helper.rb', line 36

def include_templates(*packages)
  javascript_include_tag(packages.map {|pack| Jammit.asset_url(pack, :jst) })
end

- (Object) embedded_image_stylesheets(packages, options) (private)

HTML tags for the ‘datauri’, and ‘mhtml’ versions of the packaged stylesheets, using conditional comments to load the correct variant.



55
56
57
58
59
60
61
# File 'lib/jammit/helper.rb', line 55

def embedded_image_stylesheets(packages, options)
  datauri_tags = tags_with_options(packages, options) {|p| Jammit.asset_url(p, :css, :datauri) }
  ie_tags = Jammit.mhtml_enabled ?
            tags_with_options(packages, options) {|p| Jammit.asset_url(p, :css, :mhtml) } :
            packaged_stylesheets(packages, options)
  [DATA_URI_START, datauri_tags, DATA_URI_END, MHTML_START, ie_tags, MHTML_END].join("\n")
end

- (Object) individual_stylesheets(packages, options) (private)

HTML tags, in order, for all of the individual stylesheets.



44
45
46
# File 'lib/jammit/helper.rb', line 44

def individual_stylesheets(packages, options)
  tags_with_options(packages, options) {|p| Jammit.packager.individual_urls(p.to_sym, :css) }
end

- (Object) packaged_stylesheets(packages, options) (private)

HTML tags for the stylesheet packages.



49
50
51
# File 'lib/jammit/helper.rb', line 49

def packaged_stylesheets(packages, options)
  tags_with_options(packages, options) {|p| Jammit.asset_url(p, :css) }
end

- (Object) tags_with_options(packages, options) (private)

Generate the stylesheet tags for a batch of packages, with options, by yielding each package to a block.



65
66
67
68
69
70
# File 'lib/jammit/helper.rb', line 65

def tags_with_options(packages, options)
  packages = packages.dup
  packages.map! {|package| yield package }.flatten!
  packages.push(options) unless options.empty?
  stylesheet_link_tag(*packages)
end