doc/classes/AssetHatHelper.html in asset_hat-0.3.1 vs doc/classes/AssetHatHelper.html in asset_hat-0.4.0
- old
+ new
@@ -29,11 +29,11 @@
<span class='type'>Module</span>
AssetHatHelper
</h1>
<ol class='paths'>
<li>
- <a href="../files/app/helpers/asset_hat_helper_rb.html">app/helpers/asset_hat_helper.rb</a>
+ <a href="../files/lib/asset_hat_helper_rb.html">lib/asset_hat_helper.rb</a>
</li>
</ol>
</div>
<div id='content'>
<div id='text'>
@@ -45,12 +45,13 @@
</div>
<div id='method-list'>
<h2>Methods</h2>
<h3>public instance</h3>
<ol>
- <li><a href="#M000027">include_css</a></li>
- <li><a href="#M000028">include_js</a></li>
+ <li><a href="#M000032">asset_path</a></li>
+ <li><a href="#M000030">include_css</a></li>
+ <li><a href="#M000031">include_js</a></li>
</ol>
</div>
<div id='section'>
<div id='constants-list'>
<h2>Constants</h2>
@@ -64,75 +65,118 @@
</table>
</div>
</div>
<div id='methods'>
<h2>Public instance methods</h2>
- <div class='method public-instance' id='method-M000027'>
- <a name='M000027'></a>
+ <div class='method public-instance' id='method-M000032'>
+ <a name='M000032'></a>
<div class='synopsis'>
+ <span class='name'>asset_path</span>
+ <span class='arguments'>(type, source)</span>
+ </div>
+ <div class='description'>
+ <p>
+ Returns the public URL path to the given source file.
+ </p>
+ <p>
+ <tt>type</tt> argument: <tt>:css</tt> or <tt>:js</tt>
+ </p>
+ </div>
+ <div class='source'>
+ <a class='source-toggle' href='#' onclick="toggleCode('M000032-source'); return false">
+ [show source]
+ </a>
+ <pre id='M000032-source'> <span class="ruby-comment cmt"># File lib/asset_hat_helper.rb, line 368</span>
368: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">asset_path</span>(<span class="ruby-identifier">type</span>, <span class="ruby-identifier">source</span>)
369: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">to_sym</span>
370: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:css</span> ; <span class="ruby-identifier">stylesheet_path</span>(<span class="ruby-identifier">source</span>)
371: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:js</span> ; <span class="ruby-identifier">javascript_path</span>(<span class="ruby-identifier">source</span>)
372: <span class="ruby-keyword kw">else</span>
373: <span class="ruby-identifier">raise</span> <span class="ruby-node">%{Unknown type "#{type}"; should be one of: #{TYPES.join(', ')}.}</span>
374: <span class="ruby-keyword kw">end</span>
375: <span class="ruby-keyword kw">end</span></pre>
+ </div>
+ </div>
+ <div class='method public-instance' id='method-M000030'>
+ <a name='M000030'></a>
+ <div class='synopsis'>
<span class='name'>include_css</span>
<span class='arguments'>(*args)</span>
</div>
<div class='description'>
<p>
- <tt><a href="AssetHatHelper.html#M000027">include_css</a></tt> is a smart
+ <tt><a href="AssetHatHelper.html#M000030">include_css</a></tt> is a smart
wrapper for Rails’ <tt>stylesheet_link_tag</tt>. The two can be used
together while migrating to <a href="AssetHat.html">AssetHat</a>.
</p>
<p>
- Include a single stylesheet:
+ Include a single, minified stylesheet:
</p>
<pre>include_css 'diagnostics'
=> <link href="/stylesheets/diagnostics.min.css" media="screen,projection" rel="stylesheet" type="text/css" /></pre>
<p>
- Include a single unminified stylesheet:
+ Include a single, unminified stylesheet:
</p>
<pre>include_css 'diagnostics.css'
=> <link href="/stylesheets/diagnostics.css" media="screen,projection" rel="stylesheet" type="text/css" /></pre>
<p>
Include a bundle of stylesheets (i.e., a concatenated set of stylesheets;
configure in config/assets.yml):
</p>
<pre>include_css :bundle => 'application'
=> <link href="/stylesheets/bundles/application.min.css" ... /></pre>
<p>
Include multiple stylesheets separately (not as cool):
</p>
- <pre>include_css 'reset', 'application', 'clearfix'
=> <link href="/stylesheets/reset.min.css" ... />
 <link href="/stylesheets/application.min.css" ... />
 <link href="/stylesheets/clearfix.min.css" ... /></pre>
+ <pre>include_css 'reset', 'application'
=> <link href="/stylesheets/reset.min.css" ... />
 <link href="/stylesheets/application.min.css" ... /></pre>
<p>
Include a stylesheet with extra media types:
</p>
<pre>include_css 'mobile', :media => 'handheld,screen,projection'
=> <link href="/stylesheets/mobile.min.css"
 media="handheld,screen,projection" ... /></pre>
+ <p>
+ Get the URL for a single, minified stylesheet:
+ </p>
+ <pre>include_css 'diagnostics', :only_url => true
=> '/stylesheets/diagnostics.min.css'</pre>
+ <p>
+ Get the URL for a single, unminified stylesheet:
+ </p>
+ <pre>include_css 'diagnostics.css', :only_url => true
=> '/stylesheets/diagnostics.css'</pre>
+ <p>
+ Get the URL for a bundle of stylesheets when environment <b>enables</b>
+ caching (e.g., staging, production):
+ </p>
+ <pre>include_css :bundle => 'application', :only_url => true
=> '/stylesheets/bundles/application.min.css'</pre>
+ <p>
+ Get URLs for a bundle of stylesheets when environment <b>disables</b>
+ caching (e.g., development, test):
+ </p>
+ <pre>include_css :bundle => 'application', :only_url => true
=> ['/stylesheets/reset.css', '/stylesheets/common.css', ...]</pre>
+ <p>
+ Get URLs for multiple stylesheets manually:
+ </p>
+ <pre>include_css 'reset', 'application', :only_url => true
=> ['/stylesheets/reset.css', '/stylesheets/application.css']</pre>
</div>
<div class='source'>
- <a class='source-toggle' href='#' onclick="toggleCode('M000027-source'); return false">
+ <a class='source-toggle' href='#' onclick="toggleCode('M000030-source'); return false">
[show source]
</a>
- <pre id='M000027-source'> <span class="ruby-comment cmt"># File app/helpers/asset_hat_helper.rb, line 129</span>
129: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">include_css</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
130: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">blank?</span>
131: 
132: <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span> <span class="ruby-operator">||=</span> {}
133: <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:css</span>] <span class="ruby-operator">||=</span> {}
134: 
135: <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">extract_options!</span>
136: <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys!</span>.<span class="ruby-identifier">reverse_merge!</span>(
137: <span class="ruby-identifier">:media</span> =<span class="ruby-operator">></span> <span class="ruby-value str">'screen,projection'</span>, <span class="ruby-identifier">:ssl</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">controller</span>.<span class="ruby-identifier">request</span>.<span class="ruby-identifier">ssl?</span>)
138: <span class="ruby-identifier">cache_key</span> = (<span class="ruby-identifier">args</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">options</span>]).<span class="ruby-identifier">inspect</span>
139: 
140: <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">cache?</span> <span class="ruby-operator">||</span> <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:css</span>][<span class="ruby-identifier">cache_key</span>].<span class="ruby-identifier">blank?</span>
141: <span class="ruby-comment cmt"># Generate HTML and write to cache</span>
142: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:ssl</span>] <span class="ruby-operator">&&=</span> <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">ssl_asset_host_differs?</span>
143: <span class="ruby-identifier">html</span> = <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:css</span>][<span class="ruby-identifier">cache_key</span>] =
144: <span class="ruby-identifier">include_assets</span>(<span class="ruby-identifier">:css</span>, <span class="ruby-operator">*</span>(<span class="ruby-identifier">args</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">options</span>]))
145: <span class="ruby-keyword kw">end</span>
146: 
147: <span class="ruby-identifier">html</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:css</span>][<span class="ruby-identifier">cache_key</span>]
148: <span class="ruby-identifier">html</span>
149: <span class="ruby-keyword kw">end</span></pre>
+ <pre id='M000030-source'> <span class="ruby-comment cmt"># File lib/asset_hat_helper.rb, line 159</span>
159: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">include_css</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
160: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">blank?</span>
161: 
162: <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span> <span class="ruby-operator">||=</span> {}
163: <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:css</span>] <span class="ruby-operator">||=</span> {}
164: 
165: <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">extract_options!</span>
166: <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys!</span>.<span class="ruby-identifier">reverse_merge!</span>(
167: <span class="ruby-identifier">:media</span> =<span class="ruby-operator">></span> <span class="ruby-value str">'screen,projection'</span>, <span class="ruby-identifier">:ssl</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">controller</span>.<span class="ruby-identifier">request</span>.<span class="ruby-identifier">ssl?</span>)
168: <span class="ruby-identifier">cache_key</span> = (<span class="ruby-identifier">args</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">options</span>]).<span class="ruby-identifier">inspect</span>
169: 
170: <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">cache?</span> <span class="ruby-operator">||</span> <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:css</span>][<span class="ruby-identifier">cache_key</span>].<span class="ruby-identifier">blank?</span>
171: <span class="ruby-comment cmt"># Generate HTML and write to cache</span>
172: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:ssl</span>] <span class="ruby-operator">&&=</span> <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">ssl_asset_host_differs?</span>
173: <span class="ruby-identifier">html</span> = <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:css</span>][<span class="ruby-identifier">cache_key</span>] =
174: <span class="ruby-identifier">include_assets</span>(<span class="ruby-identifier">:css</span>, <span class="ruby-operator">*</span>(<span class="ruby-identifier">args</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">options</span>]))
175: <span class="ruby-keyword kw">end</span>
176: 
177: <span class="ruby-identifier">html</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:css</span>][<span class="ruby-identifier">cache_key</span>]
178: <span class="ruby-identifier">html</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:html_safe</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">html</span>.<span class="ruby-identifier">html_safe</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">html</span>
179: <span class="ruby-keyword kw">end</span></pre>
</div>
</div>
- <div class='method public-instance' id='method-M000028'>
- <a name='M000028'></a>
+ <div class='method public-instance' id='method-M000031'>
+ <a name='M000031'></a>
<div class='synopsis'>
<span class='name'>include_js</span>
<span class='arguments'>(*args)</span>
</div>
<div class='description'>
<p>
- <tt><a href="AssetHatHelper.html#M000028">include_js</a></tt> is a smart
+ <tt><a href="AssetHatHelper.html#M000031">include_js</a></tt> is a smart
wrapper for Rails’ <tt>javascript_include_tag</tt>. The two can be
used together while migrating to <a href="AssetHat.html">AssetHat</a>.
</p>
<p>
- Include a single JS file:
+ Include a single, minified JS file:
</p>
<pre>include_js 'application'
=> <script src="/javascripts/application.min.js" type="text/javascript"></script></pre>
<p>
- Include a single JS unminified file:
+ Include a single, unminified JS file:
</p>
<pre>include_js 'application.js'
=> <script src="/javascripts/application.js" type="text/javascript"></script></pre>
<p>
Include jQuery:
</p>
- <pre># 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'`.</pre>
+ <pre># 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.0'`.</pre>
<p>
Include a bundle of JS files (i.e., a concatenated set of files; configure
in <tt>config/assets.yml</tt>):
</p>
<pre>include_js :bundle => 'application'
=> <script src="/javascripts/bundles/application.min.js" ...></script></pre>
@@ -142,15 +186,46 @@
<pre>include_js :bundles => %w[plugins common]
=> <script src="/javascripts/bundles/plugins.min.js" ...></script>
 <script src="/javascripts/bundles/common.min.js" ...></script></pre>
<p>
Include multiple JS files separately (not as cool):
</p>
<pre>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></pre>
+ <p>
+ Get the URL for a single, minified JS file:
+ </p>
+ <pre>include_js 'application', :only_url => true
=> '/javascripts/application.min.js'</pre>
+ <p>
+ Get the URL for a single, unminified JS file:
+ </p>
+ <pre>include_js 'application.js', :only_url => true
=> '/javascripts/application.js', :only_url => true</pre>
+ <p>
+ Get the URL for jQuery:
+ </p>
+ <pre># 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'</pre>
+ <p>
+ Get the URL for a bundle of JS files when environment <b>enables</b>
+ caching (e.g., staging, production):
+ </p>
+ <pre>include_js :bundle => 'application', :only_url => true
=> '/javascripts/bundles/application.min.js'</pre>
+ <p>
+ Get URLs for a bundle of JS files when environment <b>disables</b> caching
+ (e.g., development, test):
+ </p>
+ <pre>include_js :bundle => 'application', :only_url => true
=> ['/javascripts/jquery.plugin-foo.js',
 '/javascripts/jquery.plugin-bar.min.js',
 '/javascripts/json2.js',
 ...]</pre>
+ <p>
+ Get URLs for multiple JS files manually:
+ </p>
+ <pre>include_js 'json2', 'application', :only_url => true
=> ['/javascripts/json2.js', '/javascripts/application.js']</pre>
+ <p>
+ Load JS files with <a href="http://labjs.com">LABjs</a> (hosted either from
+ cdnjs or your own web server, if found in <tt>public/javascripts/</tt>):
+ </p>
+ <pre># 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></pre>
</div>
<div class='source'>
- <a class='source-toggle' href='#' onclick="toggleCode('M000028-source'); return false">
+ <a class='source-toggle' href='#' onclick="toggleCode('M000031-source'); return false">
[show source]
</a>
- <pre id='M000028-source'> <span class="ruby-comment cmt"># File app/helpers/asset_hat_helper.rb, line 189</span>
189: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">include_js</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
190: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">blank?</span>
191: 
192: <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span> <span class="ruby-operator">||=</span> {}
193: <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:js</span>] <span class="ruby-operator">||=</span> {}
194: 
195: <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">extract_options!</span>
196: <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys!</span>.<span class="ruby-identifier">reverse_merge!</span>(<span class="ruby-identifier">:ssl</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">controller</span>.<span class="ruby-identifier">request</span>.<span class="ruby-identifier">ssl?</span>)
197: <span class="ruby-identifier">cache_key</span> = (<span class="ruby-identifier">args</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">options</span>]).<span class="ruby-identifier">inspect</span>
198: 
199: <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">cache?</span> <span class="ruby-operator">||</span> <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:js</span>][<span class="ruby-identifier">cache_key</span>].<span class="ruby-identifier">blank?</span>
200: <span class="ruby-comment cmt"># Generate HTML and write to cache</span>
201: 
202: <span class="ruby-identifier">html</span> = []
203: <span class="ruby-identifier">included_vendors</span> = (<span class="ruby-identifier">args</span> <span class="ruby-operator">&</span> <span class="ruby-constant">AssetHat</span><span class="ruby-operator">::</span><span class="ruby-constant">JS</span><span class="ruby-operator">::</span><span class="ruby-constant">VENDORS</span>)
204: <span class="ruby-identifier">included_vendors</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">vendor</span><span class="ruby-operator">|</span>
205: <span class="ruby-identifier">args</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">vendor</span>
206: <span class="ruby-identifier">src</span> = <span class="ruby-constant">AssetHat</span><span class="ruby-operator">::</span><span class="ruby-constant">JS</span><span class="ruby-operator">::</span><span class="ruby-constant">Vendors</span>.<span class="ruby-identifier">source_for</span>(
207: <span class="ruby-identifier">vendor</span>, <span class="ruby-identifier">options</span>.<span class="ruby-identifier">slice</span>(<span class="ruby-identifier">:ssl</span>, <span class="ruby-identifier">:version</span>))
208: <span class="ruby-identifier">html</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">include_assets</span>(<span class="ruby-identifier">:js</span>, <span class="ruby-identifier">src</span>, <span class="ruby-identifier">:cache</span> =<span class="ruby-operator">></span> <span class="ruby-keyword kw">true</span>)
209: <span class="ruby-keyword kw">end</span>
210: 
211: <span class="ruby-identifier">options</span>.<span class="ruby-identifier">except!</span> <span class="ruby-identifier">:ssl</span>, <span class="ruby-identifier">:version</span>
212: 
213: <span class="ruby-identifier">html</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">include_assets</span>(<span class="ruby-identifier">:js</span>, <span class="ruby-operator">*</span>(<span class="ruby-identifier">args</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">options</span>]))
214: <span class="ruby-identifier">html</span> = <span class="ruby-identifier">html</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">"\n"</span>).<span class="ruby-identifier">strip</span>
215: <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:js</span>][<span class="ruby-identifier">cache_key</span>] = <span class="ruby-identifier">html</span>
216: <span class="ruby-keyword kw">end</span>
217: 
218: <span class="ruby-identifier">html</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:js</span>][<span class="ruby-identifier">cache_key</span>]
219: <span class="ruby-identifier">html</span>
220: <span class="ruby-keyword kw">end</span></pre>
+ <pre id='M000031-source'> <span class="ruby-comment cmt"># File lib/asset_hat_helper.rb, line 295</span>
295: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">include_js</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
296: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">blank?</span>
297: 
298: <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span> <span class="ruby-operator">||=</span> {}
299: <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:js</span>] <span class="ruby-operator">||=</span> {}
300: 
301: <span class="ruby-identifier">options</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">extract_options!</span>
302: <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys!</span>.<span class="ruby-identifier">reverse_merge!</span>(<span class="ruby-identifier">:ssl</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">controller</span>.<span class="ruby-identifier">request</span>.<span class="ruby-identifier">ssl?</span>)
303: <span class="ruby-identifier">cache_key</span> = (<span class="ruby-identifier">args</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">options</span>]).<span class="ruby-identifier">inspect</span>
304: 
305: <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">cache?</span> <span class="ruby-operator">||</span> <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:js</span>][<span class="ruby-identifier">cache_key</span>].<span class="ruby-identifier">blank?</span>
306: <span class="ruby-comment cmt"># Generate HTML and write to cache</span>
307: 
308: <span class="ruby-identifier">htmls</span> = []
309: <span class="ruby-identifier">include_assets_options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">except</span>(<span class="ruby-identifier">:ssl</span>, <span class="ruby-identifier">:version</span>)
310: <span class="ruby-identifier">loader</span> = <span class="ruby-keyword kw">nil</span>
311: 
312: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:loader</span>].<span class="ruby-identifier">present?</span>
313: <span class="ruby-identifier">loader</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:loader</span>)
314: <span class="ruby-identifier">include_assets_options</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">:only_url</span> =<span class="ruby-operator">></span> <span class="ruby-keyword kw">true</span>)
315: <span class="ruby-keyword kw">end</span>
316: 
317: <span class="ruby-comment cmt"># Get vendor HTML/URLs</span>
318: <span class="ruby-identifier">included_vendors</span> = (<span class="ruby-identifier">args</span> <span class="ruby-operator">&</span> <span class="ruby-constant">AssetHat</span><span class="ruby-operator">::</span><span class="ruby-constant">JS</span><span class="ruby-operator">::</span><span class="ruby-constant">VENDORS</span>)
319: 
320: <span class="ruby-comment cmt"># Add HTML inclusions for vendors</span>
321: <span class="ruby-identifier">included_vendors</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">vendor</span><span class="ruby-operator">|</span>
322: <span class="ruby-identifier">args</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">vendor</span>
323: <span class="ruby-identifier">src</span> = <span class="ruby-constant">AssetHat</span><span class="ruby-operator">::</span><span class="ruby-constant">JS</span><span class="ruby-operator">::</span><span class="ruby-constant">Vendors</span>.<span class="ruby-identifier">source_for</span>(
324: <span class="ruby-identifier">vendor</span>, <span class="ruby-identifier">options</span>.<span class="ruby-identifier">slice</span>(<span class="ruby-identifier">:ssl</span>, <span class="ruby-identifier">:version</span>))
325: <span class="ruby-identifier">htmls</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">include_assets</span>(<span class="ruby-identifier">:js</span>, <span class="ruby-identifier">src</span>,
326: <span class="ruby-identifier">include_assets_options</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">:cache</span> =<span class="ruby-operator">></span> <span class="ruby-keyword kw">true</span>).
327: <span class="ruby-identifier">except</span>(<span class="ruby-identifier">:bundle</span>, <span class="ruby-identifier">:bundles</span>))
328: <span class="ruby-keyword kw">end</span>
329: 
330: <span class="ruby-comment cmt"># Get non-vendor HTML/URLs</span>
331: <span class="ruby-identifier">htmls</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">include_assets</span>(<span class="ruby-identifier">:js</span>, <span class="ruby-operator">*</span>(<span class="ruby-identifier">args</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">include_assets_options</span>]))
332: <span class="ruby-identifier">htmls</span>.<span class="ruby-identifier">reject!</span>(<span class="ruby-operator">&</span><span class="ruby-identifier">:blank?</span>)
333: 
334: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">loader</span>
335: <span class="ruby-comment cmt"># `htmls` actually contains URLs; convert to an HTML/JS block</span>
336: <span class="ruby-identifier">urls</span> = <span class="ruby-identifier">htmls</span>.<span class="ruby-identifier">dup</span>.<span class="ruby-identifier">flatten</span>
337: <span class="ruby-identifier">htmls</span> = []
338: 
339: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">loader</span>
340: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:lab_js</span>
341: <span class="ruby-identifier">htmls</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">include_js</span>(<span class="ruby-identifier">:lab_js</span>)
342: <span class="ruby-identifier">htmls</span> <span class="ruby-operator"><<</span> <span class="ruby-value str">'<script type="text/javascript">'</span>
343: <span class="ruby-identifier">htmls</span> <span class="ruby-operator"><<</span> <span class="ruby-constant">AssetHat</span><span class="ruby-operator">::</span><span class="ruby-constant">JS</span><span class="ruby-operator">::</span><span class="ruby-constant">Vendors</span>.<span class="ruby-identifier">loader_js</span>(<span class="ruby-identifier">:lab_js</span>, <span class="ruby-identifier">:urls</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">urls</span>)
344: <span class="ruby-identifier">htmls</span> <span class="ruby-operator"><<</span> <span class="ruby-value str">'</script>'</span>
345: <span class="ruby-keyword kw">end</span>
346: <span class="ruby-keyword kw">end</span>
347: 
348: <span class="ruby-comment cmt"># Convert to a URL (string), array of URLs, or one long HTML string</span>
349: <span class="ruby-identifier">html</span> = <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:only_url</span>]
350: <span class="ruby-comment cmt"># Return one URL (string) or multiple (array of strings).</span>
351: <span class="ruby-comment cmt"># Not actually HTML.</span>
352: <span class="ruby-identifier">htmls</span>.<span class="ruby-identifier">flatten!</span>
353: <span class="ruby-identifier">htmls</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">htmls</span>.<span class="ruby-identifier">first</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">htmls</span>
354: <span class="ruby-keyword kw">else</span>
355: <span class="ruby-comment cmt"># Return one long string of HTML</span>
356: <span class="ruby-identifier">htmls</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">"\n"</span>).<span class="ruby-identifier">strip</span>
357: <span class="ruby-keyword kw">end</span>
358: <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:js</span>][<span class="ruby-identifier">cache_key</span>] = <span class="ruby-identifier">html</span>
359: <span class="ruby-keyword kw">end</span>
360: 
361: <span class="ruby-identifier">html</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">AssetHat</span>.<span class="ruby-identifier">html_cache</span>[<span class="ruby-identifier">:js</span>][<span class="ruby-identifier">cache_key</span>]
362: <span class="ruby-identifier">html</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:html_safe</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">html</span>.<span class="ruby-identifier">html_safe</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">html</span>
363: <span class="ruby-keyword kw">end</span></pre>
</div>
</div>
</div>
</div>
</div>