middleman-more 3.0.7 → 3.0.8.pre.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -91,3 +91,28 @@ Feature: link_to helper
91
91
  When I go to "/link_to.html"
92
92
  Then I should see '<a href="/foo/needs_index.html">Needs Index</a>'
93
93
 
94
+ Scenario: link_to preserves query string and anchor and isn't messed up by them
95
+ Given a fixture app "indexable-app"
96
+ And a file named "source/link_to.html.erb" with:
97
+ """
98
+ <%= link_to "Needs Index Anchor", "/needs_index.html#foo" %>
99
+ <%= link_to "Needs Index Query", "/needs_index.html?foo" %>
100
+ <%= link_to "Needs Index Query and Anchor", "/needs_index.html?foo#foo" %>
101
+ """
102
+ And the Server is running at "indexable-app"
103
+ When I go to "/link_to/"
104
+ Then I should see '<a href="/needs_index/#foo">Needs Index Anchor</a>'
105
+ Then I should see '<a href="/needs_index/?foo">Needs Index Query</a>'
106
+ Then I should see '<a href="/needs_index/?foo#foo">Needs Index Query and Anchor</a>'
107
+
108
+ Scenario: link_to accepts a :query option that appends a query string
109
+ Given a fixture app "indexable-app"
110
+ And a file named "source/link_to.html.erb" with:
111
+ """
112
+ <%= link_to "Needs Index String", "/needs_index.html", :query => "foo" %>
113
+ <%= link_to "Needs Index Hash", "/needs_index.html", :query => { :foo => :bar } %>
114
+ """
115
+ And the Server is running at "indexable-app"
116
+ When I go to "/link_to/"
117
+ Then I should see '<a href="/needs_index/?foo">Needs Index String</a>'
118
+ Then I should see '<a href="/needs_index/?foo=bar">Needs Index Hash</a>'
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/object/to_query'
2
+
1
3
  module Middleman
2
4
  module CoreExtensions
3
5
  # Built-in helpers
@@ -113,6 +115,10 @@ module Middleman
113
115
  # set :relative_links, true
114
116
  #
115
117
  # to config.rb to have all links default to relative.
118
+ #
119
+ # There is also a :query option that can be used to append a
120
+ # query string, which can be expressed as either a String,
121
+ # or a Hash which will be turned into URL parameters.
116
122
  def link_to(*args, &block)
117
123
  url_arg_index = block_given? ? 0 : 1
118
124
  options_index = block_given? ? 1 : 2
@@ -130,42 +136,59 @@ module Middleman
130
136
  # Handle relative urls
131
137
  current_source_dir = Pathname('/' + current_resource.path).dirname
132
138
 
133
- path = Pathname(url)
139
+ begin
140
+ uri = URI(url)
141
+ url_path = uri.path
142
+ rescue
143
+ end
134
144
 
135
- url = current_source_dir.join(path).to_s if path.relative?
145
+ if url_path
146
+ path = Pathname(url_path)
147
+ url_path = current_source_dir.join(path).to_s if path.relative?
136
148
 
137
- resource = sitemap.find_resource_by_path(url)
149
+ resource = sitemap.find_resource_by_path(url_path)
138
150
 
139
- # Allow people to turn on relative paths for all links with set :relative_links, true
140
- # but still override on a case by case basis with the :relative parameter.
141
- effective_relative = relative || false
142
- if relative.nil? && relative_links
143
- effective_relative = true
144
- end
151
+ # Allow people to turn on relative paths for all links with config[:relative_links] = true
152
+ # but still override on a case by case basis with the :relative parameter.
153
+ effective_relative = relative || false
154
+ if relative.nil? && relative_links
155
+ effective_relative = true
156
+ end
145
157
 
146
- if resource
147
- if effective_relative
148
- resource_url = resource.url
158
+ if resource
159
+ if effective_relative
160
+ resource_url = resource.url
149
161
 
150
- # Output urls relative to the destination path, not the source path
151
- current_dir = Pathname('/' + current_resource.destination_path).dirname
152
- new_url = Pathname(resource_url).relative_path_from(current_dir).to_s
162
+ # Output urls relative to the destination path, not the source path
163
+ current_dir = Pathname('/' + current_resource.destination_path).dirname
164
+ new_url = Pathname(resource_url).relative_path_from(current_dir).to_s
153
165
 
154
- # Put back the trailing slash to avoid unnecessary Apache redirects
155
- if resource_url.end_with?('/') && !new_url.end_with?('/')
156
- new_url << '/'
166
+ # Put back the trailing slash to avoid unnecessary Apache redirects
167
+ if resource_url.end_with?('/') && !new_url.end_with?('/')
168
+ new_url << '/'
169
+ end
170
+ else
171
+ new_url = resource.url
157
172
  end
173
+
174
+ uri.path = new_url
175
+
176
+ args[url_arg_index] = uri.to_s
158
177
  else
159
- new_url = resource.url
178
+ raise "No resource exists at #{url}" if relative
160
179
  end
161
-
162
- args[url_arg_index] = new_url
163
- else
164
- raise "No resource exists at #{url}" if relative
165
180
  end
166
181
  end
167
182
  end
168
183
 
184
+ # Support a :query option that can be a string or hash
185
+ query = options.delete(:query)
186
+ if query
187
+ uri = URI(args[url_arg_index])
188
+ uri.query = query.respond_to?(:to_param) ? query.to_param : query.to_s
189
+ args[url_arg_index] = uri.to_s
190
+ end
191
+
169
192
  super(*args, &block)
170
193
  end
171
194
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-more
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.7
5
- prerelease:
4
+ version: 3.0.8.pre.1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Thomas Reynolds
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-12-17 00:00:00.000000000 Z
13
+ date: 2013-01-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: middleman-core
@@ -19,7 +19,7 @@ dependencies:
19
19
  requirements:
20
20
  - - '='
21
21
  - !ruby/object:Gem::Version
22
- version: 3.0.7
22
+ version: 3.0.8.pre.1
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,7 +27,7 @@ dependencies:
27
27
  requirements:
28
28
  - - '='
29
29
  - !ruby/object:Gem::Version
30
- version: 3.0.7
30
+ version: 3.0.8.pre.1
31
31
  - !ruby/object:Gem::Dependency
32
32
  name: uglifier
33
33
  requirement: !ruby/object:Gem::Requirement
@@ -641,16 +641,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
641
641
  version: '0'
642
642
  segments:
643
643
  - 0
644
- hash: 4377871555131630904
644
+ hash: 1566393867800215219
645
645
  required_rubygems_version: !ruby/object:Gem::Requirement
646
646
  none: false
647
647
  requirements:
648
- - - ! '>='
648
+ - - ! '>'
649
649
  - !ruby/object:Gem::Version
650
- version: '0'
651
- segments:
652
- - 0
653
- hash: 4377871555131630904
650
+ version: 1.3.1
654
651
  requirements: []
655
652
  rubyforge_project:
656
653
  rubygems_version: 1.8.24