Module AssetHat::JS::Vendors

  1. lib/asset_hat/js/vendors.rb

For working with supported 3rd-party JavaScript plugin/framework/library vendors.

Methods

public class

  1. source_for

Constants

VENDORS_ON_GOOGLE_CDN = [ :dojo, :ext_core, :jquery, :jquery_ui, :mootools, :prototype, :scriptaculous, :swfobject, :webfont, :yui ]   A list of supported 3rd-party JavaScript plugin/vendor names. Homepages:
VENDORS_ON_CDNJS = [ :lab_js ]
VENDORS = VENDORS_ON_GOOGLE_CDN + VENDORS_ON_CDNJS

Public class methods

source_for (vendor, options={})

Accepts an item from `VENDORS`, and returns the URL at which that vendor asset can be found. The URL is either local (relative) or remote, depending on the environment configuration:

  • If `AssetHat.consider_all_requests_local?` is true:
    • The local file takes precedence.
    • If the local file is missing, the remote URL in assets.yml is used as a fallback.
    • If there is no remote URL in assets.yml, the Google CDN URL is used as a fallback. (This makes setup easier: If the app doesn’t already have a local copy of the vendor file, then it’s instead loaded remotely.)
  • If `AssetHat.consider_all_requests_local?` is false:
    • The remote URL in assets.yml takes precedence.
    • The Google CDN URL is used as a fallback, but only if a version number can be found (either in assets.yml or via the helper’s `:version` option). If no version number is found, the remote URL cannot be built, so the local file (if any) is used as a fallback.

Options:

ssl
Boolean for whether to include vendor JS via HTTPS. Defaults to false.
version
The vendor version, e.g., ‘1.6.0’ for jQuery 1.6. By default, each vendor version is taken from config/assets.yml; use this option to override the configuration.
[show source]
     # File lib/asset_hat/js/vendors.rb, line 67
 67:       def self.source_for(vendor, options={})
 68:         vendor_config =
 69:           AssetHat.config['js']['vendors'][vendor.to_s] rescue nil
 70:         use_local = AssetHat.consider_all_requests_local?
 71:         use_ssl   = !!options[:ssl]
 72:         version   = options[:version] || vendor_config['version'] rescue nil
 73: 
 74:         # Prepare local path and default remote URL
 75:         srcs = Vendors.vendor_uris(vendor,
 76:           :use_ssl => use_ssl, :version => version)
 77:         local_src, remote_src = srcs[:local], srcs[:remote]
 78: 
 79:         # Using the local URL requires that the vendor file exists locally. If
 80:         # the vendor file doesn't exist, use the remote URL as fallback.
 81:         use_local &&= AssetHat.asset_exists?(local_src, :js)
 82: 
 83:         # If no version given, can't determine the remote URL; use the local
 84:         # URL as fallback.
 85:         use_local ||= version.blank?
 86: 
 87:         if use_local
 88:           src = local_src
 89:         else
 90:           # To ease setup, if no local copy of the vendor code is found,
 91:           # use a remote URL as a fallback.
 92: 
 93:           # Give precedence to configured remote URLs
 94:           src   = vendor_config.try(:[], 'remote_ssl_url') if use_ssl
 95:           src ||= vendor_config.try(:[], 'remote_url')
 96: 
 97:           # Use default remote URL as fallback
 98:           src ||= remote_src
 99: 
100:           # Use local URL as final resort, even though the file doesn't
101:           # exist, in hopes that the app maintainer finds the 404 (or the
102:           # warning below) in the logs. This needs to be fixed in the app,
103:           # rather than relying on a CDN to dynamically provide the latest
104:           # stable vendor version.
105:           if src.blank?
106:             src = local_src
107:             Rails.logger.warn "\n\nAssetHat WARNING (#{Time.now}):\n" + %{
108:               Tried to reference the vendor JS `:#{vendor}`, but
109:               #{AssetHat.assets_dir(:js)}/#{local_src} couldn't be found, and
110:               couldn't use a remote fallback because no vendor version was
111:               given in #{AssetHat::RELATIVE_CONFIG_FILEPATH}.
112:             }.squish!
113:               # TODO: Create `AssetHat::Logger.warn`, etc. methods
114:           end
115:         end
116: 
117:         src
118:       end