Merb Pluming - The Router

6,510 views

Published on

Published in: Technology, Design
1 Comment
7 Likes
Statistics
Notes
No Downloads
Views
Total views
6,510
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
124
Comments
1
Likes
7
Embeds 0
No embeds

No notes for slide

Merb Pluming - The Router

  1. 1. Merb Plumbing The Router
  2. 2. Who am I?
  3. 3. Some simple routes Matching Merb::Router.prepare do match(quot;/signupquot;).to( :controller => quot;usersquot;, :action => quot;newquot; ).name(:signup) end Generating url(:signup) => /signup
  4. 4. Routes with variables Matching match(quot;/articles/:year/:month/:day/:titlequot;).to( :controller => quot;articlesquot;, :action => quot;showquot; ).name(:article) Generating url(:article, :year => 2008, :month => 07, :day => 22, :title => quot;Awesomenessquot;) /articles/2008/07/22/Awesomeness
  5. 5. Routes with conditions Matching match(quot;/articlesquot;) do with(:controller => quot;articlesquot;) do match(quot;/:titlequot;, :title => /[a-z]+/). to(:action => quot;with_titlequot;) match(quot;/:idquot;).to(:action => quot;with_idquot;) end end /articles/Hello_world => with_title /articles/123 => with_id
  6. 6. More conditions Matching Merb::Router.prepare do match(:domain => quot;blogofawesomeness.comquot;) do match(quot;/the-daily-awesomequot;, :method => quot;getquot;). to(:articles) # Any other route... end end
  7. 7. The subdomain problem match(:subdomain => /(.*)/). to(:account => quot;:subdomain[1]quot;) do # application routes here... end
  8. 8. Optional path segments Matching match(quot;/articles(/:year(/:month(/:day)))/:titlequot;). to(:controller => quot;articlesquot;, :action => quot;showquot;). name(:article) Generating /articles/Hello => { :title => “Hello” } /articles/2008/07/Hello => { :year => 2008, :month => “07” :title => “Hello” }
  9. 9. ... and generation still works! url(:article, :title => quot;Helloquot;) url(:article, :year => quot;2008quot;, :title => quot;Helloquot;) url(:article, :year => quot;2008quot;, :month => quot;07quot;, :title => quot;Helloquot;) url(:article, :year => quot;2008quot;, :month => quot;07quot;, :day => quot;22quot;, :title => quot;Helloquot;)
  10. 10. Getting Fancy Matching match(quot;(:pref)/:file(.:format)quot;, :pref => quot;.*quot;). to(:controller => quot;cmsquot;).name(:page) /hi => { :file => “hi” } /hi.xml => { :file => “hi”, :format => “xml”} /path/to/hi => { :pref => “/path/to”, :file => “hi }
  11. 11. And you can still generate it url(:page, :file => quot;hiquot;) /hi url(:page, :pref => quot;/path/toquot;, :file => quot;hiquot;) /path/to/hi url(:page, :pref => quot;/helloquot;, :file => quot;worldquot;, :format => quot;mp3quot;) /hello/world.mp3
  12. 12. The default route def default_route(params = {}, &block) match(quot;/:controller(/:action(/:id))(.:format)quot;). to(params, &block).name(:default) end
  13. 13. AWESOME!
  14. 14. Resource routes Matching resources :articles /articles GET => index /articles POST => create /articles/new GET => new /articles/:id GET => show /articles/:id PUT => update /articles/:id DELETE => destroy /articles/:id/edit GET => edit /articles/:id/delete GET => delete
  15. 15. and you can nest them Matching resources :articles do resources :comments end Generating link_to quot;view commentquot;, url(:article_comment, comment.article_id, comment.id)
  16. 16. Nested multiple times resources :articles do resources :comments end resources :songs do resources :comments end resources :photos do resources :comments end
  17. 17. and... generating? link_to quot;view commentquot;, comment.commentable.is_a?(Article) ? url(:article_comment, comment.commentable.id, comment.id) : comment.commentable.is_a?(Song) ? url(:song_comment, comment.commentable.id, comment.id) : url(:photo_comment, comment.commentable.id, comment.id) wha...?
  18. 18. Nah... how about this? link_to quot;view commentquot;, resource(comment.commentable, comment)
  19. 19. Some more examples resources :users do resources :comments end Generating resource(@user) resource(@user, :comments) vs. vs. url(:user, @user) url(:user_comments, @user) resource @user, @comment vs. url(:user_comment, @user, @comment)
  20. 20. Oh yeah... match(quot;/:projectquot;) do resources :tasks end - While currently at /awesome/tasks resource(@task) => /awesome/tasks/123 resource(@task, :project => quot;wootquot;) => /woot/tasks/123
  21. 21. AWESOME!
  22. 22. Friendly URLs Matching resources :users, :identify => :name Generating resource(@user) # => /users/carl
  23. 23. It works as a block too Matching identify(Article=>:permalink, User=>:login) do resources :users do resources :articles end end Generating resource(@user, @article) /users/carl/articles/hello-world
  24. 24. AWESOME!
  25. 25. Redirecting legacy URLs Old: /articles/123-hello-world New: /articles/Hello_world
  26. 26. Handle it in the controller? match(quot;/articles/:idquot;). to(:controller => quot;articlesquot;, :action => quot;showquot;)
  27. 27. Try to do it with Regexps? match(quot;/articles/:idquot;, :id => /^d+-/). to(:controller => quot;articlesquot;, :action => quot;legacyquot;) match(quot;/articles/:urlquot;). to(:controller => quot;articlesquot;, :action => quot;showquot;)
  28. 28. Or, add your own logic! match(quot;/articles/:urlquot;).defer_to do |request, params| if article = Article.first(:url => params[:url]) params.merge(:article => article) elsif article = Article.first(:id => params[:url]) redirect url(:article, article.url) else false end end
  29. 29. Authentication works too match(quot;/secretquot;).defer_to do |request, params| if request.session.authenticated? params end end
  30. 30. Works great for plugins def protect(&block) protection = Proc.new do |request, params| if request.session.authenticated? params else redirect quot;/loginquot; end end defer(protection, &block) end
  31. 31. Awesome! protect.match(quot;/adminquot;) do # ... admin routes here end
  32. 32. Thank you (Awesome!)

×