Module AssetHatHelper

  1. app/helpers/asset_hat_helper.rb

Helpers for use in layouts for global includes, and in views for view-specific includes.

Methods

public instance

  1. include_css
  2. include_js

Constants

RAILS_ROOT = File.join(File.dirname(__FILE__), '..', '..')

Public instance methods

include_css (*args)

include_css is a smart wrapper for Rails’ stylesheet_link_tag. The two can be used together while migrating to AssetHat.

Include a single stylesheet:

include_css 'diagnostics'
=>  <link href="/stylesheets/diagnostics.min.css" media="screen,projection" rel="stylesheet" type="text/css" />

Include a single unminified stylesheet:

include_css 'diagnostics.css'
=>  <link href="/stylesheets/diagnostics.css" media="screen,projection" rel="stylesheet" type="text/css" />

Include a bundle of stylesheets (i.e., a concatenated set of stylesheets; configure in config/assets.yml):

include_css :bundle => 'application'
=>  <link href="/stylesheets/bundles/application.min.css" ... />

Include multiple stylesheets separately (not as cool):

include_css 'reset', 'application', 'clearfix'
=>  <link href="/stylesheets/reset.min.css" ... />
    <link href="/stylesheets/application.min.css" ... />
    <link href="/stylesheets/clearfix.min.css" ... />
[show source]
     # File app/helpers/asset_hat_helper.rb, line 122
122:   def include_css(*args)
123:     return if args.blank?
124: 
125:     AssetHat.html_cache       ||= {}
126:     AssetHat.html_cache[:css] ||= {}
127:     cache_key = args.inspect
128: 
129:     if !AssetHat.cache? || AssetHat.html_cache[:css][cache_key].blank?
130:       # Generate HTML and write to cache
131:       html = AssetHat.html_cache[:css][cache_key] = include_assets(:css, *args)
132:     end
133: 
134:     html ||= AssetHat.html_cache[:css][cache_key]
135:     html
136:   end
include_js (*args)

include_js is a smart wrapper for Rails’ javascript_include_tag. The two can be used together while migrating to AssetHat.

Include a single JS file:

include_js 'application'
=>  <script src="/javascripts/application.min.js" type="text/javascript"></script>

Include a single JS unminified file:

include_js 'application.js'
=>  <script src="/javascripts/application.js" type="text/javascript"></script>

Include jQuery:

include_js :jquery  # Development/test environment
=>  <script src="/javascripts/jquery-VERSION.min.js" ...></script>
include_js :jquery  # Staging/production environment
=>  <script src="http://ajax.googleapis.com/.../jquery.min.js" ...></script>
  # Set jQuery versions either in `config/assets.yml`, or by using
  # `include_js :jquery, :version => '1.4'`.

Include a bundle of JS files (i.e., a concatenated set of files; configure in config/assets.yml):

include_js :bundle => 'application'
=>  <script src="/javascripts/bundles/application.min.js" ...></script>

Include multiple bundles of JS files:

include_js :bundles => %w[plugins common]
=>  <script src="/javascripts/bundles/plugins.min.js" ...></script>
    <script src="/javascripts/bundles/common.min.js" ...></script>

Include multiple JS files separately (not as cool):

include_js 'bloombox', 'jquery.cookie', 'jquery.json.min'
=>  <script src="/javascripts/bloombox.min.js" ...></script>
    <script src="/javascripts/jquery.cookie.min.js" ...></script>
    <script src="/javascripts/jquery.json.min.js" ...></script>
[show source]
     # File app/helpers/asset_hat_helper.rb, line 173
173:   def include_js(*args)
174:     return if args.blank?
175: 
176:     AssetHat.html_cache       ||= {}
177:     AssetHat.html_cache[:js]  ||= {}
178:     cache_key = args.inspect
179: 
180:     if !AssetHat.cache? || AssetHat.html_cache[:js][cache_key].blank?
181:       # Generate HTML and write to cache
182: 
183:       html = []
184:       options = args.extract_options!
185: 
186:       included_vendors = (args & AssetHat::JS::VENDORS)
187:       included_vendors.each do |vendor|
188:         args.delete vendor
189:         src = AssetHat::JS::Vendors.source_for(vendor, options.slice(:version))
190:         html << include_assets(:js, src, :cache => true)
191:       end
192: 
193:       html << include_assets(:js, *(args + [options]))
194:       html = html.join("\n").strip
195:       AssetHat.html_cache[:js][cache_key] = html
196:     end
197: 
198:     html ||= AssetHat.html_cache[:js][cache_key]
199:     html
200:   end