lib/sprockets/helpers.rb in sprockets-helpers-0.1.0 vs lib/sprockets/helpers.rb in sprockets-helpers-0.2.0
- old
+ new
@@ -11,10 +11,14 @@
class << self
# 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 base URL the Sprocket environment is mapped to.
# This defaults to "/assets".
def prefix
@prefix ||= "/assets"
end
@@ -25,10 +29,15 @@
# 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
# Returns the path to an asset either in the Sprockets environment
# or the public directory. External URIs are untouched.
#
@@ -36,12 +45,12 @@
#
# * <tt>:ext</tt> - The extension to append if the source does not have one.
# * <tt>:dir</tt> - The directory to prepend if the file is in the public directory.
# * <tt>:digest</tt> - Wether or not use the digest paths for assets. Set Sprockets::Helpers.digest for global configuration.
# * <tt>:prefix</tt> - Use a custom prefix for the Sprockets environment. Set Sprockets::Helpers.prefix for global configuration.
+ # * <tt>:body</tt> - Adds a ?body=1 flag that tells Sprockets to return only the body of the asset.
#
- #
# ==== Examples
#
# For files within Sprockets:
#
# asset_path "xmlhr.js" # => "/assets/xmlhr.js"
@@ -66,12 +75,12 @@
source << ".#{options[:ext]}"
end
# If the source points to an asset in the Sprockets
# environment use AssetPath to generate the full path.
- environment.resolve(source) do |path|
- return AssetPath.new(environment.find_asset(path), options).to_s
+ assets_environment.resolve(source) do |path|
+ return AssetPath.new(assets_environment[path], options).to_s
end
# Use FilePath for normal files on the file system
FilePath.new(source, options).to_s
end
@@ -143,9 +152,19 @@
# 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)
+ end
+
+ 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