fix(default_helpers): allow asset_url to work outside of a resource context

This way calls from extensions/etc can call app.asset_url() and get the correct
path back.

Additionally, an exception will be raised if also using relative assets as the current resource
will be needed in that case.
v3-stable
Steven Sloan 2015-11-12 15:27:26 -05:00
parent fe4d3a4bb3
commit 30335091c8
1 changed files with 5 additions and 1 deletions

View File

@ -223,7 +223,7 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
# @return [String] The fully qualified asset url
def asset_url(path, prefix='', options={})
# Don't touch assets which already have a full path
if path.include?('//') || path.start_with?('data:') || !current_resource
if path.include?('//') || path.start_with?('data:')
path
else # rewrite paths to use their destination path
result = if resource = sitemap.find_resource_by_destination_path(url_for(path))
@ -241,6 +241,10 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
if options[:relative] != true
result
else
unless current_resource
raise ArgumentError, "#asset_url must be run in a context with current_resource if relative: true"
end
current_dir = Pathname('/' + current_resource.destination_path)
Pathname(result).relative_path_from(current_dir.dirname).to_s
end