Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Re-Graphing the Mental Model of the Rails Router

Vaidehi Joshi
April 19, 2018
1k

Re-Graphing the Mental Model of the Rails Router

Rails is incredibly powerful because of its abstractions. For years, developers have been able to hit the ground running and be productive without having to know the in's and out's of the core computer science concepts that fuel this framework. But just because something is hidden away doesn't mean that it's not worth knowing! In this talk, we'll unpack the CS fundamentals that are used in the Rails router. Together, we'll demystify the graph data structure that lies under the hood of the router in order to understand how a simple structure allows for routing to actually work in a Rails app.

Vaidehi Joshi

April 19, 2018
Tweet

Transcript

  1. @vaidehijoshi $ rails routes 
 Verb URI Pattern Controller#Action GET

    /articles(.:format) articles#index POST /articles(.:format) articles#create GET /articles/new(.:format) articles#new GET /articles/:id/edit(.:format) articles#edit GET /articles/:id(.:format) articles#show PATCH /articles/:id(.:format) articles#update
 PUT /articles/:id(.:format) articles#update
 DELETE /articles/:id(.:format) articles#destroy
  2. @vaidehijoshi $ rails middleware 
 use Rack::Sendfile use ActionDispatch::Static use

    ActionDispatch::Executor
 use ActiveSupport::Cache::Strategy::LocalCache::Middleware
 … use Rack::Head
 use Rack::ConditionalGet
 use Rack::ETag use Rack::TempfileReaper run ExampleApp::Application.routes
  3. @vaidehijoshi $ rails middleware 
 use Rack::Sendfile use ActionDispatch::Static use

    ActionDispatch::Executor
 use ActiveSupport::Cache::Strategy::LocalCache::Middleware
 … use Rack::Head
 use Rack::ConditionalGet
 use Rack::ETag use Rack::TempfileReaper run ExampleApp::Application.routes
  4. @vaidehijoshi get 'recipes/:id', to: ‘recipes#show’ post ‘articles(.:format)’, to: 'articles#create' post

    'articles/new', to: ‘articles#new’ get ‘articles/:id', to: ‘articles#new’
 # even more routes here…
  5. @vaidehijoshi if request_path =~ /^\articles$/ # go to articles#index
 elsif

    request_path =~ /^\recipes$/
 # go to recipes#index elsif
 # etc etc more looping ahhhhhh! else
 # route not found?
 end linear, O(n)
  6. @vaidehijoshi $ rails routes 
 Verb URI Pattern Controller#Action GET

    /articles(.:format) articles#index POST /articles(.:format) articles#create GET /articles/new(.:format) articles#new GET /articles/:id/edit(.:format) articles#edit GET /articles/:id(.:format) articles#show PATCH /articles/:id(.:format) articles#update
 PUT /articles/:id(.:format) articles#update
 DELETE /articles/:id(.:format) articles#destroy
  7. @vaidehijoshi > scanner.next_token
 => [:SLASH, “/“] > scanner.next_token
 => [:LITERAL,

    "recipes"] 
 > scanner.next_token
 => [:SLASH, “/"]
 > scanner.next_token
 => [:SYMBOL, ":id"]
  8. @vaidehijoshi jumped The cow over the moon over the moon

    noun phrase verb prepositional phrase
  9. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json
  10. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json
  11. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json
  12. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json
  13. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json
  14. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json
  15. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json ✅
  16. @vaidehijoshi get 'recipes/:id', to: ‘recipes#show’ get ‘articles(.:format)’, to: 'articles#index' post

    'articles/new', to: ‘articles#new’ get ‘articles/:id', to: ‘articles#new’
 # even more routes here…
  17. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json
  18. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json
  19. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json
  20. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json
  21. @vaidehijoshi 0 0 0 0 0 0 1 2 4

    5 / articles / (?-mix:[^./?]+) 0 3 0 … . /articles.json