lib/sprockets/helpers.rb in sprockets-helpers-0.7.2 vs lib/sprockets/helpers.rb in sprockets-helpers-0.8.0

- old
+ new

@@ -1,9 +1,10 @@ require 'sprockets' require 'sprockets/helpers/version' require 'sprockets/helpers/base_path' require 'sprockets/helpers/asset_path' +require 'sprockets/helpers/expanded_asset_paths' require 'sprockets/helpers/file_path' require 'sprockets/helpers/manifest_path' require 'uri' module Sprockets @@ -13,10 +14,13 @@ attr_accessor :asset_host # When true, the asset paths will return digest paths. attr_accessor :digest + # When true, expand assets. + attr_accessor :expand + # 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 @@ -118,17 +122,45 @@ 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. + # With expand, return array of paths of assets required by asset. assets_environment.resolve(uri.path) do |path| - return Helpers::AssetPath.new(uri, assets_environment[path], options).to_s + asset = assets_environment[path] + if options[:expand] + return Helpers::ExpandedAssetPaths.new(uri, asset, options).to_a + else + return Helpers::AssetPath.new(uri, asset, options).to_s + end end # Use FilePath for normal files on the file system return Helpers::FilePath.new(uri, options).to_s end alias_method :path_to_asset, :asset_path + + def asset_tag(source, options = {}, &block) + raise ::ArgumentError, 'block missing' unless block + path = asset_path source, { :expand => Helpers.expand }.merge(options) + if options[:expand] && path.kind_of?(Array) + return path.map(&block).join() + else + yield path + end + end + + def javascript_tag(source, options = {}) + asset_tag(source, { :ext => 'js' }.merge(options)) do |path| + %Q(<script src="#{path}"></script>) + end + end + + def stylesheet_tag(source, options = {}) + asset_tag(source, { :ext => 'css' }.merge(options)) do |path| + %Q(<link rel="stylesheet" href="#{path}">) + end + end # Computes the path to a audio asset either in the Sprockets environment # or the public directory. External URIs are untouched. # # ==== Examples