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

Ticket #3843: rails_url_rewriter.2.diff

File rails_url_rewriter.2.diff, 2.9 kB (added by redbeard@gmail.com, 4 years ago)

Patch with fixed indentation and better testing

  • actionpack/lib/action_controller/url_rewriter.rb

    old new  
    5454        elements = [] 
    5555        query_string = "" 
    5656 
     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) 
    5765        only_keys ||= hash.keys 
    5866         
    5967        only_keys.each do |key| 
    60           value = hash[key]  
     68          value = hash[key] 
     69           
    6170          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 << "[", "]") 
    6476          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)}" } 
    6683          end 
    67           value.each { |val| elements << "#{key}=#{Routing.extract_parameter_value(val)}" } 
    6884        end 
    6985         
    70         query_string << ("?" + elements.join("&")) unless elements.empty? 
    71         query_string 
    7286      end 
     87 
    7388  end 
    7489end 
  • actionpack/test/controller/url_rewriter_test.rb

    old new  
    2424    assert_query_equal '?x=hello+world', @rewriter.send(:build_query_string, {:x => 'hello world', :y => 'goodbye world'}, [:x]) 
    2525  end 
    2626 
     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   
    2746  def test_overwrite_params 
    2847    @params[:controller] = 'hi' 
    2948    @params[:action] = 'bye'