mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add SprocketsHelper
This commit is contained in:
parent
41cc643065
commit
9cb264555d
2 changed files with 79 additions and 0 deletions
|
@ -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
|
||||
|
|
77
actionpack/lib/action_view/helpers/sprockets_helper.rb
Normal file
77
actionpack/lib/action_view/helpers/sprockets_helper.rb
Normal 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
|
Loading…
Reference in a new issue