Skip to content
Browse files

Named routes should not provide nil values to url_for. Includes facto…

…ring and extra testcases.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1825 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent f92f6a1 commit 9314793239e3728a457d8048a02b334b8955dd23 @seckar seckar committed Jul 13, 2005
Showing with 54 additions and 5 deletions.
  1. +18 −5 actionpack/lib/action_controller/routing.rb
  2. +36 −0 actionpack/test/controller/routing_test.rb
View
23 actionpack/lib/action_controller/routing.rb
@@ -22,7 +22,7 @@ def controller_relative_to(controller, previous)
def treat_hash(hash)
k = v = nil
hash.each do |k, v|
- hash[k] = (v.respond_to? :to_param) ? v.to_param.to_s : v.to_s
+ hash[k] = (v.respond_to? :to_param) ? v.to_param.to_s : v.to_s if v
end
hash
end
@@ -582,12 +582,25 @@ def hash_access_name(name)
def url_helper_name(name)
"#{name}_url"
end
-
- def name_route(route, name)
- hash = route.defaults.merge(route.known).symbolize_keys
+
+ def known_hash_for_route(route)
+ hash = route.known.symbolize_keys
+ route.defaults.each do |key, value|
+ hash[key.to_sym] ||= value if value
+ end
hash[:controller] = "/#{hash[:controller]}"
-
+
+ hash
+ end
+
+ def define_hash_access_method(route, name)
+ hash = known_hash_for_route(route)
define_method(hash_access_name(name)) { hash }
+ end
+
+ def name_route(route, name)
+ define_hash_access_method(route, name)
+
module_eval(%{def #{url_helper_name name}(options = {})
url_for(#{hash_access_name(name)}.merge(options))
end}, "generated/routing/named_routes/#{name}.rb")
View
36 actionpack/test/controller/routing_test.rb
@@ -689,6 +689,23 @@ def test_named_route_without_hash
end
end
+ def test_named_route_with_regexps
+ rs.draw do |map|
+ rs.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
+ :year => /^\d+$/, :month => /^\d+$/, :day => /^\d+$/
+ rs.connect ':controller/:action/:id'
+ end
+ x = setup_for_named_route
+ assert_equal(
+ {:controller => '/page', :action => 'show', :title => 'hi'},
+ x.new.send(:article_url, :title => 'hi')
+ )
+ assert_equal(
+ {:controller => '/page', :action => 'show', :title => 'hi', :day => 10, :year => 2005, :month => 6},
+ x.new.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
+ )
+ end
+
def test_changing_controller
assert_equal ['/admin/stuff/show/10', {}], rs.generate(
{:controller => 'stuff', :action => 'show', :id => 10},
@@ -766,6 +783,25 @@ def test_recognition_with_uppercase_controller_name
assert_equal({'controller' => ::Controllers::Admin::NewsFeedController, 'action' => 'index'}, rs.recognize_path(%w(Admin NewsFeed)))
assert_equal({'controller' => ::Controllers::Admin::NewsFeedController, 'action' => 'index'}, rs.recognize_path(%w(Admin News_Feed)))
end
+
+ def test_both_requirement_and_optional
+ rs.draw do
+ rs.blog('test/:year', :controller => 'post', :action => 'show',
+ :defaults => { :year => nil },
+ :requirements => { :year => /\d{4}/ }
+ )
+ rs.connect ':controller/:action/:id'
+ end
+
+ assert_equal ['/test', {}], rs.generate(:controller => 'post', :action => 'show')
+ assert_equal ['/test', {}], rs.generate(:controller => 'post', :action => 'show', :year => nil)
+
+ x = setup_for_named_route
+ assert_equal({:controller => '/post', :action => 'show'},
+ x.new.send(:blog_url))
+ end
+
+
end
end

0 comments on commit 9314793

Please sign in to comment.
Something went wrong with that request. Please try again.