Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3720: build_query_string_hash_patch.diff

File build_query_string_hash_patch.diff, 1.9 kB (added by gabriel@gironda.org, 4 years ago)
  • actionpack/test/controller/url_rewriter_test.rb

    old new  
    1919  def test_expand_array_build_query_string 
    2020    assert_query_equal '?x[]=1&x[]=2', @rewriter.send(:build_query_string, :x => [1, 2]) 
    2121  end 
    22  
     22  def test_multidimensional_hashes_build_query_string 
     23    assert_query_equal '?this%5Bis%5D%5Ba%5D=test&this%5Bis_also%5D%5Ba%5D=hash', @rewriter.send(:build_query_string, :this => { 'is' => {'a' => 'test'}, 'is_also' => {'a' => 'hash'} }) 
     24  end 
    2325  def test_escape_spaces_build_query_string_selected_keys 
    2426    assert_query_equal '?x=hello+world', @rewriter.send(:build_query_string, {:x => 'hello world', :y => 'goodbye world'}, [:x]) 
    2527  end 
  • actionpack/lib/action_controller/url_rewriter.rb

    old new  
    5555        query_string = "" 
    5656 
    5757        only_keys ||= hash.keys 
     58 
     59        flatten_query_hash!(hash, only_keys) 
    5860         
    5961        only_keys.each do |key| 
    6062          value = hash[key]  
     
    7072        query_string << ("?" + elements.join("&")) unless elements.empty? 
    7173        query_string 
    7274      end 
     75       
     76      # Flattens the query hash so multidimensional hashes in the params hash work properly. 
     77      def flatten_query_hash!(hash, only_keys) 
     78        hash.each do |k,v| 
     79          next unless v.kind_of? Hash 
     80          v.each do |n,o| 
     81            hash["#{k}[#{n}]"] = o 
     82            only_keys << "#{k}[#{n}]" 
     83          end 
     84          hash.delete(k) 
     85          only_keys.delete(k) 
     86          flatten_query_hash!(hash, only_keys) 
     87        end 
     88      end 
     89       
    7390  end 
    7491end