a book about this called Rebuilding Rails. My performance blog posts are often in Ruby Weekly. We work for AppFolio. Github: noahgibbs & rmacklin blog: engineering.appfolio.com
of Rails. We build chunks of working web framework, structured like Rails. Rack-Compatible Controllers Automatic "require" ERB Views Rack Requests and Params Simple Models
We work in sections. Your code will do something useful at the end of a section. Can't get a section working? There's a git branch for each. You can move on.
Annoying. What if it weren't on slides? Here's a coupon to get my book free, just for today. It's the longer version. http://bit.ly/rebuilding2019free Stay, though. You'll get through more if you start now.
web servers. A Rack endpoint acts like a proc. It calls #call with request info and the value is sent back: # config.ru run proc { [ 200, {}, ["hello, web!"]] }
quotes/Gemfile source "https://rubygems.org" gem "r00lz", path: "../r00lz" Run "bundle" from inside "quotes" to install gems and create Gemfile.lock. Now we're ready.
think; "Whoah, man..."; end end class R00lzTest < Minitest::Test def test_new_controller_action e = { "PATH_INFO" => "/ted/think", "QUERY_STRING" => "" } assert_equal 200, ::R00lz::App.new.call(e)[0] end end
to use a constant that doesn't exist, Ruby calls const_missing. So: you can use a class without loading it first if you do the right thing with const_missing.
R00lz to automatically require our classes. # r00lz/lib/r00lz.rb class Object def self.const_missing(c) require R00lz.to_underscore(c.to_s) Object.const_get(c) end end
coding is like that. If you have spare time, you could add a test. You'll need to add a controller file and maybe a $LOAD_PATH entry to test auto- requiring controllers.
module R00lz require "erb" # Or do this up-top class Controller def render(name, b = binding()) template = "app/views/#{name}.html.erb" e = ERB.new(File.read template) e.result(b) end end end
have to. We're going to use models that read files. For our quotes app, we'll use JSON files to contain quotes. Model objects will represent data files, not database rows.
this time and what using it looks like. # quotes/app/q_controller.rb class QController def fq @q = FileModel.find(params["q"] || 1) render :quote end end
slides. You know what you just did anyway? Built a model, like a boss. Take a breath, then "bundle exec rackup -p 3000", and point your browser at "http:// localhost:3000/q/fq".
minute. Some of you saw it just work. Some of you didn't. This was the biggest chunk of code in this whole workshop. (After this you're done.) So let's debug it.
you. Seriously. Here's that coupon again. I'd be honored if you worked through the book - but life's busy, so up to you. http://bit.ly/rebuilding2019free