fix(Util::asset_url): allow use without passing a current_resource

Most cases the current_resource isn’t actually needed, so calls could be made
from extensions and still get the correct path back.

An exception will be raised with a descriptive message if the current_resource
is needed but not given. (for example, if relative is true — it needs a resource
to base the url on)
feature/livereload-locales-data
Steven Sloan 2015-11-13 16:42:08 -05:00
parent 49a7435dc9
commit 6ab6669456
1 changed files with 5 additions and 1 deletions

View File

@ -201,7 +201,7 @@ module Middleman
Contract IsA['Middleman::Application'], String, String, Hash => String
def asset_url(app, path, prefix='', options={})
# Don't touch assets which already have a full path
if path.include?('//') || path.start_with?('data:') || !options[:current_resource]
if path.include?('//') || path.start_with?('data:')
path
else # rewrite paths to use their destination path
result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path))
@ -218,6 +218,10 @@ module Middleman
if options[:relative] != true
result
else
unless options[:current_resource]
raise ArgumentError, '#asset_url must be run in a context with current_resource if relative: true'
end
current_dir = Pathname('/' + options[:current_resource].destination_path)
Pathname(result).relative_path_from(current_dir.dirname).to_s
end