lib/sprockets/helpers.rb in sprockets-helpers-1.1.0 vs lib/sprockets/helpers.rb in sprockets-helpers-1.2.0
- old
+ new
@@ -7,10 +7,13 @@
require 'uri'
module Sprockets
module Helpers
class << self
+ # Indicates whenever we are using Sprockets 3.x.
+ attr_accessor :are_using_sprockets_3
+
# Link to assets from a dedicated server.
attr_accessor :asset_host
# When true, the asset paths will return digest paths.
attr_accessor :digest
@@ -34,10 +37,11 @@
# The base URL the Sprocket environment is mapped to.
# This defaults to '/assets'.
def prefix
@prefix ||= '/assets'
+ @prefix.respond_to?(:first) ? "/#{@prefix.first}" : @prefix
end
attr_writer :prefix
# Customize the protocol when using asset hosts.
# If the value is :relative, A relative protocol ('//')
@@ -86,10 +90,13 @@
super(context)
end
end
+ # We are checking here to skip this at runtime
+ @are_using_sprockets_3 = Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('3.0')
+
# Returns the path to an asset either in the Sprockets environment
# or the public directory. External URIs are untouched.
#
# ==== Options
#
@@ -119,33 +126,36 @@
#
def asset_path(source, options = {})
uri = URI.parse(source)
return source if uri.absolute?
+ options[:prefix] = Sprockets::Helpers.prefix unless options[:prefix]
+
if Helpers.debug || options[:debug]
options[:manifest] = false
options[:digest] = false
options[:asset_host] = false
end
source_ext = File.extname(source)
+
if options[:ext] && source_ext != ".#{options[:ext]}"
uri.path << ".#{options[:ext]}"
end
- path = find_asset_path(uri, options)
+ path = find_asset_path(uri, source, options)
if options[:expand] && path.respond_to?(:to_a)
path.to_a
else
path.to_s
end
end
alias_method :path_to_asset, :asset_path
def asset_tag(source, options = {}, &block)
raise ::ArgumentError, 'block missing' unless block
- options = { :expand => Helpers.debug || Helpers.expand, :debug => Helpers.debug }.merge(options)
+ options = { :expand => !!Helpers.debug || !!Helpers.expand, :debug => Helpers.debug }.merge(options)
path = asset_path(source, options)
output = if options[:expand] && path.respond_to?(:map)
"\n<!-- Expanded from #{source} -->\n" + path.map(&block).join("\n")
else
@@ -326,20 +336,30 @@
# returned by #environment.
def assets_environment
Helpers.environment || environment
end
- def find_asset_path(uri, options = {})
+ def find_asset_path(uri, source, options = {})
if Helpers.manifest && options[:manifest] != false
manifest_path = Helpers.manifest.assets[uri.path]
return Helpers::ManifestPath.new(uri, manifest_path, options) if manifest_path
end
- assets_environment.resolve(uri.path) do |path|
- return Helpers::AssetPath.new(uri, assets_environment[path], options)
- end
+ if Sprockets::Helpers.are_using_sprockets_3
+ resolved = assets_environment.resolve(uri.path)
- return Helpers::FilePath.new(uri, options)
+ if resolved
+ return Helpers::AssetPath.new(uri, assets_environment[uri.path], options)
+ else
+ return Helpers::FilePath.new(uri, options)
+ end
+ else
+ assets_environment.resolve(uri.path) do |path|
+ return Helpers::AssetPath.new(uri, assets_environment[path], options)
+ end
+
+ return Helpers::FilePath.new(uri, options)
+ end
end
end
class Context
include Helpers