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

Rails Internals - Saroj Maharjan

zoras
August 30, 2014

Rails Internals - Saroj Maharjan

Session on #DevMeetup (Aug. 30, 2014), Kathmandu, Nepal.
https://plus.google.com/u/0/events/cr78vpsi1466vv0ldj990fdgdr8

zoras

August 30, 2014
Tweet

More Decks by zoras

Other Decks in Technology

Transcript

  1. Ruby on Rails is_a? • Open-source • On Github •

    git clone rails/rails # using hub • 100% Ruby • No C extensions!
  2. • a Ruby Webserver Interface • an interface between a

    HTTP server, and a Ruby application • a standard language between those two parts
  3. Rack • When the HTTP Server receives a HTTP request,

    it will create a hash called env • A Rack application is any ruby object that can respond to the call method, it can be a lambda, or an instance of a class that defines a call method. ! • rackup
  4. Rack Middlewares • Rack is a two-ways pipeline • add

    some code in the pipeline to modify either the request or the response • A middleware is just like a Rack app, except that you construct it with another Rack application.
  5. Rails application • Rails is a Rack application like the

    one we just saw • config.ru ! ! • Rails.application is an attribute accessor defined in railties/lib/rails.rb • config/environment.rb will Initialize your rails application with
  6. Rails Middleware • When instantiating a rails app, Rails.application is

    set in railties/lib/rails/ application.rb ! ! ! ! ! • Now that we have a Rails.application, rack can use it to speak with our app.
  7. Rails::Application < Rails::Engine ! ! • Rails inserts the routes

    in a action_dispatch.routes key into the rack env • config.middleware.build will return a ActionDispatch::MiddlewareStack.
  8. ActionDispatch::MiddlewareStack • This middleware stack object composed of middlewares. •

    Each middleware is instantiated with the one that follows him in the stack. • The last middleware is instantiated with the actual rack app. • Invoking call on this object will go through all the middlewares and the app.
  9. run MyApp::Application.routes • ActionDispatch::Routing::RouteSet.new in railties/lib/rails/engine.rb • @router is an

    instance of Journey::Router • we are inside the Journey engine, the rails router. • Its job is to find the correct route defined in config/routes.rb for the incoming request. • The router will choose the appropriate controller and action for this request, and place in the Rack env a hash containing them.
  10. ActionDispatch::Routing::RouteSet::Dispatcher • Rails normalizes the params, and tries to get

    a controller from them. params[:controller] • If the controller is not found, Rails will return an empty response with the HTTP status code 404. • X-Cascade • dispatch(controller, params[:action], env)
  11. We did it! • Yes ! • We followed the

    request from the beginning to the final action call.
  12. Ingredients of Rails • gem install rails • let’s look

    at rails.gemspec ! • This gem doesn’t really include any program.
  13. Rails Dependencies • It has no code, but instead it

    defines several dependencies ! ! ! • It means that the rails gem is a meta package to install these 7 gems
  14. What is Railties? • It is a library providing something

    underneath the rails application • or Railroad tie