From 9cb264555dbc630ff5e44d96b0d70061856f568c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 28 Mar 2011 15:19:34 -0500 Subject: Add SprocketsHelper --- actionpack/lib/action_view/helpers.rb | 2 + .../lib/action_view/helpers/sprockets_helper.rb | 77 ++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/sprockets_helper.rb (limited to 'actionpack') 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 -- cgit v1.2.3