module Roda::RodaPlugins::NamedRoutes

  1. lib/roda/plugins/named_routes.rb

The named_routes plugin allows for multiple named routes, which the main route block can dispatch to by name at any point by calling r.route. If the named route doesn’t handle the request, execution will continue, and if the named route does handle the request, the response returned by the named route will be returned.

Example:

plugin :multi_route

route('foo') do |r|
  r.is 'bar' do
    '/foo/bar'
  end
end

route('bar') do |r|
  r.is 'foo' do
    '/bar/foo'
  end
end

route do |r|
  r.on "foo" do
    r.route 'foo'
  end

  r.on "bar" do
    r.route 'bar'
  end
end

Note that in multi-threaded code, you should not attempt to add a named route after accepting requests.

To handle development environments that reload code, you can call the route class method without a block to remove an existing named route.

Routing Files

The convention when using the named_routes plugin is to have a single named route per file, and these routing files should be stored in a routes subdirectory in your application. So for the above example, you would use the following files:

routes/bar.rb
routes/foo.rb

Namespace Support

The named_routes plugin also has support for namespaces, allowing you to use r.route at multiple levels in your routing tree. Example:

route('foo') do |r|
  r.on("baz"){r.route("baz", "foo")}
  r.on("quux"){r.route("quux", "foo")}
end

route('bar') do |r|
  r.on("baz"){r.route("baz", "bar")}
  r.on("quux"){r.route("quux", "bar")}
end

route('baz', 'foo') do |r|
  # handles /foo/baz prefix
end

route('quux', 'foo') do |r|
  # handles /foo/quux prefix
end

route('baz', 'bar') do |r|
  # handles /bar/baz prefix
end

route('quux', 'bar') do |r|
  # handles /bar/quux prefix
end

route do |r|
  r.on "foo" do
    r.route("foo")
  end

  r.on "bar" do
    r.route("bar")
  end
end

Routing Files

The convention when using namespaces with the multi_route plugin is to store the routing files in subdirectories per namespace. So for the above example, you would have the following routing files:

routes/bar.rb
routes/bar/baz.rb
routes/bar/quux.rb
routes/foo.rb
routes/foo/baz.rb
routes/foo/quux.rb

Methods

Public Class

  1. configure

Public Class methods

configure(app)

Initialize storage for the named routes.

[show source]
    # File lib/roda/plugins/named_routes.rb
109 def self.configure(app)
110   app.opts[:namespaced_routes] ||= {}
111 end