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

- old
+ new

@@ -1,10 +1,9 @@ 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 @@ -47,10 +46,24 @@ def public_path @public_path ||= './public' end attr_writer :public_path + # The default options for each asset path method. This is where you + # can change your default directories for the fallback directory. + def default_path_options + @default_path_options ||= { + :audio_path => { :dir => 'audios' }, + :font_path => { :dir => 'fonts' }, + :image_path => { :dir => 'images' }, + :javascript_path => { :dir => 'javascripts', :ext => 'js' }, + :stylesheet_path => { :dir => 'stylesheets', :ext => 'css' }, + :video_path => { :dir => 'videos' } + } + end + attr_writer :default_path_options + # Convience method for configuring Sprockets::Helpers. def configure yield self end @@ -97,68 +110,53 @@ # 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[: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 - # 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 + path = find_asset_path(uri, options) + if options[:expand] && path.respond_to?(:to_a) + path.to_a + else + path.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| - 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) + options = { :expand => Helpers.expand }.merge(options) + path = asset_path source, options + if options[:expand] && path.respond_to?(:map) return path.map(&block).join() else yield path end end def javascript_tag(source, options = {}) - asset_tag(source, { :ext => 'js' }.merge(options)) do |path| + options = Helpers.default_path_options[:javascript_path].merge(options) + asset_tag(source, options) do |path| %Q(<script src="#{path}"></script>) end end def stylesheet_tag(source, options = {}) - asset_tag(source, { :ext => 'css' }.merge(options)) do |path| + options = Helpers.default_path_options[:stylesheet_path].merge(options) + asset_tag(source, options) do |path| %Q(<link rel="stylesheet" href="#{path}">) end end # Computes the path to a audio asset either in the Sprockets environment @@ -179,11 +177,11 @@ # audio_path 'subfolder/audio.mp3' # => '/audios/subfolder/audio.mp3' # audio_path '/subfolder/audio.mp3 # => '/subfolder/audio.mp3' # audio_path 'http://www.example.com/img/audio.mp3' # => 'http://www.example.com/img/audio.mp3' # def audio_path(source, options = {}) - asset_path source, { :dir => 'audios' }.merge(options) + asset_path source, Helpers.default_path_options[:audio_path].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. @@ -203,11 +201,11 @@ # font_path 'subfolder/font.ttf' # => '/fonts/subfolder/font.ttf' # font_path '/subfolder/font.ttf # => '/subfolder/font.ttf' # font_path 'http://www.example.com/img/font.ttf' # => 'http://www.example.com/img/font.ttf' # def font_path(source, options = {}) - asset_path source, { :dir => 'fonts' }.merge(options) + asset_path source, Helpers.default_path_options[:font_path].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. @@ -227,11 +225,11 @@ # 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, Helpers.default_path_options[:image_path].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, @@ -252,11 +250,11 @@ # 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, Helpers.default_path_options[:javascript_path].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, @@ -277,11 +275,11 @@ # 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, Helpers.default_path_options[:stylesheet_path].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. @@ -301,11 +299,11 @@ # video_path 'subfolder/video.mp4' # => '/videos/subfolder/video.mp4' # video_path '/subfolder/video.mp4 # => '/subfolder/video.mp4' # video_path 'http://www.example.com/img/video.mp4' # => 'http://www.example.com/img/video.mp4' # def video_path(source, options = {}) - asset_path source, { :dir => 'videos' }.merge(options) + asset_path source, Helpers.default_path_options[:video_path].merge(options) end alias_method :path_to_video, :video_path protected @@ -313,9 +311,22 @@ # 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 + + def find_asset_path(uri, 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 + + return Helpers::FilePath.new(uri, options) end end class Context include Helpers