1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add SprocketsHelper

This commit is contained in:
Joshua Peek 2011-03-28 15:19:34 -05:00
parent 41cc643065
commit 9cb264555d
2 changed files with 79 additions and 0 deletions

View file

@ -22,6 +22,7 @@ module ActionView #:nodoc:
autoload :RecordTagHelper autoload :RecordTagHelper
autoload :SanitizeHelper autoload :SanitizeHelper
autoload :ScriptaculousHelper autoload :ScriptaculousHelper
autoload :SprocketsHelper
autoload :TagHelper autoload :TagHelper
autoload :TextHelper autoload :TextHelper
autoload :TranslationHelper autoload :TranslationHelper
@ -52,6 +53,7 @@ module ActionView #:nodoc:
include RecordTagHelper include RecordTagHelper
include SanitizeHelper include SanitizeHelper
include ScriptaculousHelper include ScriptaculousHelper
include SprocketsHelper
include TagHelper include TagHelper
include TextHelper include TextHelper
include TranslationHelper include TranslationHelper

View file

@ -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