lib/sprockets/helpers.rb in sprockets-helpers-0.3.0 vs lib/sprockets/helpers.rb in sprockets-helpers-0.4.0
- old
+ new
@@ -1,12 +1,13 @@
-require "sprockets/helpers/version"
-require "sprockets"
+require 'sprockets/helpers/version'
+require 'sprockets'
module Sprockets
module Helpers
- autoload :AssetPath, "sprockets/helpers/asset_path"
- autoload :FilePath, "sprockets/helpers/file_path"
+ autoload :AssetPath, 'sprockets/helpers/asset_path'
+ autoload :FilePath, 'sprockets/helpers/file_path'
+ autoload :ManifestPath, 'sprockets/helpers/manifest_path'
# Pattern for checking if a given path is an external URI.
URI_MATCH = %r(^[-a-z]+://|^cid:|^//)
class << self
@@ -14,23 +15,26 @@
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".
+ # This defaults to '/assets'.
def prefix
- @prefix ||= "/assets"
+ @prefix ||= '/assets'
end
attr_writer :prefix
# The path to the public directory, where the assets
# not managed by Sprockets will be located.
- # Defaults to "./public"
+ # Defaults to './public'
def public_path
- @public_path ||= "./public"
+ @public_path ||= './public'
end
attr_writer :public_path
# Convience method for configuring Sprockets::Helpers.
def configure
@@ -51,32 +55,37 @@
#
# ==== Examples
#
# For files within Sprockets:
#
- # asset_path "xmlhr.js" # => "/assets/xmlhr.js"
- # asset_path "xmlhr", :ext => "js" # => "/assets/xmlhr.js"
- # asset_path "xmlhr.js", :digest => true # => "/assets/xmlhr-27a8f1f96afd8d4c67a59eb9447f45bd.js"
- # asset_path "xmlhr.js", :prefix => "/themes" # => "/themes/xmlhr.js"
+ # asset_path 'xmlhr.js' # => '/assets/xmlhr.js'
+ # asset_path 'xmlhr', :ext => 'js' # => '/assets/xmlhr.js'
+ # asset_path 'xmlhr.js', :digest => true # => '/assets/xmlhr-27a8f1f96afd8d4c67a59eb9447f45bd.js'
+ # asset_path 'xmlhr.js', :prefix => '/themes' # => '/themes/xmlhr.js'
#
# For files outside of Sprockets:
#
- # asset_path "xmlhr" # => "/xmlhr"
- # 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"
+ # asset_path 'xmlhr' # => '/xmlhr'
+ # 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 = {})
return source if source =~ URI_MATCH
# Append extension if necessary
if options[:ext] && File.extname(source).empty?
source << ".#{options[:ext]}"
end
+ # If a manifest is present, try to grab the path from the manifest first
+ if Helpers.manifest && Helpers.manifest.assets[source]
+ return ManifestPath.new(Helpers.manifest.assets[source], 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(source) do |path|
return AssetPath.new(assets_environment[path], options).to_s
end
@@ -92,24 +101,24 @@
#
# ==== Examples
#
# For files within Sprockets:
#
- # javascript_path "xmlhr" # => "/assets/xmlhr.js"
- # javascript_path "dir/xmlhr.js" # => "/assets/dir/xmlhr.js"
- # javascript_path "/dir/xmlhr" # => "/assets/dir/xmlhr.js"
+ # javascript_path 'xmlhr' # => '/assets/xmlhr.js'
+ # javascript_path 'dir/xmlhr.js' # => '/assets/dir/xmlhr.js'
+ # javascript_path '/dir/xmlhr' # => '/assets/dir/xmlhr.js'
#
# For files outside of Sprockets:
#
- # javascript_path "xmlhr" # => "/javascripts/xmlhr.js"
- # javascript_path "dir/xmlhr.js" # => "/javascripts/dir/xmlhr.js"
- # javascript_path "/dir/xmlhr" # => "/dir/xmlhr.js"
- # javascript_path "http://www.example.com/js/xmlhr" # => "http://www.example.com/js/xmlhr"
- # javascript_path "http://www.example.com/js/xmlhr.js" # => "http://www.example.com/js/xmlhr.js"
+ # javascript_path 'xmlhr' # => '/javascripts/xmlhr.js'
+ # javascript_path 'dir/xmlhr.js' # => '/javascripts/dir/xmlhr.js'
+ # javascript_path '/dir/xmlhr' # => '/dir/xmlhr.js'
+ # javascript_path 'http://www.example.com/js/xmlhr' # => 'http://www.example.com/js/xmlhr'
+ # javascript_path 'http://www.example.com/js/xmlhr.js' # => 'http://www.example.com/js/xmlhr.js'
#
def javascript_path(source, options = {})
- asset_path source, { :dir => "javascripts", :ext => "js" }.merge(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,
@@ -117,47 +126,47 @@
#
# ==== Examples
#
# For files within Sprockets:
#
- # stylesheet_path "style" # => "/assets/style.css"
- # stylesheet_path "dir/style.css" # => "/assets/dir/style.css"
- # stylesheet_path "/dir/style.css" # => "/assets/dir/style.css"
+ # stylesheet_path 'style' # => '/assets/style.css'
+ # stylesheet_path 'dir/style.css' # => '/assets/dir/style.css'
+ # stylesheet_path '/dir/style.css' # => '/assets/dir/style.css'
#
# For files outside of Sprockets:
#
- # stylesheet_path "style" # => "/stylesheets/style.css"
- # stylesheet_path "dir/style.css" # => "/stylesheets/dir/style.css"
- # stylesheet_path "/dir/style.css" # => "/dir/style.css"
- # stylesheet_path "http://www.example.com/css/style" # => "http://www.example.com/css/style"
- # stylesheet_path "http://www.example.com/css/style.css" # => "http://www.example.com/css/style.css"
+ # stylesheet_path 'style' # => '/stylesheets/style.css'
+ # stylesheet_path 'dir/style.css' # => '/stylesheets/dir/style.css'
+ # stylesheet_path '/dir/style.css' # => '/dir/style.css'
+ # stylesheet_path 'http://www.example.com/css/style' # => 'http://www.example.com/css/style'
+ # stylesheet_path 'http://www.example.com/css/style.css' # => 'http://www.example.com/css/style.css'
#
def stylesheet_path(source, options = {})
- asset_path source, { :dir => "stylesheets", :ext => "css" }.merge(options)
+ asset_path source, { :dir => 'stylesheets', :ext => 'css' }.merge(options)
end
alias_method :path_to_stylesheet, :stylesheet_path
# Computes the path to an image asset either in the Sprockets environment
# or the public directory. External URIs are untouched.
#
# ==== Examples
#
# With files within Sprockets:
#
- # image_path "edit.png" # => "/assets/edit.png"
- # image_path "icons/edit.png" # => "/assets/icons/edit.png"
- # image_path "/icons/edit.png" # => "/assets/icons/edit.png"
+ # image_path 'edit.png' # => '/assets/edit.png'
+ # image_path 'icons/edit.png' # => '/assets/icons/edit.png'
+ # image_path '/icons/edit.png' # => '/assets/icons/edit.png'
#
# With files outside of Sprockets:
#
- # image_path "edit" # => "/images/edit"
- # image_path "edit.png" # => "/images/edit.png"
- # image_path "icons/edit.png" # => "/images/icons/edit.png"
- # image_path "/icons/edit.png" # => "/icons/edit.png"
- # image_path "http://www.example.com/img/edit.png" # => "http://www.example.com/img/edit.png"
+ # image_path 'edit' # => '/images/edit'
+ # image_path 'edit.png' # => '/images/edit.png'
+ # image_path 'icons/edit.png' # => '/images/icons/edit.png'
+ # image_path '/icons/edit.png' # => '/icons/edit.png'
+ # image_path 'http://www.example.com/img/edit.png' # => 'http://www.example.com/img/edit.png'
#
def image_path(source, options = {})
- asset_path source, { :dir => "images" }.merge(options)
+ asset_path source, { :dir => 'images' }.merge(options)
end
alias_method :path_to_image, :image_path
protected