diff --git a/lib/merb-core/dispatch/router.rb b/lib/merb-core/dispatch/router.rb index c6d4b165..dc054991 100644 --- a/lib/merb-core/dispatch/router.rb +++ b/lib/merb-core/dispatch/router.rb @@ -236,8 +236,40 @@ def resource(*args) route.generate(params, defaults) end - private - + # Add functionality to the router. This can be in the form of + # including a new module or directly defining new methods. + # + # ==== Parameters + # &block:: + # A block of code used to extend the route builder with. This + # can be including a module or directly defining some new methods + # that should be available to building routes. + # + # ==== Example + # Merb::Router.extensions do + # def domain(name, domain, options={}, &block) + # match(:domain => domain).namespace(name, :path => nil, &block) + # end + # end + # + # In this case, a method 'domain' will be available to the route builder + # which will create namespaces around domains instead of path prefixes. + # + # This can then be used as follows. + # + # Merb::Router.prepare do + # domain(:admin, "my-admin.com") do + # # ... routes come here ... + # end + # end + # --- + # @api public + def extensions(&block) + Router::Behavior.class_eval(&block) + end + + private + # Compiles the routes and creates the +match+ method. # # @api private diff --git a/lib/merb-core/dispatch/router/resources.rb b/lib/merb-core/dispatch/router/resources.rb index 3e93ec27..d10116ae 100644 --- a/lib/merb-core/dispatch/router/resources.rb +++ b/lib/merb-core/dispatch/router/resources.rb @@ -272,11 +272,7 @@ def resource_block(builders, &block) end end # Resources - - class Behavior - include Resources - end - + # Adding the collection and member methods to behavior class ResourceBehavior < Behavior #:nodoc: @@ -301,5 +297,9 @@ def member(action, options = {}) end end + + class Behavior + include Resources + end end end \ No newline at end of file diff --git a/spec/public/router/router_spec.rb b/spec/public/router/router_spec.rb index d8bbc243..db695c70 100644 --- a/spec/public/router/router_spec.rb +++ b/spec/public/router/router_spec.rb @@ -145,7 +145,7 @@ end it "should not be able to match routes anymore" do - lambda { route_for("/users") } + lambda { route_for("/users") }.should raise_error(Merb::Router::NotCompiledError) end end @@ -157,5 +157,21 @@ end end + + describe "#extensions" do + it "should be able to extend the router" do + Merb::Router.extensions do + def hello_world + match("/hello").to(:controller => "world") + end + end + + Merb::Router.prepare do + hello_world + end + + route_for("/hello").should have_route(:controller => "world") + end + end end \ No newline at end of file