Ticket #3843: rails_url_rewriter.2.diff
| File rails_url_rewriter.2.diff, 2.9 kB (added by redbeard@gmail.com, 4 years ago) |
|---|
-
actionpack/lib/action_controller/url_rewriter.rb
old new 54 54 elements = [] 55 55 query_string = "" 56 56 57 build_elements(hash, only_keys, elements) 58 59 query_string << ("?" + elements.join("&")) unless elements.empty? 60 query_string 61 end 62 63 # Used to build the path elements recusively 64 def build_elements(hash, only_keys, elements, key_prefix = nil, key_suffix = nil) 57 65 only_keys ||= hash.keys 58 66 59 67 only_keys.each do |key| 60 value = hash[key] 68 value = hash[key] 69 61 70 key = CGI.escape key.to_s 62 if value.class == Array 63 key << '[]' 71 key = (key_prefix + key) unless (key_prefix.nil?) 72 key = (key + key_suffix) unless (key_suffix.nil?) 73 74 if value.is_a?(Hash) then 75 build_elements(value, nil, elements, key << "[", "]") 64 76 else 65 value = [ value ] 77 if value.class == Array then 78 key << '[]' 79 else 80 value = [ value ] 81 end 82 value.each { |val| elements << "#{key}=#{Routing.extract_parameter_value(val)}" } 66 83 end 67 value.each { |val| elements << "#{key}=#{Routing.extract_parameter_value(val)}" }68 84 end 69 85 70 query_string << ("?" + elements.join("&")) unless elements.empty?71 query_string72 86 end 87 73 88 end 74 89 end -
actionpack/test/controller/url_rewriter_test.rb
old new 24 24 assert_query_equal '?x=hello+world', @rewriter.send(:build_query_string, {:x => 'hello world', :y => 'goodbye world'}, [:x]) 25 25 end 26 26 27 # This test goes through the router recognition and back, 28 # because the hash is not guaranteed to iterate in the same order, 29 # thus we can't compare the strings. 30 def test_nested_hashes 31 # Setup 32 test_hash = { "a_key" => {"b_key" => "b_key", "c_key" => "c_value", "d_key" => "d_value", "e_value" => { "f_key" => "f_value", "g_key" => "g_value"}}, "h_key" => "h_value" } 33 34 # Perform the URL generation 35 url = @rewriter.send(:build_query_string, test_hash) 36 # (we need to kill the leading '?' as CGIMethods doesn't like that) 37 url = url[1..url.length] 38 39 # Now parse back through the routing code 40 result_hash = CGIMethods.parse_query_parameters(url) 41 42 # Compare and assert 43 assert_equal test_hash, result_hash 44 end 45 27 46 def test_overwrite_params 28 47 @params[:controller] = 'hi' 29 48 @params[:action] = 'bye'