Module: Lash::BundleHelper
- Defined in:
- lib/lash/bundle_helper.rb
Instance Method Summary collapse
-
#bundle_files? ⇒ Boolean
Determines if the processed bundle files should be used, or the loose files used to build those bundles.
-
#javascript_bundle(*sources) ⇒ String
Generates a javascript include tag for each of the listed bundles.
-
#javascript_cdn(lib, cdn, test = nil) ⇒ String
Generates a javascript include tag that tries to first load the javascript from a CDN source and if not availble, falls back to a local version of the script.
-
#javascript_root ⇒ Object
Root folder where application javascript files can be found.
Instance Method Details
#bundle_files? ⇒ Boolean
Determines if the processed bundle files should be used, or the loose files used to build those bundles.
By default bundled files are used in production or if the request includes a :bundle
param or cookie.
14 15 16 17 18 19 |
# File 'lib/lash/bundle_helper.rb', line 14 def bundle_files? if params.has_key? :bundle return /^t(rue)?|y(es)?|1$/i.match( params[:bundle] ) != nil end Rails.env.production? || [:bundle] == "yes" end |
#javascript_bundle(*sources) ⇒ String
Generates a javascript include tag for each of the listed bundles.
30 31 32 33 |
# File 'lib/lash/bundle_helper.rb', line 30 def javascript_bundle( *sources ) sources = sources.to_a bundle_files? ? javascript_include_bundles( sources ) : javascript_include_files( sources ) end |
#javascript_cdn(lib, cdn, test = nil) ⇒ String
Generates a javascript include tag that tries to first load the javascript from a CDN source and if not availble, falls back to a local version of the script. Also uses local versions of the scripts in development to make it easier to work offline.
Based on Stack Overflow question.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/lash/bundle_helper.rb', line 59 def javascript_cdn( lib, cdn, test = nil ) # http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-goo output = "" unless bundle_files? output << javascript_src_tag( "cdn/#{lib}", {} ) + "\n" else output << %{<script src="#{cdn}" type="text/javascript"></script>\n} output << %{<script type="text/javascript">if(typeof #{test} === "undefined" ) document.write( unescape("#{ escape_javascript javascript_src_tag( 'cdn/' + lib + '.min', {} )}") );</script>\n}.html_safe unless test.nil? end output.html_safe end |
#javascript_root ⇒ Object
Root folder where application javascript files can be found
7 8 9 |
# File 'lib/lash/bundle_helper.rb', line 7 def javascript_root File.join( ::Rails.root, 'public', 'javascripts', '' ) end |