aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller/routing.rb17
-rw-r--r--actionpack/test/controller/routing_test.rb42
2 files changed, 51 insertions, 8 deletions
diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb
index 008e6d2164..f7d3fe3369 100644
--- a/actionpack/lib/action_controller/routing.rb
+++ b/actionpack/lib/action_controller/routing.rb
@@ -26,15 +26,19 @@ module ActionController
end
hash
end
- end
-
- class << self
+
def test_condition(expression, condition)
case condition
when String then "(#{expression} == #{condition.inspect})"
when Regexp then
condition = Regexp.new("^#{condition.source}$") unless /^\^.*\$$/ =~ condition.source
"(#{condition.inspect} =~ #{expression})"
+ when Array then
+ conds = condition.collect do |condition|
+ cond = test_condition(expression, condition)
+ (cond[0, 1] == '(' && cond[-1, 1] == ')') ? cond : "(#{cond})"
+ end
+ "(#{conds.join(' || ')})"
when true then expression
when nil then "! #{expression}"
else
@@ -272,6 +276,7 @@ module ActionController
defaults, conditions = initialize_hashes options.dup
@defaults = defaults.dup
configure_components(defaults, conditions)
+ add_default_requirements
initialize_keys
end
@@ -324,7 +329,6 @@ module ActionController
end
protected
-
def initialize_components(path)
path = path.split('/') if path.is_a? String
path.shift if path.first.blank?
@@ -356,6 +360,11 @@ module ActionController
component.condition = conditions[component.key] if conditions.key?(component.key)
end
end
+
+ def add_default_requirements
+ component_keys = components.collect {|c| c.key}
+ known[:action] ||= [nil, 'index'] unless component_keys.include? :action
+ end
end
class RouteSet #:nodoc:
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index 946dba6c06..1d7613b2ac 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -504,16 +504,19 @@ class RouteTests < Test::Unit::TestCase
end
def test_static
- route 'hello/world', :known => 'known_value'
+ route 'hello/world', :known => 'known_value', :controller => 'content', :action => 'index'
assert_nil rec('hello/turn')
assert_nil rec('turn/world')
- assert_equal({:known => 'known_value'}, rec('hello/world'))
+ assert_equal(
+ {:known => 'known_value', :controller => ::Controllers::ContentController, :action => 'index'},
+ rec('hello/world')
+ )
assert_nil gen(:known => 'foo')
assert_nil gen({})
- assert_equal '/hello/world', gen(:known => 'known_value')
- assert_equal '/hello/world', gen(:known => 'known_value', :extra => 'hi')
+ assert_equal '/hello/world', gen(:known => 'known_value', :controller => 'content', :action => 'index')
+ assert_equal '/hello/world', gen(:known => 'known_value', :extra => 'hi', :controller => 'content', :action => 'index')
assert_equal [:extra], route.extra_keys(:known => 'known_value', :extra => 'hi')
end
@@ -800,8 +803,39 @@ class RouteSetTests < Test::Unit::TestCase
assert_equal({:controller => '/post', :action => 'show'},
x.new.send(:blog_url))
end
+
+ def test_set_to_nil_forgets
+ rs.draw do
+ rs.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil
+ rs.connect ':controller/:action/:id'
+ end
+
+ assert_equal ['/pages/2005', {}],
+ rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005)
+ assert_equal ['/pages/2005/6', {}],
+ rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
+ assert_equal ['/pages/2005/6/12', {}],
+ rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
+
+ assert_equal ['/pages/2005/6/4', {}],
+ rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
+ assert_equal ['/pages/2005/6', {}],
+ rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
+ assert_equal ['/pages/2005', {}],
+ rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
+ end
+
+ def test_url_with_no_action_specified
+ rs.draw do
+ rs.connect '', :controller => 'content'
+ rs.connect ':controller/:action/:id'
+ end
+
+ assert_equal ['/', {}], rs.generate(:controller => 'content', :action => 'index')
+ assert_equal ['/', {}], rs.generate(:controller => 'content')
+ end
end
end