Slide 1

Slide 1 text

Understanding How Rails Works by Building It Small 〜 mini-rails 〜 1

Slide 2

Slide 2 text

Nobo I work at SonicGarden Inc. I organize Seto.rb. Also organizing 沖縄Ruby会議03 !(Feb 20 2027) @Hitoshi-Noborikawa 2

Slide 3

Slide 3 text

How It Started 3

Slide 4

Slide 4 text

The more I used Rails every day, the more I wondered what was happening inside. What is Rails.application.routes.draw? What does where actually do? When is a view loaded? I know — let's read the Rails source. 4

Slide 5

Slide 5 text

I tried to look up the definition of where, but... So many methods I didn't understand: include, extend, included, define_method... Too early for me. Time to come back later. 5

Slide 6

Slide 6 text

Expanding what I could read, bit by bit Metaprogramming Reading the source code of small gems Reading the code of projects I work on, closely Maybe I can do it now? 6

Slide 7

Slide 7 text

Reading got easier, but Rails is still too big I understand the parts, but not the whole picture What if I build a small one to see the whole picture? 7

Slide 8

Slide 8 text

mini-rails 8

Slide 9

Slide 9 text

About mini-rails A framework I built myself, modeled mainly on Rails 1–3. Strictly a learning project. I'll publish it on GitHub after this talk. 9

Slide 10

Slide 10 text

What This Talk Covers Walking through Request → Response with a mini blog running on mini-rails. The focus is on the mini-rails implementation. 10

Slide 11

Slide 11 text

Demo 11

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Mapping to Rails mini-rails Rails MiniPack ActionPack MiniRecord ActiveRecord MiniView ActionView MiniModel ActiveModel MiniSupport ActiveSupport 13

Slide 14

Slide 14 text

Start Rack → Router → Controller → View → Response 14

Slide 15

Slide 15 text

Rack Rack → Router → Controller → View → Response 15

Slide 16

Slide 16 text

rackup → run MiniRails.application Rails: the Application sits on top of a middleware stack mini-rails: only the bare minimum middleware Reference: railties/lib/rails/application.rb 16

Slide 17

Slide 17 text

Just calls routes.call(env) 17

Slide 18

Slide 18 text

Router Rack → Router → Controller → View → Response 18

Slide 19

Slide 19 text

draw → resources There's a Mapper class in between — skipping it here 19

Slide 20

Slide 20 text

Defines each action Kept simple on purpose. Rails is far more complex Reference: actionpack/lib/action_dispatch/routing/mapper.rb 20

Slide 21

Slide 21 text

Stores routes in an array Calls add_route 21

Slide 22

Slide 22 text

Finds the route, hands off to the controller GET /posts matches posts#index; dispatch hands off to the controller 22

Slide 23

Slide 23 text

Controller Rack → Router → Controller → View → Response 23

Slide 24

Slide 24 text

Finds PostsController, calls dispatch 24

Slide 25

Slide 25 text

process calls the action 25

Slide 26

Slide 26 text

Calls the action with public_send 26

Slide 27

Slide 27 text

MiniRecord Rack → Router → Controller (MiniRecord) → View → Response 27

Slide 28

Slide 28 text

Post.order 28

Slide 29

Slide 29 text

Post.order goes through all Rails: delegate(*QUERYING_METHODS, to: :all) No delegate here — order, where and find all go through define_method Reference: activerecord/lib/active_record/querying.rb 29

Slide 30

Slide 30 text

all creates a new Relation If it were reused, A's conditions would leak into B Rails: all is current_scope || default_scoped — default_scope and scoping feed in mini-rails: just returns a new Relation 30

Slide 31

Slide 31 text

Stacks up the Relation 31

Slide 32

Slide 32 text

Builds the SQL Rails: builds an Arel AST, and a Visitor turns it into SQL mini-rails: just concatenates strings 32

Slide 33

Slide 33 text

@post.save 33

Slide 34

Slide 34 text

save create_record is called for a new record valid? runs validations and run_callbacks runs before/after callbacks, but I'll skip those here 34

Slide 35

Slide 35 text

create_record 35

Slide 36

Slide 36 text

View Rack → Router → Controller → View → Response 36

Slide 37

Slide 37 text

Renders the view Rails splits this into Callbacks and ImplicitRender; mini-rails has only this method Reference: actionpack/lib/action_controller/metal/implicit_render.rb 37

Slide 38

Slide 38 text

Calls the Renderer 38

Slide 39

Slide 39 text

Reads the template, passes it to ERB Rails: caches templates mini-rails: reads them on every request Reference: actionview/lib/action_view/template.rb 39

Slide 40

Slide 40 text

The action's HTML is a block; the layout's yield calls it 40

Slide 41

Slide 41 text

Response Rack → Router → Controller → View → Response Rails: returns it via Response#to_a mini-rails: named to_rack_response for clarity Reference: actionpack/lib/action_dispatch/http/response.rb 41

Slide 42

Slide 42 text

Finish 🎉🎉 Rack → Router → Controller → View → Response 42

Slide 43

Slide 43 text

Other Features 43

Slide 44

Slide 44 text

Post works without any require Autoloaded at boot Rails: Zeitwerk also uses Ruby's autoload, and reloads on change mini-rails: same idea, once at boot — no reloading 44

Slide 45

Slide 45 text

before_action Appended to an array when the class loads, then run per request via only / except Rails: AbstractController::Callbacks, which also controls conditions and execution order mini-rails: just runs an array of hashes 45

Slide 46

Slide 46 text

Features I Couldn't Cover - MiniSupport::Concern - Migration - Validation - HashWithIndifferentAccess - MiniPack::Flash - MiniView::Helpers - MiniView::Partial 46

Slide 47

Slide 47 text

What to Do After This Talk Read mini-rails Next read the Rails 1.x implementation https://github.com/Hitoshi-Noborikawa/mini-rails 47

Slide 48

Slide 48 text

Thank you! 48