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" ... />

Include a stylesheet with extra media types:

include_css 'mobile', :media => 'handheld,screen,projection'
=>  <link href="/stylesheets/mobile.min.css"
          media="handheld,screen,projection" ... />
[show source]
     # File app/helpers/asset_hat_helper.rb, line 129
129:   def include_css(*args)
130:     return if args.blank?
131: 
132:     AssetHat.html_cache       ||= {}
133:     AssetHat.html_cache[:css] ||= {}
134: 
135:     options = args.extract_options!
136:     options.symbolize_keys!.reverse_merge!(
137:       :media => 'screen,projection', :ssl => controller.request.ssl?)
138:     cache_key = (args + [options]).inspect
139: 
140:     if !AssetHat.cache? || AssetHat.html_cache[:css][cache_key].blank?
141:       # Generate HTML and write to cache
142:       options[:ssl] &&= AssetHat.ssl_asset_host_differs?
143:       html = AssetHat.html_cache[:css][cache_key] =
144:         include_assets(:css, *(args + [options]))
145:     end
146: 
147:     html ||= AssetHat.html_cache[:css][cache_key]
148:     html
149:   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:

# 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.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 189
189:   def include_js(*args)
190:     return if args.blank?
191: 
192:     AssetHat.html_cache       ||= {}
193:     AssetHat.html_cache[:js]  ||= {}
194: 
195:     options = args.extract_options!
196:     options.symbolize_keys!.reverse_merge!(:ssl => controller.request.ssl?)
197:     cache_key = (args + [options]).inspect
198: 
199:     if !AssetHat.cache? || AssetHat.html_cache[:js][cache_key].blank?
200:       # Generate HTML and write to cache
201: 
202:       html = []
203:       included_vendors = (args & AssetHat::JS::VENDORS)
204:       included_vendors.each do |vendor|
205:         args.delete vendor
206:         src = AssetHat::JS::Vendors.source_for(
207:                 vendor, options.slice(:ssl, :version))
208:         html << include_assets(:js, src, :cache => true)
209:       end
210: 
211:       options.except! :ssl, :version
212: 
213:       html << include_assets(:js, *(args + [options]))
214:       html = html.join("\n").strip
215:       AssetHat.html_cache[:js][cache_key] = html
216:     end
217: 
218:     html ||= AssetHat.html_cache[:js][cache_key]
219:     html
220:   end