aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2010-09-17 12:05:40 -0700
committerCarl Lerche <me@carllerche.com>2010-09-17 12:05:40 -0700
commit7418a440967586851a182bbcd6d93dbba9497cdb (patch)
tree35be90b8735b6dc7065762ca6618fedb53502e13 /actionpack
parentf2765a1cb3559f7c9d79d998039b6a43b5830c9f (diff)
downloadrails-7418a440967586851a182bbcd6d93dbba9497cdb.tar.gz
rails-7418a440967586851a182bbcd6d93dbba9497cdb.tar.bz2
rails-7418a440967586851a182bbcd6d93dbba9497cdb.zip
Add RouteSet#append
Allows specifying blocks to the routeset that will get appended after the RouteSet is drawn.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb16
-rw-r--r--actionpack/test/dispatch/routing_test.rb33
2 files changed, 45 insertions, 4 deletions
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index 835ba03784..7ac07fe426 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -217,27 +217,35 @@ module ActionDispatch
self.valid_conditions.delete(:id)
self.valid_conditions.push(:controller, :action)
+ @append = []
@disable_clear_and_finalize = false
clear!
end
def draw(&block)
clear! unless @disable_clear_and_finalize
+ eval_block(block)
+ finalize! unless @disable_clear_and_finalize
+
+ nil
+ end
+
+ def append(&block)
+ @append << block
+ end
+ def eval_block(block)
mapper = Mapper.new(self)
if default_scope
mapper.with_default_scope(default_scope, &block)
else
mapper.instance_exec(&block)
end
-
- finalize! unless @disable_clear_and_finalize
-
- nil
end
def finalize!
return if @finalized
+ @append.each { |blk| eval_block(blk) }
@finalized = true
@set.freeze
end
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index b642adc06b..8a2311ab3e 100644
--- a/actionpack/test/dispatch/routing_test.rb
+++ b/actionpack/test/dispatch/routing_test.rb
@@ -2152,6 +2152,39 @@ private
end
end
+class TestAppendingRoutes < ActionController::IntegrationTest
+ def simple_app(resp)
+ lambda { |e| [ 200, { 'Content-Type' => 'text/plain' }, [resp] ] }
+ end
+
+ setup do
+ s = self
+ @app = ActionDispatch::Routing::RouteSet.new
+ @app.append do
+ match '/hello' => s.simple_app('fail')
+ match '/goodbye' => s.simple_app('goodbye')
+ end
+
+ @app.draw do
+ match '/hello' => s.simple_app('hello')
+ end
+ end
+
+ def test_goodbye_should_be_available
+ get '/goodbye'
+ assert_equal 'goodbye', @response.body
+ end
+
+ def test_hello_should_not_be_overwritten
+ get '/hello'
+ assert_equal 'hello', @response.body
+ end
+
+ def test_missing_routes_are_still_missing
+ get '/random'
+ assert_equal 404, @response.status
+ end
+end
class TestDefaultScope < ActionController::IntegrationTest
module ::Blog