Merb Pluming - The Router
-
Upload
carllerche -
Category
Technology
-
view
6.120 -
download
0
Transcript of Merb Pluming - The Router
![Page 1: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/1.jpg)
Merb PlumbingThe Router
![Page 2: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/2.jpg)
Who am I?
![Page 3: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/3.jpg)
![Page 4: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/4.jpg)
Some simple routes
Matching
Merb::Router.prepare do match("/signup").to( :controller => "users", :action => "new" ).name(:signup) end
Generating
url(:signup) => /signup
![Page 5: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/5.jpg)
Routes with variables
Matching
match("/articles/:year/:month/:day/:title").to( :controller => "articles", :action => "show" ).name(:article)
Generating
url(:article, :year => 2008, :month => 07, :day => 22, :title => "Awesomeness")
/articles/2008/07/22/Awesomeness
![Page 6: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/6.jpg)
Routes with conditions
Matching
match("/articles") do with(:controller => "articles") do match("/:title", :title => /[a-z]+/). to(:action => "with_title") match("/:id").to(:action => "with_id") end end
/articles/Hello_world => with_title /articles/123 => with_id
![Page 7: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/7.jpg)
More conditions
Matching
Merb::Router.prepare do match(:domain => "blogofawesomeness.com") do match("/the-daily-awesome", :method => "get"). to(:articles) # Any other route... end end
![Page 8: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/8.jpg)
The subdomain problem
match(:subdomain => /(.*)/). to(:account => ":subdomain[1]") do # application routes here... end
![Page 9: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/9.jpg)
Optional path segments
Matching
match("/articles(/:year(/:month(/:day)))/:title"). to(:controller => "articles", :action => "show"). name(:article)
Generating
/articles/Hello => { :title => “Hello” }
/articles/2008/07/Hello => { :year => 2008, :month => “07” :title => “Hello” }
![Page 10: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/10.jpg)
... and generation still works!
url(:article, :title => "Hello")
url(:article, :year => "2008", :title => "Hello")
url(:article, :year => "2008", :month => "07", :title => "Hello")
url(:article, :year => "2008", :month => "07", :day => "22", :title => "Hello")
![Page 11: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/11.jpg)
Getting Fancy
Matching
match("(:pref)/:file(.:format)", :pref => ".*"). to(:controller => "cms").name(:page)
/hi => { :file => “hi” } /hi.xml => { :file => “hi”, :format => “xml”} /path/to/hi => { :pref => “/path/to”, :file => “hi }
![Page 12: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/12.jpg)
And you can still generate it url(:page, :file => "hi")
/hi
url(:page, :pref => "/path/to", :file => "hi")
/path/to/hi
url(:page, :pref => "/hello", :file => "world", :format => "mp3")
/hello/world.mp3
![Page 13: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/13.jpg)
The default route
def default_route(params = {}, &block) match("/:controller(/:action(/:id))(.:format)"). to(params, &block).name(:default) end
![Page 14: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/14.jpg)
AWESOME!
![Page 15: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/15.jpg)
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
![Page 16: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/16.jpg)
and you can nest them
Matching
resources :articles do resources :comments end
Generating
link_to "view comment", url(:article_comment, comment.article_id, comment.id)
![Page 17: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/17.jpg)
Nested multiple times
resources :articles do resources :comments end resources :songs do resources :comments end resources :photos do resources :comments end
![Page 18: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/18.jpg)
and... generating?
link_to "view comment", 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...?
![Page 19: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/19.jpg)
Nah... how about this?
link_to "view comment", resource(comment.commentable, comment)
![Page 20: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/20.jpg)
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)
![Page 21: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/21.jpg)
Oh yeah...
match("/:project") do resources :tasks end
- While currently at /awesome/tasks
resource(@task) => /awesome/tasks/123
resource(@task, :project => "woot") => /woot/tasks/123
![Page 22: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/22.jpg)
AWESOME!
![Page 23: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/23.jpg)
Friendly URLs
Matching resources :users, :identify => :name
Generating
resource(@user) # => /users/carl
![Page 24: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/24.jpg)
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
![Page 25: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/25.jpg)
AWESOME!
![Page 26: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/26.jpg)
Redirecting legacy URLs
Old: /articles/123-hello-world
New: /articles/Hello_world
![Page 27: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/27.jpg)
Handle it in the controller?
match("/articles/:id"). to(:controller => "articles", :action => "show")
![Page 28: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/28.jpg)
Try to do it with Regexps?
match("/articles/:id", :id => /^\d+-/). to(:controller => "articles", :action => "legacy")
match("/articles/:url"). to(:controller => "articles", :action => "show")
![Page 29: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/29.jpg)
Or, add your own logic!
match("/articles/:url").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
![Page 30: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/30.jpg)
Authentication works too
match("/secret").defer_to do |request, params| if request.session.authenticated? params end end
![Page 31: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/31.jpg)
Works great for plugins
def protect(&block) protection = Proc.new do |request, params| if request.session.authenticated? params else redirect "/login" end end
defer(protection, &block) end
![Page 32: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/32.jpg)
Awesome!
protect.match("/admin") do # ... admin routes here end
![Page 33: Merb Pluming - The Router](https://reader033.fdocuments.in/reader033/viewer/2022052823/55593089d8b42a4f3d8b4912/html5/thumbnails/33.jpg)
Thank you(Awesome!)