diff options
author | Joshua Peek <josh@joshpeek.com> | 2011-03-28 15:19:34 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2011-03-28 15:19:34 -0500 |
commit | 9cb264555dbc630ff5e44d96b0d70061856f568c (patch) | |
tree | bc7e5b3e7bcd162aadf77c494cc143383f9af6b8 | |
parent | 41cc6430650d0e90e892e0bd9a29ddcefd8a9c64 (diff) | |
download | rails-9cb264555dbc630ff5e44d96b0d70061856f568c.tar.gz rails-9cb264555dbc630ff5e44d96b0d70061856f568c.tar.bz2 rails-9cb264555dbc630ff5e44d96b0d70061856f568c.zip |
Add SprocketsHelper
-rw-r--r-- | actionpack/lib/action_view/helpers.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/sprockets_helper.rb | 77 |
2 files changed, 79 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index d338ce616a..cb1ab64121 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -22,6 +22,7 @@ module ActionView #:nodoc: autoload :RecordTagHelper autoload :SanitizeHelper autoload :ScriptaculousHelper + autoload :SprocketsHelper autoload :TagHelper autoload :TextHelper autoload :TranslationHelper @@ -52,6 +53,7 @@ module ActionView #:nodoc: include RecordTagHelper include SanitizeHelper include ScriptaculousHelper + include SprocketsHelper include TagHelper include TextHelper include TranslationHelper diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb new file mode 100644 index 0000000000..4abc09e9a5 --- /dev/null +++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb @@ -0,0 +1,77 @@ +require 'uri' + +module ActionView + module Helpers + module SprocketsHelper + def sprockets_javascript_path(source) + compute_sprockets_path source, 'javascripts', 'js' + end + + def sprockets_javascript_include_tag(source, options = {}) + options = { + 'type' => "application/javascript", + 'src' => sprockets_javascript_path(source) + }.merge(options.stringify_keys) + + content_tag 'script', "", options + end + + def sprockets_stylesheet_path(source) + compute_sprockets_path source, 'stylesheets', 'css' + end + + def sprockets_stylesheet_link_tag(source, options = {}) + options = { + 'rel' => "stylesheet", + 'type' => "text/css", + 'media' => "screen", + 'href' => sprockets_stylesheet_path(source) + }.merge(options.stringify_keys) + + tag 'link', options + end + + private + def compute_sprockets_path(source, dir, default_ext) + return source if URI.parse(source).host + + # Add /javscripts to relative paths + if source[0] != ?/ + source = "/#{dir}/#{source}" + end + + # Add default extension if there isn't one + if default_ext && File.extname(source).empty? + source = "#{source}.#{default_ext}" + end + + # Fingerprint url + source = Rails.application.assets.url(source) + + host = compute_asset_host(source) + + if controller.respond_to?(:request) && host && URI.parse(host).host.nil? + host = "#{controller.request.protocol}#{host}" + end + + "#{host}#{source}" + end + + def compute_asset_host(source) + if host = config.asset_host + if host.is_a?(Proc) || host.respond_to?(:call) + case host.is_a?(Proc) ? host.arity : host.method(:call).arity + when 2 + request = controller.respond_to?(:request) && controller.request + host.call(source, request) + else + host.call(source) + end + else + (host =~ /%d/) ? host % (source.hash % 4) : host + end + end + end + end + end +end |