Module: AssetHat::JS::Vendors

Defined in:
lib/asset_hat/js/vendors.rb

Overview

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

Constant Summary collapse

VENDORS_ON_GOOGLE_CDN =

A list of supported 3rd-party JavaScript plugin/vendor names. Homepages:

[
  :dojo,
  :ext_core,
  :jquery,
  :jquery_ui,
  :mootools,
  :prototype,
  :scriptaculous,
  :swfobject,
  :webfont,
  :yui
]
VENDORS_ON_CDNJS =
[
  :lab_js
]
VENDORS =
VENDORS_ON_GOOGLE_CDN + VENDORS_ON_CDNJS

Class Method Summary collapse

Class Method Details

.source_for(vendor, options = {}) ⇒ Object

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.5.0’ for jQuery 1.5. By default, each vendor version is taken from config/assets.yml; use this option to override the configuration.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/asset_hat/js/vendors.rb', line 67

def self.source_for(vendor, options={})
  vendor_config =
    AssetHat.config['js']['vendors'][vendor.to_s] rescue nil
  use_local = AssetHat.consider_all_requests_local?
  use_ssl   = !!options[:ssl]
  version   = options[:version] || vendor_config['version'] rescue nil

  # Prepare local path and default remote URL
  srcs = Vendors.vendor_uris(vendor,
    :use_ssl => use_ssl, :version => version)
  local_src, remote_src = srcs[:local], srcs[:remote]

  # Using the local URL requires that the vendor file exists locally. If
  # the vendor file doesn't exist, use the remote URL as fallback.
  use_local &&= AssetHat.asset_exists?(local_src, :js)

  # If no version given, can't determine the remote URL; use the local
  # URL as fallback.
  use_local ||= version.blank?

  if use_local
    src = local_src
  else
    # To ease setup, if no local copy of the vendor code is found,
    # use a remote URL as a fallback.

    # Give precedence to configured remote URLs
    src   = vendor_config.try(:[], 'remote_ssl_url') if use_ssl
    src ||= vendor_config.try(:[], 'remote_url')

    # Use default remote URL as fallback
    src ||= remote_src

    # Use local URL as final resort, even though the file doesn't
    # exist, in hopes that the app maintainer finds the 404 (or the
    # warning below) in the logs. This needs to be fixed in the app,
    # rather than relying on a CDN to dynamically provide the latest
    # stable vendor version.
    if src.blank?
      src = local_src
      Rails.logger.warn "\n\nAssetHat WARNING (#{Time.now}):\n" + %{
        Tried to reference the vendor JS `:#{vendor}`, but
        #{AssetHat.assets_dir(:js)}/#{local_src} couldn't be found, and
        couldn't use a remote fallback because no vendor version was
        given in #{AssetHat::RELATIVE_CONFIG_FILEPATH}.
      }.squish!
        # TODO: Create `AssetHat::Logger.warn`, etc. methods
    end
  end

  src
end