lib/sprockets/helpers.rb in sprockets-helpers-0.6.1 vs lib/sprockets/helpers.rb in sprockets-helpers-0.7.0
- old
+ new
@@ -9,50 +9,52 @@
module Sprockets
module Helpers
class << self
# Link to assets from a dedicated server.
attr_accessor :asset_host
-
+
# When true, the asset paths will return digest paths.
attr_accessor :digest
-
+
# Set the Sprockets environment to search for assets.
# This defaults to the context's #environment method.
attr_accessor :environment
-
+
# The manifest file used for lookup
attr_accessor :manifest
-
+
# The base URL the Sprocket environment is mapped to.
# This defaults to '/assets'.
def prefix
@prefix ||= '/assets'
end
attr_writer :prefix
-
+
# Customize the protocol when using asset hosts.
# If the value is :relative, A relative protocol ('//')
# will be used.
def protocol
@protocol ||= 'http://'
end
attr_writer :protocol
-
+
# The path to the public directory, where the assets
# not managed by Sprockets will be located.
# Defaults to './public'
def public_path
@public_path ||= './public'
end
attr_writer :public_path
-
+
# Convience method for configuring Sprockets::Helpers.
def configure
yield self
end
end
-
+ end
+
+ class Context
# Returns the path to an asset either in the Sprockets environment
# or the public directory. External URIs are untouched.
#
# ==== Options
#
@@ -77,39 +79,47 @@
# asset_path 'xmlhr', :ext => 'js' # => '/xmlhr.js'
# asset_path 'dir/xmlhr.js', :dir => 'javascripts' # => '/javascripts/dir/xmlhr.js'
# asset_path '/dir/xmlhr.js', :dir => 'javascripts' # => '/dir/xmlhr.js'
# asset_path 'http://www.example.com/js/xmlhr' # => 'http://www.example.com/js/xmlhr'
# asset_path 'http://www.example.com/js/xmlhr.js' # => 'http://www.example.com/js/xmlhr.js'
- #
+ #
def asset_path(source, options = {})
uri = URI.parse(source)
-
+
# Return fast if the URI is absolute
return source if uri.absolute?
-
+
+ # When debugging return paths without digests, asset_hosts, etc.
+ if options[:debug]
+ options[:manifest] = false
+ options[:digest] = false
+ options[:asset_host] = false
+ end
+
# Append extension if necessary
source_ext = File.extname(source)
if options[:ext] && source_ext != ".#{options[:ext]}"
uri.path << ".#{options[:ext]}"
end
-
+
# If a manifest is present, try to grab the path from the manifest first
- if Helpers.manifest && Helpers.manifest.assets[uri.path]
- return ManifestPath.new(uri, Helpers.manifest.assets[uri.path], options).to_s
+ # Don't try looking in manifest in the first place if we're not looking for digest version
+ if options[:manifest] != false && Helpers.manifest && Helpers.manifest.assets[uri.path]
+ return Helpers::ManifestPath.new(uri, Helpers.manifest.assets[uri.path], options).to_s
end
-
+
# If the source points to an asset in the Sprockets
# environment use AssetPath to generate the full path.
assets_environment.resolve(uri.path) do |path|
- return AssetPath.new(uri, assets_environment[path], options).to_s
+ return Helpers::AssetPath.new(uri, assets_environment[path], options).to_s
end
-
+
# Use FilePath for normal files on the file system
- FilePath.new(uri, options).to_s
+ return Helpers::FilePath.new(uri, options).to_s
end
alias_method :path_to_asset, :asset_path
-
+
# Computes the path to a audio asset either in the Sprockets environment
# or the public directory. External URIs are untouched.
#
# ==== Examples
#
@@ -129,11 +139,11 @@
#
def audio_path(source, options = {})
asset_path source, { :dir => 'audios' }.merge(options)
end
alias_method :path_to_audio, :audio_path
-
+
# Computes the path to a font asset either in the Sprockets environment
# or the public directory. External URIs are untouched.
#
# ==== Examples
#
@@ -153,11 +163,11 @@
#
def font_path(source, options = {})
asset_path source, { :dir => 'fonts' }.merge(options)
end
alias_method :path_to_font, :font_path
-
+
# Computes the path to an image asset either in the Sprockets environment
# or the public directory. External URIs are untouched.
#
# ==== Examples
#
@@ -177,11 +187,11 @@
#
def image_path(source, options = {})
asset_path source, { :dir => 'images' }.merge(options)
end
alias_method :path_to_image, :image_path
-
+
# Computes the path to a javascript asset either in the Sprockets
# environment or the public directory. If the +source+ filename has no extension,
# <tt>.js</tt> will be appended. External URIs are untouched.
#
# ==== Examples
@@ -202,11 +212,11 @@
#
def javascript_path(source, options = {})
asset_path source, { :dir => 'javascripts', :ext => 'js' }.merge(options)
end
alias_method :path_to_javascript, :javascript_path
-
+
# Computes the path to a stylesheet asset either in the Sprockets
# environment or the public directory. If the +source+ filename has no extension,
# <tt>.css</tt> will be appended. External URIs are untouched.
#
# ==== Examples
@@ -227,11 +237,11 @@
#
def stylesheet_path(source, options = {})
asset_path source, { :dir => 'stylesheets', :ext => 'css' }.merge(options)
end
alias_method :path_to_stylesheet, :stylesheet_path
-
+
# Computes the path to a video asset either in the Sprockets environment
# or the public directory. External URIs are untouched.
#
# ==== Examples
#
@@ -251,21 +261,17 @@
#
def video_path(source, options = {})
asset_path source, { :dir => 'videos' }.merge(options)
end
alias_method :path_to_video, :video_path
-
+
protected
-
+
# Returns the Sprockets environment #asset_path uses to search for
# assets. This can be overridden for more control, if necessary.
# Defaults to Sprockets::Helpers.environment or the envrionment
# returned by #environment.
def assets_environment
Helpers.environment || environment
end
- end
-
- class Context
- include Helpers
end
end