Jan Molič
2006-Aug-09 21:15 UTC
[Rails] Improvement: SiteMapper - working ideas as a possible RoR''s routing replacement
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: -------------------- 1. uses regexp mask to get variables from url and the same regexp mask to create new url. 2. Automatically finds the best route (=> create the best url) in dependence on passed variables. 3. Define a site structure at once with the mapping, that''s because it''s called SiteMapper. 4. Moreover you can determine languages from url. 5. You can dump it instead of caching generated ruby code. 6. 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
Ezra Zygmuntowicz
2006-Aug-13 00:49 UTC
[Rails] Improvement: SiteMapper - working ideas as a possible RoR''s routing replacement
Hi~ On Aug 9, 2006, at 2:14 PM, Jan Moli? wrote:> > 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: > -------------------- > > 1. uses regexp mask to get variables from url and the same regexp > mask to > create new url. > > 2. Automatically finds the best route (=> create the best url) in > dependence on passed variables. > > 3. Define a site structure at once with the mapping, that''s because > it''s called SiteMapper. > > 4. Moreover you can determine languages from url. > > 5. You can dump it instead of caching generated ruby code. > > 6. 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 MolicIf you can link us to the code then we could try it out and give feedback. Is it released somewhere? Cheers- -Ezra