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. Rails Internals
    Saroj Maharjan (zoras)

    @zoraslapen

    View full-size slide

  2. #RailsTurns10

    View full-size slide

  3. Ruby on Rails is_a?
    • Open-source

    • On Github

    • git clone rails/rails # using hub

    • 100% Ruby

    • No C extensions!

    View full-size slide

  4. This Talk
    • The Request Handling

    • How Rails Works

    • What Rails Gives us

    View full-size slide

  5. The Request Handling

    View full-size slide

  6. • a Ruby Webserver Interface

    • an interface between a HTTP server, and a Ruby
    application

    • a standard language between those two parts

    View full-size slide

  7. 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

    View full-size slide

  8. 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.

    View full-size slide

  9. How Rails Works

    View full-size slide

  10. How Rails server boots
    % rails server

    Rails.root/bin/rails

    View full-size slide

  11. 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

    View full-size slide

  12. 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.

    View full-size slide

  13. 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.

    View full-size slide

  14. 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.

    View full-size slide

  15. Middleware stack of Rails 4.1 app

    View full-size slide

  16. 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.

    View full-size slide

  17. 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)

    View full-size slide

  18. ActionController::Base < ActionController::Metal

    View full-size slide

  19. ActionController::Rendering

    View full-size slide

  20. We did it!
    • Yes !

    • We followed the request from the beginning to
    the final action call.

    View full-size slide

  21. What Rails Gives us

    View full-size slide

  22. Ingredients of Rails
    • gem install rails

    • let’s look at rails.gemspec

    !
    • This gem doesn’t really include any program.

    View full-size slide

  23. 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

    View full-size slide

  24. Rails is a full-stack MVC framework

    View full-size slide

  25. What is Railties?
    • It is a library providing something underneath the
    rails application

    • or Railroad tie

    View full-size slide

  26. Rails is a full-stack MVC framework

    View full-size slide

  27. Q & A
    • gem install rails --pre

    • Thank You!

    !
    !
    !
    !
    @zoraslapen

    View full-size slide