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) |
|---|
-
test/controller/routing_test.rb
old new 1588 1588 controller.send(:hash_for_show_path, :id => 5) 1589 1589 ) 1590 1590 end 1591 1592 def test_named_route_hash_access_method_with_ordered_parameters 1593 controller = setup_named_route_test 1591 1594 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 1592 1616 def test_named_route_url_method 1593 1617 controller = setup_named_route_test 1594 1618 -
lib/action_controller/routing/route_set.rb
old new 138 138 139 139 def define_hash_access(route, name, kind, options) 140 140 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 156 142 # allow ordered parameters to be associated with corresponding 157 143 # dynamic segments, so you can do 158 144 # 159 # foo_url(bar, baz, bang)145 # hash_for_foo_url(bar, baz, bang) 160 146 # 161 147 # instead of 162 148 # 163 # foo_url(:bar => bar, :baz => baz, :bang => bang)149 # hash_for_foo_url(:bar => bar, :baz => baz, :bang => bang) 164 150 # 165 151 # Also allow options hash, so you can do 166 152 # 167 # foo_url(bar, baz, bang, :sort_by => 'baz')153 # hash_for_foo_url(bar, baz, bang, :sort_by => 'baz') 168 154 # 169 155 @module.module_eval <<-end_eval # We use module_eval to avoid leaks 170 156 def #{selector}(*args) 171 #{generate_optimisation_block(route, kind)}172 173 157 opts = if args.empty? || Hash === args.first 174 158 args.first || {} 175 159 else … … 180 164 end 181 165 options.merge(args) 182 166 end 167 opts ? #{options.inspect}.merge(opts) : #{options.inspect} 168 end 169 protected :#{selector} 170 end_eval 171 helpers << selector 172 end 183 173 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)) 185 183 end 186 184 protected :#{selector} 187 185 end_eval