Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add SprocketsHelper

  • Loading branch information...
commit 9cb264555dbc630ff5e44d96b0d70061856f568c 1 parent 41cc643
@josh josh authored
View
2  actionpack/lib/action_view/helpers.rb
@@ -22,6 +22,7 @@ module Helpers #:nodoc:
autoload :RecordTagHelper
autoload :SanitizeHelper
autoload :ScriptaculousHelper
+ autoload :SprocketsHelper
autoload :TagHelper
autoload :TextHelper
autoload :TranslationHelper
@@ -52,6 +53,7 @@ module Helpers #:nodoc:
include RecordTagHelper
include SanitizeHelper
include ScriptaculousHelper
+ include SprocketsHelper
include TagHelper
include TextHelper
include TranslationHelper
View
77 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
Please sign in to comment.
Something went wrong with that request. Please try again.