Module AssetHatHelper

  1. lib/asset_hat_helper.rb

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

Methods

public instance

  1. asset_path
  2. include_css
  3. include_js

Constants

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

Public instance methods

asset_path (type, source)

Returns the public URL path to the given source file.

type argument: :css or :js

[show source]
     # File lib/asset_hat_helper.rb, line 363
363:   def asset_path(type, source)
364:     case type.to_sym
365:     when :css ; stylesheet_path(source)
366:     when :js  ; javascript_path(source)
367:     else
368:       raise %{
369:         Unknown type "#{type}"; should be one of:
370:         #{AssetHat::TYPES.join(', ')}.
371:       }.squish!
372:     end
373:   end
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, minified 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'
=>  <link href="/stylesheets/reset.min.css" ... />
    <link href="/stylesheets/application.min.css" ... />

Include a stylesheet with extra media types:

include_css 'mobile', :media => 'handheld,screen,projection'
=>  <link href="/stylesheets/mobile.min.css"
          media="handheld,screen,projection" ... />

Get the URL for a single, minified stylesheet:

include_css 'diagnostics', :only_url => true
=> '/stylesheets/diagnostics.min.css'

Get the URL for a single, unminified stylesheet:

include_css 'diagnostics.css', :only_url => true
=> '/stylesheets/diagnostics.css'

Get the URL for a bundle of stylesheets when environment enables caching (e.g., staging, production):

include_css :bundle => 'application', :only_url => true
=> '/stylesheets/bundles/application.min.css'

Get URLs for a bundle of stylesheets when environment disables caching (e.g., development, test):

include_css :bundle => 'application', :only_url => true
=> ['/stylesheets/reset.css', '/stylesheets/common.css', ...]

Get URLs for multiple stylesheets manually:

include_css 'reset', 'application', :only_url => true
=> ['/stylesheets/reset.css', '/stylesheets/application.css']
[show source]
     # File lib/asset_hat_helper.rb, line 159
159:   def include_css(*args)
160:     return if args.blank?
161: 
162:     initialize_html_cache :css
163: 
164:     options = setup_options(args,
165:       :media => 'screen,projection',
166:       :ssl => controller.request.ssl?
167:     )
168:     cache_key = setup_cache_key(args, options)
169: 
170:     if !asset_cached?(:css, cache_key)
171:       # Generate HTML and write to cache
172:       options[:ssl] &&= AssetHat.ssl_asset_host_differs?
173:       html = AssetHat.html_cache[:css][cache_key] =
174:         include_assets(:css, *(args + [options]))
175:     end
176: 
177:     html ||= AssetHat.html_cache[:css][cache_key]
178:     make_html_safe html
179:   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, minified JS file:

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

Include a single, unminified JS file:

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

Include jQuery:

# Development/test environment:
include_js :jquery
=>  <script src="/javascripts/jquery-VERSION.min.js" ...></script>

# Staging/production environment:
include_js :jquery
=>  <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.6.1'`.

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>

Get the URL for a single, minified JS file:

include_js 'application', :only_url => true
=>  '/javascripts/application.min.js'

Get the URL for a single, unminified JS file:

include_js 'application.js', :only_url => true
=>  '/javascripts/application.js', :only_url => true

Get the URL for jQuery:

# Development/test environment:
include_js :jquery, :only_url => true
=>  '/javascripts/jquery-VERSION.min.js'

# Staging/production environment:
include_js :jquery, :only_url => true
=>  'http://ajax.googleapis.com/.../jquery.min.js'

Get the URL for a bundle of JS files when environment enables caching (e.g., staging, production):

include_js :bundle => 'application', :only_url => true
=> '/javascripts/bundles/application.min.js'

Get URLs for a bundle of JS files when environment disables caching (e.g., development, test):

include_js :bundle => 'application', :only_url => true
=> ['/javascripts/jquery.plugin-foo.js',
    '/javascripts/jquery.plugin-bar.min.js',
    '/javascripts/json2.js',
    ...]

Get URLs for multiple JS files manually:

include_js 'json2', 'application', :only_url => true
=> ['/javascripts/json2.js', '/javascripts/application.js']

Load JS files with LABjs (hosted either from cdnjs or your own web server, if found in public/javascripts/):

# config/assets.yml:
js:
  vendors:
    lab_js:
      version: 1.x.x

# Usage:
include_js :jquery, :bundle => 'application', :loader => :lab_js
=>  <script src="http://ajax.cdnjs.com/.../1.x.x/LAB.min.js" ...></script>
    <script type="text/javascript">
    window.$LABinst=$LAB.
      script('http://ajax.googleapis.com/.../jquery.min.js').wait().
      script('/javascripts/bundles/application.min.js').wait();
    </script>

# For advanced fine-tuning, build the LABjs calls manually (based on
# example from http://labjs.com/documentation.php ):
<script>
   window.$LABinst = $LAB.
     script('<%= include_js 'framework', :only_url => true %>').wait().
     script('<%= include_js 'plugin.framework.js',
                            :only_url => true %>').
     script('<%= include_js 'myplugin.framework.js',
                            :only_url => true %>').wait().
     script('<%= include_js 'init.js', :only_url => true %>').wait();
</script>

# If you want to execute an inline <script> block that relies on any
# of these dependencies, use the JS variable `window.$LABinst`.
# Example (using jQuery to handle when DOM is ready):
<script>
window.$LABinst(function(){
  console.log('JS dependencies are ready');
  $(function(){
    console.log('DOM is ready');
  });
});
</script>
[show source]
     # File lib/asset_hat_helper.rb, line 295
295:   def include_js(*args)
296:     return if args.blank?
297: 
298:     initialize_html_cache :js
299: 
300:     options = setup_options(args, :ssl => controller.request.ssl?)
301:     cache_key = setup_cache_key(args, options)
302: 
303:     if !asset_cached?(:js, cache_key)
304:       # Generate HTML and write to cache
305: 
306:       htmls = []
307:       include_assets_options = options.except(:ssl, :version)
308: 
309:       loader = options.delete(:loader)
310:       include_assets_options.merge!(:only_url => true) if loader
311: 
312:       # Get vendor HTML/URLs
313:       included_vendors = (args & AssetHat::JS::VENDORS)
314: 
315:       # Add HTML inclusions for vendors
316:       included_vendors.each do |vendor|
317:         args.delete vendor
318:         src = AssetHat::JS::Vendors.source_for(
319:                 vendor, options.slice(:ssl, :version))
320:         htmls << include_assets(:js, src,
321:                   include_assets_options.merge(:cache => true).
322:                                          except(:bundle, :bundles))
323:       end
324: 
325:       # Get non-vendor HTML/URLs
326:       htmls << include_assets(:js, *(args + [include_assets_options]))
327:       htmls.reject!(&:blank?)
328: 
329:       if loader
330:         # `htmls` actually contains URLs; convert to an HTML/JS block
331:         urls  = htmls.dup.flatten
332:         htmls = []
333: 
334:         case loader
335:         when :lab_js
336:           htmls << include_js(:lab_js)
337:           htmls << '<script type="text/javascript">'
338:           htmls << AssetHat::JS::Vendors.loader_js(:lab_js, :urls => urls)
339:           htmls << '</script>'
340:         end
341:       end
342: 
343:       # Convert to a URL (string), array of URLs, or one long HTML string
344:       html =  if options[:only_url]
345:                 # Return one URL (string) or multiple (array of strings).
346:                 # Not actually HTML.
347:                 htmls.flatten!
348:                 htmls.size == 1 ? htmls.first : htmls
349:               else
350:                 # Return one long string of HTML
351:                 htmls.join("\n").strip
352:               end
353:       AssetHat.html_cache[:js][cache_key] = html
354:     end
355: 
356:     html ||= AssetHat.html_cache[:js][cache_key]
357:     make_html_safe html
358:   end