From 377aa41a23ec7433d051172973a7220e4f5a8c4a Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Fri, 21 Dec 2012 18:25:49 -0800 Subject: [PATCH 1/3] Make link_to ignore (and preserve) query string and anchor in URLs/paths. --- middleman-more/features/helpers_link_to.feature | 13 +++++++++++++ .../core_extensions/default_helpers.rb | 13 +++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/middleman-more/features/helpers_link_to.feature b/middleman-more/features/helpers_link_to.feature index 990e7e8a..3ecc0895 100644 --- a/middleman-more/features/helpers_link_to.feature +++ b/middleman-more/features/helpers_link_to.feature @@ -91,3 +91,16 @@ Feature: link_to helper When I go to "/link_to.html" Then I should see 'Needs Index' + Scenario: link_to preserves query string and anchor and isn't messed up by them + Given a fixture app "indexable-app" + And a file named "source/link_to.html.erb" with: + """ + <%= link_to "Needs Index Anchor", "/needs_index.html#foo" %> + <%= link_to "Needs Index Query", "/needs_index.html?foo" %> + <%= link_to "Needs Index Query and Anchor", "/needs_index.html?foo#foo" %> + """ + And the Server is running at "indexable-app" + When I go to "/link_to/" + Then I should see 'Needs Index Anchor' + Then I should see 'Needs Index Query' + Then I should see 'Needs Index Query and Anchor' diff --git a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb index f9b66cb6..68e0bf88 100644 --- a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb +++ b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb @@ -130,11 +130,14 @@ module Middleman # Handle relative urls current_source_dir = Pathname('/' + current_resource.path).dirname - path = Pathname(url) + uri = URI(url) + url_path = uri.path - url = current_source_dir.join(path).to_s if path.relative? + path = Pathname(url_path) - resource = sitemap.find_resource_by_path(url) + url_path = current_source_dir.join(path).to_s if path.relative? + + resource = sitemap.find_resource_by_path(url_path) # Allow people to turn on relative paths for all links with config[:relative_links] = true # but still override on a case by case basis with the :relative parameter. @@ -159,7 +162,9 @@ module Middleman new_url = resource.url end - args[url_arg_index] = new_url + uri.path = new_url + + args[url_arg_index] = uri.to_s else raise "No resource exists at #{url}" if relative end From 028c3ec480c8a906190ad0fb8f405c9cb96ca963 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 30 Dec 2012 15:55:46 -0800 Subject: [PATCH 2/3] Fix mail_to --- .../core_extensions/default_helpers.rb | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb index 68e0bf88..9d237f55 100644 --- a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb +++ b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb @@ -133,40 +133,41 @@ module Middleman uri = URI(url) url_path = uri.path - path = Pathname(url_path) + if url_path + path = Pathname(url_path) + url_path = current_source_dir.join(path).to_s if path.relative? - url_path = current_source_dir.join(path).to_s if path.relative? + resource = sitemap.find_resource_by_path(url_path) - resource = sitemap.find_resource_by_path(url_path) - - # Allow people to turn on relative paths for all links with config[:relative_links] = true - # but still override on a case by case basis with the :relative parameter. - effective_relative = relative || false - if relative.nil? && config[:relative_links] - effective_relative = true - end - - if resource - if effective_relative - resource_url = resource.url - - # Output urls relative to the destination path, not the source path - current_dir = Pathname('/' + current_resource.destination_path).dirname - new_url = Pathname(resource_url).relative_path_from(current_dir).to_s - - # Put back the trailing slash to avoid unnecessary Apache redirects - if resource_url.end_with?('/') && !new_url.end_with?('/') - new_url << '/' - end - else - new_url = resource.url + # Allow people to turn on relative paths for all links with config[:relative_links] = true + # but still override on a case by case basis with the :relative parameter. + effective_relative = relative || false + if relative.nil? && config[:relative_links] + effective_relative = true end - uri.path = new_url + if resource + if effective_relative + resource_url = resource.url - args[url_arg_index] = uri.to_s - else - raise "No resource exists at #{url}" if relative + # Output urls relative to the destination path, not the source path + current_dir = Pathname('/' + current_resource.destination_path).dirname + new_url = Pathname(resource_url).relative_path_from(current_dir).to_s + + # Put back the trailing slash to avoid unnecessary Apache redirects + if resource_url.end_with?('/') && !new_url.end_with?('/') + new_url << '/' + end + else + new_url = resource.url + end + + uri.path = new_url + + args[url_arg_index] = uri.to_s + else + raise "No resource exists at #{url}" if relative + end end end end From 9954bce2e3e7af4f935258c9995ba3555cf3a258 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sun, 30 Dec 2012 16:27:33 -0800 Subject: [PATCH 3/3] Add a :query option to link_to to allow specifying query string --- middleman-more/features/helpers_link_to.feature | 12 ++++++++++++ .../core_extensions/default_helpers.rb | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/middleman-more/features/helpers_link_to.feature b/middleman-more/features/helpers_link_to.feature index 3ecc0895..1607cc24 100644 --- a/middleman-more/features/helpers_link_to.feature +++ b/middleman-more/features/helpers_link_to.feature @@ -104,3 +104,15 @@ Feature: link_to helper Then I should see 'Needs Index Anchor' Then I should see 'Needs Index Query' Then I should see 'Needs Index Query and Anchor' + + Scenario: link_to accepts a :query option that appends a query string + Given a fixture app "indexable-app" + And a file named "source/link_to.html.erb" with: + """ + <%= link_to "Needs Index String", "/needs_index.html", :query => "foo" %> + <%= link_to "Needs Index Hash", "/needs_index.html", :query => { :foo => :bar } %> + """ + And the Server is running at "indexable-app" + When I go to "/link_to/" + Then I should see 'Needs Index String' + Then I should see 'Needs Index Hash' diff --git a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb index 9d237f55..ee075b44 100644 --- a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb +++ b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/object/to_query' + module Middleman module CoreExtensions # Built-in helpers @@ -113,6 +115,10 @@ module Middleman # config[:relative_links] = true # # to config.rb to have all links default to relative. + # + # There is also a :query option that can be used to append a + # query string, which can be expressed as either a String, + # or a Hash which will be turned into URL parameters. def link_to(*args, &block) url_arg_index = block_given? ? 0 : 1 options_index = block_given? ? 1 : 2 @@ -172,6 +178,14 @@ module Middleman end end + # Support a :query option that can be a string or hash + query = options.delete(:query) + if query + uri = URI(args[url_arg_index]) + uri.query = query.respond_to?(:to_param) ? query.to_param : query.to_s + args[url_arg_index] = uri.to_s + end + super(*args, &block) end end