Merge pull request #1687 from stevenosloan/allow_asset_url_to_work_without_a_resource
Allow asset url to work without passing a current_resource (v4)
This commit is contained in:
commit
45939e7e93
4 changed files with 120 additions and 4 deletions
|
@ -201,7 +201,7 @@ module Middleman
|
||||||
Contract IsA['Middleman::Application'], String, String, Hash => String
|
Contract IsA['Middleman::Application'], String, String, Hash => String
|
||||||
def asset_url(app, path, prefix='', options={})
|
def asset_url(app, path, prefix='', options={})
|
||||||
# Don't touch assets which already have a full path
|
# 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
|
path
|
||||||
else # rewrite paths to use their destination path
|
else # rewrite paths to use their destination path
|
||||||
result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, 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
|
if options[:relative] != true
|
||||||
result
|
result
|
||||||
else
|
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)
|
current_dir = Pathname('/' + options[:current_resource].destination_path)
|
||||||
Pathname(result).relative_path_from(current_dir.dirname).to_s
|
Pathname(result).relative_path_from(current_dir.dirname).to_s
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
require 'middleman-core/util'
|
require 'middleman-core'
|
||||||
|
|
||||||
describe Middleman::Util do
|
describe Middleman::Util do
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ describe Middleman::Util do
|
||||||
it "returns Hashie extended Hash if given a hash" do
|
it "returns Hashie extended Hash if given a hash" do
|
||||||
input = {test: "subject"}
|
input = {test: "subject"}
|
||||||
subject = Middleman::Util.recursively_enhance input
|
subject = Middleman::Util.recursively_enhance input
|
||||||
|
|
||||||
expect( subject.test ).to eq "subject"
|
expect( subject.test ).to eq "subject"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -73,4 +73,72 @@ describe Middleman::Util do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
describe "::asset_url" do
|
||||||
|
|
||||||
|
after(:each) do
|
||||||
|
Given.cleanup!
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when http_prefix is activated" do
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
Given.fixture 'clean-dir-app'
|
||||||
|
Given.file 'source/images/blank.gif', ''
|
||||||
|
@mm = Middleman::Application.new do
|
||||||
|
config[:http_prefix] = 'http_prefix'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns path with http_prefix pre-pended if resource is found" do
|
||||||
|
expect( Middleman::Util.asset_url( @mm, 'blank.gif', 'images', http_prefix: 'http_prefix' ) ).to eq 'http_prefix/images/blank.gif'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns path with http_prefix pre-pended if resource is not found" do
|
||||||
|
expect( Middleman::Util.asset_url( @mm, 'missing.gif', 'images', http_prefix: 'http_prefix' ) ).to eq 'http_prefix/images/missing.gif'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when relative is true" do
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
Given.fixture 'relative-assets-app'
|
||||||
|
@mm = Middleman::Application.new
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns path relative to the provided current_resource" do
|
||||||
|
current_resource = instance_double("Middleman::Sitemap::Resource", destination_path: 'a-path/index.html')
|
||||||
|
expect( Middleman::Util.asset_url( @mm, 'blank.gif', 'images', current_resource: current_resource,
|
||||||
|
relative: true ) ).to eq '../images/blank.gif'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises error if not given a current_resource" do
|
||||||
|
expect{
|
||||||
|
Middleman::Util.asset_url( @mm, 'blank.gif', 'images', relative: true )
|
||||||
|
}.to raise_error ArgumentError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns path if it is already a full path" do
|
||||||
|
expect( Middleman::Util.asset_url( @mm, 'http://example.com' ) ).to eq 'http://example.com'
|
||||||
|
expect( Middleman::Util.asset_url( @mm, 'data:example' ) ).to eq 'data:example'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns a resource url if given a resource's destination path" do
|
||||||
|
Given.fixture 'clean-dir-app' # includes directory indexes extension
|
||||||
|
Given.file 'source/how/about/that.html', ''
|
||||||
|
@mm = Middleman::Application.new
|
||||||
|
|
||||||
|
expect( Middleman::Util.asset_url( @mm, '/how/about/that/index.html' ) ).to eq '/how/about/that/'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns a resource url if given a resources path" do
|
||||||
|
Given.fixture 'clean-dir-app' # includes directory indexes extension
|
||||||
|
Given.file 'source/how/about/that.html', ''
|
||||||
|
@mm = Middleman::Application.new
|
||||||
|
|
||||||
|
expect( Middleman::Util.asset_url( @mm, '/how/about/that.html' ) ).to eq '/how/about/that/'
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
|
@ -12,6 +12,8 @@ RSpec.configure do |config|
|
||||||
config.include Aruba::Api
|
config.include Aruba::Api
|
||||||
end
|
end
|
||||||
|
|
||||||
|
require_relative 'support/given'
|
||||||
|
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
config.filter_run :focus
|
config.filter_run :focus
|
||||||
|
|
42
middleman-core/spec/support/given.rb
Normal file
42
middleman-core/spec/support/given.rb
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
module Given
|
||||||
|
ROOT = File.expand_path( '../..', File.dirname( File.realpath(__FILE__) ) )
|
||||||
|
TMP = File.join( ROOT, 'tmp' )
|
||||||
|
|
||||||
|
class << self
|
||||||
|
|
||||||
|
def fixture name
|
||||||
|
cleanup!
|
||||||
|
|
||||||
|
`rsync -av #{File.join( ROOT, 'fixtures', name )}/ #{TMP}/`
|
||||||
|
Dir.chdir TMP
|
||||||
|
ENV['MM_ROOT'] = TMP
|
||||||
|
end
|
||||||
|
|
||||||
|
def no_file name
|
||||||
|
FileUtils.rm name, force: true
|
||||||
|
end
|
||||||
|
|
||||||
|
def symlink source, destination
|
||||||
|
no_file destination
|
||||||
|
FileUtils.symlink File.expand_path(source),
|
||||||
|
File.expand_path(destination),
|
||||||
|
force: true
|
||||||
|
end
|
||||||
|
|
||||||
|
def file name, content
|
||||||
|
file_path = File.join( TMP, name )
|
||||||
|
FileUtils.mkdir_p( File.dirname(file_path) )
|
||||||
|
File.open( file_path, 'w' ) do |file|
|
||||||
|
file.write content
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def cleanup!
|
||||||
|
Dir.chdir ROOT
|
||||||
|
if File.exist? TMP
|
||||||
|
`rm -rf #{TMP}`
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue