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

Best Rails Book You've Never Read

Best Rails Book You've Never Read

A presentation on all the benefits you gain after reading Rebuilding Rails by Noah Gibbs.

DamirSvrtan

June 23, 2016
Tweet

More Decks by DamirSvrtan

Other Decks in Programming

Transcript

  1. The best Rails book
    you’ve never read.
    DAMIR SVRTAN

    View full-size slide

  2. 01
    RUBY/RAILS BOOKS

    View full-size slide

  3. WELL KNOWN BOOKS
    POODR
    Sandi Metz
    RoR Tutorial
    Michael Hartl
    Pickaxe
    David Thomas
    Metaprogramming
    Ruby
    Paolo Perrotta

    View full-size slide

  4. and many more..

    View full-size slide

  5. 02
    REBUILDING RAILS

    View full-size slide

  6. Get your hands dirty
    and build your own
    Ruby Web Framework
    - NOAH GIBBS

    View full-size slide

  7. RAILS INTERNALS CAN TEACH
    YOU A LOT…

    View full-size slide

  8. NOT JUST READING - ALSO
    LOTS OF CODING

    View full-size slide

  9. BUILD YOUR OWN GEM
    bundler new my_framework

    View full-size slide

  10. PUBLISH YOUR FIRST GEM
    push it to rubygems.org

    View full-size slide

  11. LEARN HOW RACK WORKS
    what is Rack?

    View full-size slide

  12. Browser
    App
    Server
    RACK
    Ruby
    App

    View full-size slide

  13. ALMOST EVERY RUBY
    FRAMEWORK USES IT!
    Rails, Sinatra, Hanami, Roda..

    View full-size slide

  14. # config.ru
    require 'rack'
    class MyApp
    def call(env)
    [200, { 'Content-Type' => 'text/html' }, ["Hello World"]]
    end
    end
    run MyApp.new

    View full-size slide

  15. run object.call

    View full-size slide

  16. run proc { |env| [200, { 'Content-Type' => 'text/html' }, [Time.now] }

    View full-size slide

  17. # config.ru
    require 'rack'
    class MyApp
    def call(env)
    [200, { 'Content-Type' => 'text/html' }, ["Hello World"]]
    end
    end
    run MyApp.new

    View full-size slide

  18. env argument

    View full-size slide

  19. {
    "SERVER_SOFTWARE"=>"THIN 1.6.3 CODENAME PROTEIN POWDER",
    "SERVER_NAME"=>"LOCALHOST",
    "RACK.INPUT"=>#,
    "RACK.VERSION"=>[1, 0],
    "RACK.ERRORS"=>#"RACK.MULTITHREAD"=>FALSE,
    "RACK.MULTIPROCESS"=>FALSE,
    "RACK.RUN_ONCE"=>FALSE,
    "REQUEST_METHOD"=>"GET",
    "REQUEST_PATH"=>"/",
    "PATH_INFO"=>"/",
    "REQUEST_URI"=>"/",
    "HTTP_VERSION"=>"HTTP/1.1",
    "HTTP_HOST"=>"LOCALHOST:3000",
    "HTTP_CONNECTION"=>"KEEP-ALIVE",
    "HTTP_CACHE_CONTROL"=>"MAX-AGE=0",
    "HTTP_UPGRADE_INSECURE_REQUESTS"=>"1",
    "HTTP_USER_AGENT"=>"MOZILLA/5.0 (MACINTOSH; INTEL MAC OS X 10_11_3) APPLEWEBKIT/537.36 (KH™L, LIKE
    GECKO) CHROME/51.0.2704.103 SAFARI/537.36",
    "HTTP_ACCEPT"=>"TEXT/H™L,APPLICATION/XH™L+XML,APPLICATION/XML;Q=0.9,IMAGE/WEBP,*/*;Q=0.8",
    "HTTP_ACCEPT_ENCODING"=>"GZIP, DEFLATE, SDCH",
    "HTTP_ACCEPT_LANGUAGE"=>"EN-US,EN;Q=0.8",
    "HTTP_COOKIE"=> "",
    "GATEWAY_INTERFACE"=>"CGI/1.2",
    "SERVER_PORT"=>"3000",
    "QUERY_STRING"=>"",
    "SERVER_PROTOCOL"=>"HTTP/1.1",
    "RACK.URL_SCHEME"=>"HTTP",
    "SCRIPT_NAME"=>"",
    "REMOTE_ADDR"=>"127.0.0.1"
    }

    View full-size slide

  20. # config.ru
    require 'rack'
    class MyApp
    def call(env)
    [200, { 'Content-Type' => 'text/html' }, ["Hello World"]]
    end
    end
    run MyApp.new

    View full-size slide

  21. [200, { 'Content-Type' => 'text/html' }, ["Hello World”]]

    View full-size slide

  22. use Rack::Session::Cookie, :secret => 'abc123'
    use BetterErrors::Middleware if Noodles.env.development?
    use Rack::CommonLogger, $stdout
    use Rack::ContentType
    use Rack::MethodOverride
    use Rack::Static, urls: ["/css", "/images", "/js", "/favicon.ico"], root: "public"
    run Noodles.application

    View full-size slide

  23. BUILD YOUR OWN ROUTER

    View full-size slide

  24. Noodles.http_app.routes do
    root_to 'home#index'
    get 'chat', 'chat#index'
    get "auth/:provider/callback", 'session#success'
    get "auth/failure", 'session#failure'
    post "rooms", "rooms#create"
    delete "logout", 'session#logout'
    resources :users
    end

    View full-size slide

  25. BUILD YOUR OWN
    CONTROLLERS

    View full-size slide

  26. class RoomsController < BaseController
    def create
    room = Room.new(name: params['name'], public: true)
    room.save!
    redirect_to ‘/'
    end
    end

    View full-size slide

  27. HOW TO LOAD
    CONTROLLERS?

    View full-size slide

  28. HOW TO LOAD RAILS
    CONTROLLERS?
    Rails magic autoloading

    View full-size slide

  29. require ‘users_controller’

    View full-size slide

  30. puts $LOAD_PATH
    [
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib",
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/eventmachine-1.0.7/lib",
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/daemons-1.1.9/lib",
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thin-1.6.3/lib",
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-1.6.4/lib",
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/slop-3.6.0/lib",
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/method_source-0.8.2/lib",
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.3/lib",
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/coderay-1.1.1/lib",
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-byebug-3.3.0/lib",
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/byebug-8.2.4/lib"
    ]

    View full-size slide

  31. puts $LOAD_PATH
    [
    "/Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib"
    ]
    ls /Users/damirsvrtan/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0/lib/did_you_mean
    core_ext formatter.rb spell_checkable.rb version.rb
    extra_features jaro_winkler.rb spell_checkers
    extra_features.rb levenshtein.rb verbose_formatter.rb
    require 'did_you_mean/extra_features'

    View full-size slide

  32. controllers_folder_path = File.join("app", "controllers")
    $LOAD_PATH << controllers_folder_path

    View full-size slide

  33. method_missing?

    View full-size slide

  34. const_missing?

    View full-size slide

  35. class Object
    def self.const_missing(constant)
    require Noodles.to_underscore(constant.to_s)
    Object.const_get(constant)
    end
    end
    Admin::UsersController -> admin/users_controller

    View full-size slide

  36. Learn how redirects work (30× status)

    View full-size slide

  37. Learn how render and ERB work (use
    haml, slim, tilt)

    View full-size slide

  38. Learn how cookies work

    View full-size slide

  39. class UsersController < ApplicationController
    def index
    cookies[:favorite_color] ||= :black
    end
    end

    View full-size slide

  40. class UsersController < ApplicationController
    def index
    unless request.cookies[:favorite_color] == :black
    response.headers.push('Set-Cookie', 'favorite_color=black')
    end
    end
    end

    View full-size slide

  41. Build your own small ORM

    View full-size slide

  42. REINVENTING THE WHEEL

    View full-size slide

  43. BETTER GRASP ON RAILS,
    RACK, GEMS & THE HTTP
    LAYER

    View full-size slide

  44. Any questions?
    [email protected]
    @DAMIRSVRTAN
    Visit infinum.co or find us on social networks:
    infinum.co infinumco infinumco infinum

    View full-size slide