Module: Lash::BundleHelper

Defined in:
lib/lash/bundle_helper.rb

Instance Method Summary collapse

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.

Returns:

  • (Boolean)

    true if bundled assets should be used.



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? || cookies[:bundle] == "yes"
end

#javascript_bundle(*sources) ⇒ String

Generates a javascript include tag for each of the listed bundles.

Examples:

bundle_files?                             # => true
<%= javascript_bundle 'common' %>         # => <script src="/javascripts/bundle_common.min.js" type="text/javascript"></script>
bundle_files?                             # => false
<%= javascript_bundle 'common' %>         # => <script src="/javascripts/cmmmon/application.js" type="text/javascript"></script>
                                          # => <script src="/javascripts/common/superfish.js" type="text/javascript"></script>

Returns:

  • (String)


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.

Examples:

bundle_files?         # => true

<%= javascript_cdn 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', 'jQuery' -%>

# => <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
# => <script type="text/javascript">
# =>  if(typeof jQuery === "undefined" ) 
# =>    document.write( unescape("<script src=\"/javascripts/cdn/jquery.min.js?1297459967\" type=\"text/javascript\"><\/script>") );
# => </script> 

bundle_files?         # => false

<%= javascript_cdn 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', 'jQuery' -%>

# => <script src="/javascripts/cdn/jquery.js?1299809021" type="text/javascript"></script> 

Returns:

  • (String)


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_rootObject

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