Merge pull request #488 from bhollis/link_to

Improve link_to so that it can handle a Resource in place of a url
This commit is contained in:
Thomas Reynolds 2012-06-16 19:44:42 -07:00
commit 524fd49c47
2 changed files with 19 additions and 7 deletions

View file

@ -1,6 +1,6 @@
Feature: relative_link_to helper
Feature: link_to helper
Scenario: relative_link_to produces relative links
Scenario: link_to produces relative links
Given a fixture app "indexable-app"
And an empty file named "config.rb"
And a file named "source/link_to.html.erb" with:
@ -21,7 +21,7 @@ Feature: relative_link_to helper
Then I should see 'absolute: <a href="../needs_index.html">Needs Index</a>'
Then I should see 'relative: <a href="../needs_index.html">Relative</a>'
Scenario: relative_link_to produces relative links when :relative_links is set to true
Scenario: link_to produces relative links when :relative_links is set to true
Given a fixture app "indexable-app"
And a file named "config.rb" with:
"""
@ -47,7 +47,7 @@ Feature: relative_link_to helper
Then I should see 'absolute: <a href="../needs_index.html">Needs Index</a>'
Then I should see 'relative: <a href="../needs_index.html">Relative</a>'
Scenario: relative_link_to knows about directory indexes
Scenario: link_to knows about directory indexes
Given a fixture app "indexable-app"
And a file named "source/link_to.html.erb" with:
"""
@ -65,4 +65,14 @@ Feature: relative_link_to helper
Then I should see 'relative: <a href="needs_index/">Relative</a>'
When I go to "/link_to/sub/"
Then I should see 'absolute: <a href="../needs_index/">Needs Index</a>'
Then I should see 'relative: <a href="../needs_index/">Relative</a>'
Then I should see 'relative: <a href="../needs_index/">Relative</a>'
Scenario: link_to can take a Resource
Given a fixture app "indexable-app"
And a file named "source/link_to.html.erb" with:
"""
<%= link_to "Needs Index", sitemap.find_resource_by_path("/needs_index.html") %>
"""
And the Server is running at "indexable-app"
When I go to "/link_to/"
Then I should see '<a href="/needs_index/">Needs Index</a>'

View file

@ -124,8 +124,10 @@ module Middleman
options = args[options_index] || {}
relative = options.delete(:relative)
if url.include? '://'
# Handle Resources, which define their own url method
if url.respond_to? :url
args[url_arg_index] = url.url
elsif url.include? '://'
raise "Can't use the relative option with an external URL" if relative
else
# Handle relative urls