public
Fork of NZKoz/koz-rails
Description: Koz's rails git-svn clone
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/eventualbuddha/koz-rails.git
Backport: allow array and hash query parameters. Array route parameters 
are converted/to/a/path as before. Closes #7047.

git-svn-id: 
http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@7834 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
bitsweat (author)
Wed Oct 10 22:56:58 -0700 2007
commit  2097512b752a2b9dfe8b90a379f15818e2130f21
tree    882ddaa2c1685fe3707631aaf74b6996e1550453
parent  ab4d36816cbf50beabf436553d17f480438244a2
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Backport: allow array and hash query parameters. Array route parameters are converted/to/a/path as before. #6765, #7047, #7462 [bgipsy, Jeremy McAnally, Dan Kubb, brendan, Diego Algorta Casamayou]
0
+
0
 * Fix in place editor's setter action with non-string fields. #7418 [Andreas]
0
 
0
 
...
451
452
453
454
 
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
 
 
 
 
 
 
472
473
 
474
475
476
...
668
669
670
671
 
672
673
674
...
1230
1231
1232
1233
1234
1235
1236
 
 
 
1237
1238
1239
...
451
452
453
 
454
455
 
 
 
 
 
 
 
 
 
 
 
 
 
456
 
 
457
458
459
460
461
462
463
 
464
465
466
467
...
659
660
661
 
662
663
664
665
...
1221
1222
1223
 
 
 
 
1224
1225
1226
1227
1228
1229
0
@@ -451,26 +451,17 @@ module ActionController
0
       # is given (as an array), only the keys indicated will be used to build
0
       # the query string. The query string will correctly build array parameter
0
       # values.
0
- def build_query_string(hash, only_keys=nil)
0
+ def build_query_string(hash, only_keys = nil)
0
         elements = []
0
-
0
- only_keys ||= hash.keys
0
-
0
- only_keys.each do |key|
0
- value = hash[key] or next
0
- key = CGI.escape key.to_s
0
- if value.class == Array
0
- key << '[]'
0
- else
0
- value = [ value ]
0
- end
0
- value.each { |val| elements << "#{key}=#{CGI.escape(val.to_param.to_s)}" }
0
- end
0
         
0
- query_string = "?#{elements.join("&")}" unless elements.empty?
0
- query_string || ""
0
+ (only_keys || hash.keys).each do |key|
0
+ if value = hash[key]
0
+ elements << value.to_query(key)
0
+ end
0
+ end
0
+ elements.empty? ? '' : "?#{elements.sort * '&'}"
0
       end
0
-
0
+
0
       # Write the real recognition implementation and then resend the message.
0
       def recognize(path, environment={})
0
         write_recognition
0
@@ -668,7 +659,7 @@ module ActionController
0
       end
0
   
0
       def extract_value
0
- "#{local_name} = hash[:#{key}] #{"|| #{default.inspect}" if default}"
0
+ "#{local_name} = hash[:#{key}] && hash[:#{key}].to_param #{"|| #{default.inspect}" if default}"
0
       end
0
       def value_check
0
         if default # Then we know it won't be nil
0
@@ -1230,10 +1221,9 @@ module ActionController
0
         #
0
         # great fun, eh?
0
 
0
- options_as_params = options[:controller] ? { :action => "index" } : {}
0
- options.each do |k, value|
0
- options_as_params[k] = value.to_param
0
- end
0
+ options_as_params = options.clone
0
+ options_as_params[:action] ||= 'index' if options[:controller]
0
+ options_as_params[:action] = options_as_params[:action].to_s if options_as_params[:action]
0
         options_as_params
0
       end
0
   
...
946
947
948
949
 
950
951
952
...
946
947
948
 
949
950
951
952
0
@@ -946,7 +946,7 @@ class RouteTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_expand_array_build_query_string
0
- assert_equal '?x[]=1&x[]=2', order_query_string(@route.build_query_string(:x => [1, 2]))
0
+ assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2]))
0
   end
0
 
0
   def test_escape_spaces_build_query_string_selected_keys
...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
...
123
124
125
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
...
23
24
25
 
 
 
 
 
 
 
 
 
26
27
28
...
114
115
116
 
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
0
@@ -23,15 +23,6 @@ class UrlRewriterTests < Test::Unit::TestCase
0
       @rewriter.rewrite(:controller => 'c', :action => 'a', :id => 'i', :anchor => 'anchor')
0
     )
0
   end
0
-
0
- private
0
- def split_query_string(str)
0
- [str[0].chr] + str[1..-1].split(/&/).sort
0
- end
0
-
0
- def assert_query_equal(q1, q2)
0
- assert_equal(split_query_string(q1), split_query_string(q2))
0
- end
0
 end
0
 
0
 class UrlWriterTests < Test::Unit::TestCase
0
@@ -123,5 +114,54 @@ class UrlWriterTests < Test::Unit::TestCase
0
   ensure
0
     ActionController::Routing::Routes.load!
0
   end
0
-
0
+
0
+ def test_one_parameter
0
+ assert_equal('/c/a?param=val',
0
+ W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :param => 'val')
0
+ )
0
+ end
0
+
0
+ def test_two_parameters
0
+ url = W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :p1 => 'X1', :p2 => 'Y2')
0
+ params = extract_params(url)
0
+ assert_equal params[0], { :p1 => 'X1' }.to_query
0
+ assert_equal params[1], { :p2 => 'Y2' }.to_query
0
+ end
0
+
0
+ def test_hash_parameter
0
+ url = W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => {:name => 'Bob', :category => 'prof'})
0
+ params = extract_params(url)
0
+ assert_equal params[0], { 'query[category]' => 'prof' }.to_query
0
+ assert_equal params[1], { 'query[name]' => 'Bob' }.to_query
0
+ end
0
+
0
+ def test_array_parameter
0
+ url = W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => ['Bob', 'prof'])
0
+ params = extract_params(url)
0
+ assert_equal params[0], { 'query[]' => 'Bob' }.to_query
0
+ assert_equal params[1], { 'query[]' => 'prof' }.to_query
0
+ end
0
+
0
+ def test_hash_recursive_parameters
0
+ url = W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :query => {:person => {:name => 'Bob', :position => 'prof'}, :hobby => 'piercing'})
0
+ params = extract_params(url)
0
+ assert_equal params[0], { 'query[hobby]' => 'piercing' }.to_query
0
+ assert_equal params[1], { 'query[person][name]' => 'Bob' }.to_query
0
+ assert_equal params[2], { 'query[person][position]' => 'prof' }.to_query
0
+ end
0
+
0
+ def test_hash_recursive_and_array_parameters
0
+ url = W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :id => 101, :query => {:person => {:name => 'Bob', :position => ['prof', 'art director']}, :hobby => 'piercing'})
0
+ assert_match %r(^/c/a/101), url
0
+ params = extract_params(url)
0
+ assert_equal params[0], { 'query[hobby]' => 'piercing' }.to_query
0
+ assert_equal params[1], { 'query[person][name]' => 'Bob' }.to_query
0
+ assert_equal params[2], { 'query[person][position][]' => 'art director' }.to_query
0
+ assert_equal params[3], { 'query[person][position][]' => 'prof' }.to_query
0
+ end
0
+
0
+ private
0
+ def extract_params(url)
0
+ url.split('?', 2).last.split('&')
0
+ end
0
 end
...
 
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
8
0
@@ -1,3 +1,8 @@
0
+*SVN*
0
+
0
+* Backport: allow array and hash query parameters. Array route parameters are converted/to/a/path as before. #6765, #7047, #7462 [bgipsy, Jeremy McAnally, Dan Kubb, brendan, Diego Algorta Casamayou]
0
+
0
+
0
 *1.4.3* (October 4th, 2007)
0
 
0
 * Demote Hash#to_xml to use XmlSimple#xml_in_string so it can't read files or stdin. #8453 [candlerb, Jeremy Kemper]
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
...
48
49
50
 
 
 
 
 
 
51
52
53
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
...
66
67
68
69
70
71
72
73
74
75
76
77
0
@@ -1,5 +1,23 @@
0
 require 'date'
0
 require 'xml_simple'
0
+require 'cgi'
0
+
0
+# Extensions needed for Hash#to_query
0
+class Object
0
+ def to_param #:nodoc:
0
+ to_s
0
+ end
0
+
0
+ def to_query(key) #:nodoc:
0
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
0
+ end
0
+end
0
+
0
+class Array
0
+ def to_query(key) #:nodoc:
0
+ collect { |value| value.to_query("#{key}[]") }.sort * '&'
0
+ end
0
+end
0
 
0
 # Locked down XmlSimple#xml_in_string
0
 class XmlSimple
0
@@ -48,6 +66,12 @@ module ActiveSupport #:nodoc:
0
           klass.extend(ClassMethods)
0
         end
0
 
0
+ def to_query(namespace = nil)
0
+ collect do |key, value|
0
+ value.to_query(namespace ? "#{namespace}[#{key}]" : key)
0
+ end.sort * '&'
0
+ end
0
+
0
         def to_xml(options = {})
0
           options[:indent] ||= 2
0
           options.reverse_merge!({ :builder => Builder::XmlMarkup.new(:indent => options[:indent]),
...
470
471
472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
0
@@ -470,3 +470,40 @@ class HashToXmlTest < Test::Unit::TestCase
0
     end
0
   end
0
 end
0
+
0
+class QueryTest < Test::Unit::TestCase
0
+ def test_simple_conversion
0
+ assert_query_equal 'a=10', :a => 10
0
+ end
0
+
0
+ def test_cgi_escaping
0
+ assert_query_equal 'a%3Ab=c+d', 'a:b' => 'c d'
0
+ end
0
+
0
+ def test_nil_parameter_value
0
+ empty = Object.new
0
+ def empty.to_param; nil end
0
+ assert_query_equal 'a=', 'a' => empty
0
+ end
0
+
0
+ def test_nested_conversion
0
+ assert_query_equal 'person%5Bname%5D=Nicholas&person%5Blogin%5D=seckar',
0
+ :person => {:name => 'Nicholas', :login => 'seckar'}
0
+ end
0
+
0
+ def test_multiple_nested
0
+ assert_query_equal 'account%5Bperson%5D%5Bid%5D=20&person%5Bid%5D=10',
0
+ :person => {:id => 10}, :account => {:person => {:id => 20}}
0
+ end
0
+
0
+ def test_array_values
0
+ assert_query_equal 'person%5Bid%5D%5B%5D=10&person%5Bid%5D%5B%5D=20',
0
+ :person => {:id => [10, 20]}
0
+ end
0
+
0
+ private
0
+ def assert_query_equal(expected, actual, message = nil)
0
+ assert_equal expected.split('&').sort, actual.to_query.split('&').sort
0
+ end
0
+end
0
+

Comments

    No one has commented yet.