Hello,
I’ve done potencial replacement for RoR’s routing mechanism.
It just works as an independent class so you can try bundled examples
withou installing anything else. I didn’t integrated it into Rails (as
a plugin?) because I don’t know if anyone would appreciate it etc.
SiteMapper features:
-
uses regexp mask to get variables from url and the same regexp mask
to
create new url. -
Automatically finds the best route (=> create the best url) in
dependence on passed variables. -
Define a site structure at once with the mapping, that’s because
it’s called SiteMapper. -
Moreover you can determine languages from url.
-
You can dump it instead of caching generated ruby code.
-
The class is only 8,3KB with comments.
A real example:
mapper = SiteMapper.new
firstly define the map and site structure…
mapper.add( :gallery ) do |level1|
level1.add( :mammals ) do |level2|
level2.route( ‘gallery/mammals’,
:controller=>‘gallery’,
:type=>‘mammals’
)
level1.add( :insect ) do |level2|
level2.route( ‘gallery/insect’,
:controller=>‘gallery’,
:type=>‘insect’
)
level2.add( :description ) do |level3|
level3.route( [ ‘gallery/insect/(.*?).html’, :insect_name ],
:controller=>‘gallery’,
:action=>‘show’
)
end
end
end
then you can recognize an url to a ceratin route
route = mapper.recognize( ‘/gallery/insect/135.html’ )
puts route.parameters.join( ', ’ ) # output: insect_name
puts route.navigation.join( ’ -> ') # output: gallery -> insect ->
description
and create new url using automatic selection of the best route
puts mapper.url_for(
:controller=>‘gallery’,
:action=>‘show’,
:insect_name=>‘567’
) # output: /gallery/insect/567.html (= the last route used )
or let just the recognized route generate the new url
puts route.url_for( :insect_name=>‘567’ )
I think the url parameters can be inherrited from upper levels, too.
I’d like to receive your comments
Jan Molic