Add RouteSet#append

Allows specifying blocks to the routeset that will get 	appended after the RouteSet is drawn.
This commit is contained in:
Carl Lerche
2010-09-17 12:05:40 -07:00
parent f2765a1cb3
commit 7418a44096
3 changed files with 73 additions and 4 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -177,6 +177,34 @@ module ApplicationTests
assert_equal 'admin::foo', last_response.body
end
test "reloads appended route blocks" do
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do
match ':controller#:action'
end
RUBY
add_to_config <<-R
routes.append do
match '/win' => lambda { |e| [200, {'Content-Type'=>'text/plain'}, 'WIN'] }
end
R
app 'development'
get '/win'
assert_equal 'WIN', last_response.body
app_file 'config/routes.rb', <<-R
AppTemplate::Application.routes.draw do
match 'lol' => 'hello#index'
end
R
get '/win'
assert_equal 'WIN', last_response.body
end
{"development" => "baz", "production" => "bar"}.each do |mode, expected|
test "reloads routes when configuration is changed in #{mode}" do
controller :foo, <<-RUBY