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

Ticket #11460: hash_for_method_with_ordered_parameters_bug_patch.diff

File hash_for_method_with_ordered_parameters_bug_patch.diff, 4.0 kB (added by berpasan, 7 months ago)

Patch that fixes the bug (includes the test from the previous patch)

  • test/controller/routing_test.rb

    old new  
    15881588      controller.send(:hash_for_show_path, :id => 5) 
    15891589    ) 
    15901590  end 
     1591   
     1592    def test_named_route_hash_access_method_with_ordered_parameters 
     1593    controller = setup_named_route_test 
    15911594 
     1595    assert_equal( 
     1596      { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false }, 
     1597      controller.send(:hash_for_show_url, 5) 
     1598    ) 
     1599    assert_equal( 
     1600      { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true }, 
     1601      controller.send(:hash_for_show_path, 5) 
     1602    ) 
     1603    assert_equal( 
     1604      { :controller => 'people', :action => 'multi', :foo => 5,  
     1605        :bar=> "text", :id=>69, :use_route => :multi, :only_path => false }, 
     1606      controller.send(:hash_for_multi_url, 5, "text", 69) 
     1607    ) 
     1608    assert_equal( 
     1609      { :controller => 'people', :action => 'multi', :foo => 5,  
     1610        :bar=> "text", :id=>69, :use_route => :multi, :only_path => true }, 
     1611      controller.send(:hash_for_multi_path, 5, "text", 69) 
     1612    ) 
     1613 
     1614  end 
     1615 
    15921616  def test_named_route_url_method 
    15931617    controller = setup_named_route_test 
    15941618     
  • lib/action_controller/routing/route_set.rb

    old new  
    138138 
    139139          def define_hash_access(route, name, kind, options) 
    140140            selector = hash_access_name(name, kind) 
    141             @module.module_eval <<-end_eval # We use module_eval to avoid leaks 
    142               def #{selector}(options = nil) 
    143                 options ? #{options.inspect}.merge(options) : #{options.inspect} 
    144               end 
    145               protected :#{selector} 
    146             end_eval 
    147             helpers << selector 
    148           end 
    149  
    150           def define_url_helper(route, name, kind, options) 
    151             selector = url_helper_name(name, kind) 
    152             # The segment keys used for positional paramters 
    153  
    154             hash_access_method = hash_access_name(name, kind) 
    155  
     141             
    156142            # allow ordered parameters to be associated with corresponding 
    157143            # dynamic segments, so you can do 
    158144            # 
    159             #   foo_url(bar, baz, bang) 
     145            #   hash_for_foo_url(bar, baz, bang) 
    160146            # 
    161147            # instead of 
    162148            # 
    163             #   foo_url(:bar => bar, :baz => baz, :bang => bang) 
     149            #   hash_for_foo_url(:bar => bar, :baz => baz, :bang => bang) 
    164150            # 
    165151            # Also allow options hash, so you can do 
    166152            # 
    167             #   foo_url(bar, baz, bang, :sort_by => 'baz') 
     153            #   hash_for_foo_url(bar, baz, bang, :sort_by => 'baz') 
    168154            # 
    169155            @module.module_eval <<-end_eval # We use module_eval to avoid leaks 
    170156              def #{selector}(*args) 
    171                 #{generate_optimisation_block(route, kind)} 
    172  
    173157                opts = if args.empty? || Hash === args.first 
    174158                  args.first || {} 
    175159                else 
     
    180164                  end 
    181165                  options.merge(args) 
    182166                end 
     167                opts ? #{options.inspect}.merge(opts) : #{options.inspect} 
     168              end 
     169              protected :#{selector} 
     170            end_eval 
     171            helpers << selector 
     172          end 
    183173 
    184                 url_for(#{hash_access_method}(opts)) 
     174          def define_url_helper(route, name, kind, options) 
     175            selector = url_helper_name(name, kind) 
     176 
     177            hash_access_method = hash_access_name(name, kind) 
     178 
     179            @module.module_eval <<-end_eval # We use module_eval to avoid leaks 
     180              def #{selector}(*args) 
     181                #{generate_optimisation_block(route, kind)} 
     182                url_for(#{hash_access_method}(*args)) 
    185183              end 
    186184              protected :#{selector} 
    187185            end_eval