link_to with an absolute path that corresponds to a page path will get rewritten to the page's output URL

This commit is contained in:
Ben Hollis 2012-04-27 22:05:52 -07:00
parent 593cdb27aa
commit 05a769d762
2 changed files with 44 additions and 15 deletions

View file

@ -36,3 +36,20 @@ Feature: Directory Index
Given the Server is running at "indexable-app"
When I go to "/leave_me_alone/"
Then I should see "File Not Found"
Scenario: Link_to knows about directory indexes
Given a fixture app "indexable-app"
And a file named "source/link_to.html.erb" with:
"""
link_to: <%= link_to "Needs Index", "/needs_index.html" %>
explicit_link_to: <%= link_to "Explicit", "/needs_index/index.html" %>
unknown_link_to: <%= link_to "Unknown", "/unknown.html" %>
relative_link_to: <%= link_to "Relative", "needs_index.html" %>
"""
And the Server is running at "indexable-app"
When I go to "/link_to/"
Then I should see 'link_to: <a href="/needs_index/">Needs Index</a>'
Then I should see 'explicit_link_to: <a href="/needs_index/index.html">Explicit</a>'
Then I should see 'unknown_link_to: <a href="/unknown.html">Unknown</a>'
# Relative links aren't touched
Then I should see 'relative_link_to: <a href="needs_index.html">Relative</a>'

View file

@ -102,5 +102,17 @@ module Middleman::CoreExtensions::DefaultHelpers
result_path ||= asset_url(source, asset_folder)
"#{result_path}"
end
def link_to(*args, &block)
url_arg_index = block_given? ? 0 : 1
if url = args[url_arg_index]
# Only try to work with absolute URLs
if url.start_with? '/'
resource = sitemap.find_resource_by_path(url)
args[url_arg_index] = resource.url if resource
end
end
super(*args, &block)
end
end
end