WEBVTT 00:00:18.560 --> 00:00:23.780 AKIRA MATSUDA: So. Welcome to Ruby on Rails internal talk. 00:00:24.449 --> 00:00:26.750 This is titled Ruby on Rails Hacking Guide, 00:00:26.750 --> 00:00:30.989 which is deeply inspired by Minero Alki's Legendary Ruby 00:00:30.989 --> 00:00:34.579 book titles Rails- for, Ruby Hacking Guide. 00:00:34.579 --> 00:00:38.719 So, before I get started, let me just introduce 00:00:38.719 --> 00:00:43.590 myself. I am Akira Matsuda - amatsuda on GitHub 00:00:43.590 --> 00:00:47.410 and Twitter. I'm a committer of Ruby and Ruby 00:00:47.410 --> 00:00:52.000 on Rails projects and I'm from Japan. 00:00:52.000 --> 00:00:55.579 As a individual on GitHub, I created a pagination 00:00:55.579 --> 00:01:01.489 library called kaminari and active_decoractor, action_args and et cetera, 00:01:01.489 --> 00:01:04.599 et cetera. Such libraries. 00:01:04.599 --> 00:01:08.149 As a Rails committer, ranked in here, in the 00:01:08.149 --> 00:01:15.149 bottom, like, twenty-seventh. I've done 374 commits so far 00:01:15.310 --> 00:01:19.600 and, since I made my first commit about six 00:01:19.600 --> 00:01:20.939 years ago. 00:01:20.939 --> 00:01:23.509 I initially titled this talk as Ruby on Rails 00:01:23.509 --> 00:01:29.429 Hacking Guide, but I slightly changed my mind after 00:01:29.429 --> 00:01:34.109 I listened to DHH's keynote. This talk should be 00:01:34.109 --> 00:01:38.549 titled like Ruby on Rails Reading Guide. Since we're 00:01:38.549 --> 00:01:42.209 actually gonna spend the whole forty minutes just for 00:01:42.209 --> 00:01:46.249 reading the whole framework code. 00:01:46.249 --> 00:01:50.259 And, not gonna talk anything about, like, science. But 00:01:50.259 --> 00:01:57.119 it's like, reading a novel, reading a story. So, 00:01:57.119 --> 00:01:59.489 why should we, why should we read Rails? I 00:01:59.489 --> 00:02:04.179 think we have three big reasons here. 00:02:04.179 --> 00:02:11.179 Firstly, as DHH defined, we're software writers, and you 00:02:11.700 --> 00:02:13.520 need to read a lot in order to be 00:02:13.520 --> 00:02:17.610 a good writer, right. This is obvious. So, we 00:02:17.610 --> 00:02:20.770 need to read Rails to be a good Rails 00:02:20.770 --> 00:02:27.200 programmer. And if you don't understand any piece of 00:02:27.200 --> 00:02:31.069 code in your application, that means you don't really 00:02:31.069 --> 00:02:34.140 understand your application. 00:02:34.140 --> 00:02:38.860 So, don't let a magical piece of code exist 00:02:38.860 --> 00:02:43.040 in your app. I know that Rails applications are 00:02:43.040 --> 00:02:50.040 filled with such magics like, for example, like this. 00:02:50.680 --> 00:02:55.510 It's such simple, just one line of Ruby code, 00:02:55.510 --> 00:02:58.620 in just one method. But it, it really does 00:02:58.620 --> 00:03:03.000 a lot of things for you, right. But we 00:03:03.000 --> 00:03:06.109 need to be able to explain why, why is 00:03:06.109 --> 00:03:13.109 this controller inheriting from ApplicationController inheriting from ActionController::Base? We. 00:03:13.489 --> 00:03:18.129 Or, how the http request actually calls this in 00:03:18.129 --> 00:03:22.650 index Ruby method. Or why are we setting this 00:03:22.650 --> 00:03:28.269 u- users relation into a controller instance variable. Et 00:03:28.269 --> 00:03:30.790 cetera, et cetera. 00:03:30.790 --> 00:03:36.219 Or, think about this example. This, this, this is 00:03:36.219 --> 00:03:41.709 a typical Rails-ish DSL. This reads good. It's pretty 00:03:41.709 --> 00:03:46.950 straight forward for readers, as a documentation. But, you're 00:03:46.950 --> 00:03:51.230 not an application reader, right. We, we're application writer. 00:03:51.230 --> 00:03:54.879 So you have to write this. And as a 00:03:54.879 --> 00:03:57.840 writer, you don't, don't usually write when you don't 00:03:57.840 --> 00:04:02.200 understand what it means, right. 00:04:02.200 --> 00:04:07.400 So, so a question. What, what, what is this 00:04:07.400 --> 00:04:14.400 Rails.application.routes.draw, or get in the second line. Looks like 00:04:15.219 --> 00:04:21.029 a method call taking a hash parameter. What is 00:04:21.029 --> 00:04:26.430 it doing? Who actually knows, who here actually knows 00:04:26.430 --> 00:04:30.430 what's gonna happen with this code? Can explain? 00:04:30.430 --> 00:04:37.430 Right. So, if you think, if you think you 00:04:41.870 --> 00:04:45.970 can explain how this define a route, if, if 00:04:45.970 --> 00:04:50.319 you cannot explain. Sorry. If you cannot explain that 00:04:50.319 --> 00:04:54.430 means for, it's just a magic. And that's why 00:04:54.430 --> 00:04:58.960 you're, you're in this room now, right. So, you'll 00:04:58.960 --> 00:05:03.370 soon get to know what the get means. 00:05:03.370 --> 00:05:08.400 And as the third reason to read Rails code 00:05:08.400 --> 00:05:15.400 is because Rails.has_many :bugs. So, as I told you, 00:05:16.569 --> 00:05:19.050 I patched a lot. And mostly I, I patch 00:05:19.050 --> 00:05:23.190 Rails because I hit bugs in my production application. 00:05:23.190 --> 00:05:29.139 And I guess, you all have the same experience. 00:05:29.139 --> 00:05:34.280 I'm sure. Rails is basically buggy. And we need 00:05:34.280 --> 00:05:37.319 to deal with them. With the bugs. 00:05:37.319 --> 00:05:41.150 So, naturally, Rails programmer job is not just to 00:05:41.150 --> 00:05:45.349 maintain the application, but you also need to maintain 00:05:45.349 --> 00:05:49.080 Rails framework as well. It's our job. And it's 00:05:49.080 --> 00:05:51.280 your job. So we need to be familiar with 00:05:51.280 --> 00:05:55.250 the code base. 00:05:55.250 --> 00:05:59.710 And strangely, this talk is kind of categorized as 00:05:59.710 --> 00:06:06.660 a novice talk. But, I'm not sure. I'm, I'm 00:06:06.660 --> 00:06:13.660 gonna talk about the Rails internal. So. I hope 00:06:14.110 --> 00:06:15.509 you enjoy. 00:06:15.509 --> 00:06:22.509 Let's begin. This is what I'm gonna talk. So, 00:06:25.889 --> 00:06:29.470 first of all, Ruby on Rails is a open-source 00:06:29.470 --> 00:06:36.470 software on GitHub. There's no C-extension inside. So. I 00:06:36.639 --> 00:06:42.330 mean, there's, there's no C-code. 100% Ruby. That means, 00:06:42.330 --> 00:06:44.940 if you're a Ruby programmer, you just can read 00:06:44.940 --> 00:06:51.800 it and, and you can write it if necessary. 00:06:51.800 --> 00:06:54.750 You can get its source code here, by git 00:06:54.750 --> 00:07:01.750 cloning from GitHub. By the way, here's a tiny 00:07:02.319 --> 00:07:09.319 tip. How you can find the GitHub repository for 00:07:10.240 --> 00:07:14.919 the gem you use. You can use, you can 00:07:14.919 --> 00:07:20.090 use a gem named gem-src. It's on, it's here 00:07:20.090 --> 00:07:24.759 on GitHub. And please take a look at this 00:07:24.759 --> 00:07:27.970 repository. This is how you can install it and 00:07:27.970 --> 00:07:29.599 how you can use it. 00:07:29.599 --> 00:07:35.139 I'm not gonna explain here. But. So, what this 00:07:35.139 --> 00:07:40.569 does is after this gem installed, it will automatically 00:07:40.569 --> 00:07:45.560 git, git clone this source from GitHub repository. So, 00:07:45.560 --> 00:07:49.879 you, you need not to memorize the author's name 00:07:49.879 --> 00:07:52.330 or you can not, you don't need to search 00:07:52.330 --> 00:07:58.629 on GitHub. As a gem install, it automatically gets 00:07:58.629 --> 00:08:01.699 the code from GitHub. It's so useful for social 00:08:01.699 --> 00:08:07.169 coders. 00:08:07.169 --> 00:08:14.169 Anyway. Let's go back to the Rails source. Firstly, 00:08:17.539 --> 00:08:21.319 let's see what's included in the gem. To know 00:08:21.319 --> 00:08:24.250 what's included in gem, take a look at the 00:08:24.250 --> 00:08:31.250 gemspec. So this is taken from the file rails.gemspec. 00:08:32.299 --> 00:08:36.919 Here it defines the files included. It looks like 00:08:36.919 --> 00:08:42.729 it doesn't really include any program. It's just documents, 00:08:42.729 --> 00:08:45.000 right. 00:08:45.000 --> 00:08:49.110 And you can find these declarations. A bunch of 00:08:49.110 --> 00:08:56.110 add_dependencies. So that means Rails has no code inside, 00:08:58.060 --> 00:09:03.760 but instead if defines several dependencies. It means that 00:09:03.760 --> 00:09:06.750 this is a meta, meta-gem, meta-package, to install these 00:09:06.750 --> 00:09:12.210 seven gems, right. 00:09:12.210 --> 00:09:19.210 And these seven gems are in the Rails repository. 00:09:21.390 --> 00:09:25.550 And Rails is a full-stack framework, a full-stack MVC 00:09:25.550 --> 00:09:28.410 framework. And all these M and V and C 00:09:28.410 --> 00:09:34.850 are included in this one repository. So we have 00:09:34.850 --> 00:09:39.130 model, view, and controller. 00:09:39.130 --> 00:09:42.070 And we have these gems. I'm sorry it's, can 00:09:42.070 --> 00:09:44.800 you read it? I'm sorry it's hard to read. 00:09:44.800 --> 00:09:51.800 Anyway. We have ActiveRecord, ActionPack and ActionView. And this 00:09:52.380 --> 00:09:59.380 is something called model, and these are views, and 00:09:59.390 --> 00:10:02.850 these are controllers. 00:10:02.850 --> 00:10:07.100 So, Rails gem is a meta-package that depends on 00:10:07.100 --> 00:10:13.890 these seven gems. 00:10:13.890 --> 00:10:16.510 So let's take a look at the Rails server 00:10:16.510 --> 00:10:23.170 command and, and let me explain how, how the 00:10:23.170 --> 00:10:27.420 Rails actually boots. For, starting up the Rails server, 00:10:27.420 --> 00:10:31.010 you just need to run Rails server command. So, 00:10:31.010 --> 00:10:34.720 what's happening internally when you execute this command? 00:10:34.720 --> 00:10:39.330 When I hit the Rails command it actually executes 00:10:39.330 --> 00:10:43.820 bin slash rails in your application. So, let's read 00:10:43.820 --> 00:10:48.530 that file first. That file should look like this. 00:10:48.530 --> 00:10:52.510 It requires config/boot inside your application first, and then 00:10:52.510 --> 00:10:55.040 requires rails/commands. 00:10:55.040 --> 00:11:01.210 Rails/boot looks like this. It just bundles something. I'm 00:11:01.210 --> 00:11:04.260 gonna skip the bundler part, because it's, it's out 00:11:04.260 --> 00:11:08.910 of today's scope, but it, it, it kind of 00:11:08.910 --> 00:11:12.200 tweaks the $LOAD_PATH nicely. 00:11:12.200 --> 00:11:19.200 And so the second line. Rails/commands. It requires rails/commands. 00:11:24.880 --> 00:11:29.240 So where is "rails" directory? You can find "rails" 00:11:29.240 --> 00:11:34.740 directory in the railties gem. There's a gem called 00:11:34.740 --> 00:11:40.460 railties, which I, I didn't explain. So, next chapter 00:11:40.460 --> 00:11:42.090 is about railties. 00:11:42.090 --> 00:11:49.090 What, what actually railties is? Railties is a very 00:11:49.870 --> 00:11:56.570 wisely-crafted tiny library, initially created by Yehuda Katz and 00:11:56.570 --> 00:12:02.260 Carl, Carl Lerche. So let's dive into it. 00:12:02.260 --> 00:12:09.260 Here's the gemspec. The gemspec says this is about 00:12:10.630 --> 00:12:17.130 Rails internals. And the gemspec shows that now this 00:12:17.130 --> 00:12:24.130 gem includes some libraries. Ruby files. Actually, it includes 00:12:25.920 --> 00:12:30.880 like this. So, when you step inside the railties 00:12:30.880 --> 00:12:36.060 lib directory, you'll see these. One directory and one 00:12:36.060 --> 00:12:41.690 file called rails dot rb. 00:12:41.690 --> 00:12:47.080 So these's the rails directory. And this is how 00:12:47.080 --> 00:12:51.990 Rails/commands dot rb looks like in the railties gem. 00:12:51.990 --> 00:12:58.640 It requires commands_tasks, and then invokes a CommandTask with 00:12:58.640 --> 00:13:05.640 a given command name. ARGV, right. 00:13:07.520 --> 00:13:14.520 In this case, so, in this case server, right. 00:13:14.810 --> 00:13:18.060 So this is the file named commands_tasks dot rb. 00:13:18.060 --> 00:13:21.950 It requires rails/commands/server first. So let's take a look 00:13:21.950 --> 00:13:28.950 at rails/commands/server. And this is rails/commands/server. It requires, at 00:13:32.020 --> 00:13:39.020 very top, it requires rails. So, then jump to 00:13:40.480 --> 00:13:43.810 rails dot rb. Rails dot rb was at the 00:13:43.810 --> 00:13:48.450 very top of the repository, right. 00:13:48.450 --> 00:13:51.580 So this is how rails dot rb looks like. 00:13:51.580 --> 00:13:56.770 It requires rails/application. So, let's go to rails/application. And 00:13:56.770 --> 00:14:01.830 it defines a top-level module named Rails. 00:14:01.830 --> 00:14:08.830 So rails/application, it requires rails/engine, and it defines a 00:14:09.130 --> 00:14:14.070 Rails Application class, inheriting Rails Engine. 00:14:14.070 --> 00:14:21.070 So, jump to rails/engine. Now, rails/engine requires rails/railtie and 00:14:23.190 --> 00:14:28.170 it, Rails Engine class inherits from Rails Railtie. So, 00:14:28.170 --> 00:14:31.360 next file is rails/railtie. 00:14:31.360 --> 00:14:38.080 Railtie, it looks like this. It requires rails/initializable and 00:14:38.080 --> 00:14:41.510 rails/configuration. And defines Rails Railtie class, that includes Rails 00:14:41.510 --> 00:14:48.510 Initializable. So next follow the, next file is rails/initializable. 00:14:48.610 --> 00:14:55.610 It defines a Rails Initizable- Initializer. 00:14:58.980 --> 00:15:05.080 So, what we learned so far is, railties defines 00:15:05.080 --> 00:15:12.080 these core classes, like Application, Engine, Railtie, Initializable. Which 00:15:16.520 --> 00:15:23.520 are very, very important. Actually, the core of Rails. 00:15:24.410 --> 00:15:28.870 So railties is like this. It's the core of 00:15:28.870 --> 00:15:32.200 Rails, in the middle. 00:15:32.200 --> 00:15:34.970 And we had something called Plugin as well, but 00:15:34.970 --> 00:15:38.570 it was gone since Rails 4. 00:15:38.570 --> 00:15:45.570 So, let's skip it. So railties is a library 00:15:48.910 --> 00:15:55.910 providing something underneath the Rails Application. And I have 00:15:57.180 --> 00:16:04.180 another answer for you. Railties is, according to Wikipedia, 00:16:08.410 --> 00:16:13.270 this is the definition of something called Railroad tie. 00:16:13.270 --> 00:16:20.270 It's a rectangular piece of wood which is laid 00:16:21.520 --> 00:16:28.520 underneath the rails, sometimes called sleeper, railway sleeper. 00:16:29.070 --> 00:16:35.830 So this is rail ties. Consider this. The applications 00:16:35.830 --> 00:16:42.200 are the trains that run on Rails, and railties, 00:16:42.200 --> 00:16:46.420 Rails is the framework, and railties is something lies 00:16:46.420 --> 00:16:50.510 underneath the Rails framework. This is why they named 00:16:50.510 --> 00:16:55.450 it railties, I guess. This is a beautiful metaphor, 00:16:55.450 --> 00:16:58.190 isn't it? 00:16:58.190 --> 00:17:01.500 So how does it work? Or, how can we 00:17:01.500 --> 00:17:05.939 use it? This is how, how, how it's work. 00:17:05.939 --> 00:17:10.169 We can git grep the whole railtie repository, and 00:17:10.169 --> 00:17:15.900 you can find some uses of railtie. These are 00:17:15.900 --> 00:17:20.939 in, not only in railties, they're actually, these are 00:17:20.939 --> 00:17:25.339 in mailers, ActiveRecord, ActionPack, blah, blah, blah. 00:17:25.339 --> 00:17:30.379 We're gonna take it. So, so let's take a, 00:17:30.379 --> 00:17:34.820 take a look at it, like this. So, each 00:17:34.820 --> 00:17:39.159 component has its own railtie, inheriting from Rails::Railtie, like 00:17:39.159 --> 00:17:43.250 this. This is how we use railtie. 00:17:43.250 --> 00:17:48.690 So, railtie is not only in the core, but 00:17:48.690 --> 00:17:55.690 in, we can find railties everywhere. Railtie. It's inside 00:17:55.690 --> 00:18:00.779 each component, right. And, so, this is what's written 00:18:00.779 --> 00:18:03.580 inside the railtie class. 00:18:03.580 --> 00:18:06.179 It has some, mainly we, we see two kinds 00:18:06.179 --> 00:18:13.179 of class method calls, config and initializer, right. These 00:18:14.429 --> 00:18:21.429 are class method calls to railtie class. So, what's 00:18:22.799 --> 00:18:24.299 config? 00:18:24.299 --> 00:18:31.299 This is the definition. Railtie config create something. Instantiates 00:18:31.679 --> 00:18:38.679 something like Railtie::Configuration. And so config is a instance 00:18:41.639 --> 00:18:47.070 of Railtie::Configuration. Railtie::Configuration is something like this. It, it 00:18:47.070 --> 00:18:52.220 has one method_missing method. And it accepts any method 00:18:52.220 --> 00:18:55.860 call and just stores the given name and arguments 00:18:55.860 --> 00:19:02.570 and block into a class-level hash. 00:19:02.570 --> 00:19:09.570 So, what is initializer? Initializer looks like this. The, 00:19:10.590 --> 00:19:15.600 it just keeps the given block as a proc 00:19:15.600 --> 00:19:22.600 instance, with a name, with a given name, right. 00:19:26.389 --> 00:19:32.610 So, this is everything. This is what railties is 00:19:32.610 --> 00:19:38.019 doing. It, it sets something, some values via config 00:19:38.019 --> 00:19:42.950 method call and initializer method call. And to, to 00:19:42.950 --> 00:19:49.950 use them for the later use. 00:19:51.299 --> 00:19:54.370 And railties is actually class, but it's prohibited to 00:19:54.370 --> 00:19:59.610 instantiate. So, you cannot make a instance of the 00:19:59.610 --> 00:20:06.610 railtie class. Instead, you just can inherit and, when 00:20:07.809 --> 00:20:13.960 inherited, it, it kind of memorizes the, the children 00:20:13.960 --> 00:20:17.889 class names. 00:20:17.889 --> 00:20:24.759 So, this is a summary what, of what railties 00:20:24.759 --> 00:20:27.759 is doing. Railties is a core of Rails that 00:20:27.759 --> 00:20:30.779 provides Railtie, Engine, Application, et cetera, and we can 00:20:30.779 --> 00:20:34.870 find Railtie in other Rails components like ActiveRecord, ActionPack. 00:20:34.870 --> 00:20:38.580 And a railtie can keep config and initializer values. 00:20:38.580 --> 00:20:43.499 And Railtie ties up these railties, all right. 00:20:43.499 --> 00:20:47.009 So, let's see how, how actually Rails server ties 00:20:47.009 --> 00:20:53.999 these up. And how, how Rails server boots. So, 00:20:53.999 --> 00:20:58.169 let's go back to rails/commands/commands_tasks dot rb. After it 00:20:58.169 --> 00:21:03.929 requiring rails/commands/server, it executes, it requires the application and 00:21:03.929 --> 00:21:07.340 then executes server dot start. 00:21:07.340 --> 00:21:14.340 Server dot start is defined here, because it was 00:21:15.909 --> 00:21:22.909 defined in Rails/commands/server. Server dot start calls, just calls 00:21:23.870 --> 00:21:28.470 super. And super is, super class of this class 00:21:28.470 --> 00:21:30.669 is Rack::Server. 00:21:30.669 --> 00:21:36.870 So, welcome to the Rack world. Now, now we 00:21:36.870 --> 00:21:43.870 need to read the Rack code. Oh my god. 00:21:48.330 --> 00:21:51.970 This is not Rack hacking guide, so I'm just 00:21:51.970 --> 00:21:58.970 gonna skip it. It. Anyway. Finally calls something like 00:22:04.019 --> 00:22:11.019 this, via rack/builder. It evaluates this Ruby string. Like, 00:22:16.610 --> 00:22:22.549 Rack::Builder dot new taking contents of config dot ru 00:22:22.549 --> 00:22:26.610 in your application. 00:22:26.610 --> 00:22:33.610 And Rack::Builder just instance_evals the given block. So, this 00:22:35.110 --> 00:22:42.110 is what's written inside your application's config dot ru. 00:22:43.940 --> 00:22:50.940 It requires config/environment dot rb first, and runs Rails.application. 00:22:52.299 --> 00:22:55.220 Run is also a Rack command, I mean, Rack 00:22:55.220 --> 00:22:58.379 method. 00:22:58.379 --> 00:23:05.379 So, I just, I just, like, explained very fast, 00:23:07.519 --> 00:23:11.730 like, how the method, method call goes. How method 00:23:11.730 --> 00:23:15.409 call graph looks like. So, I guess you find 00:23:15.409 --> 00:23:19.759 it difficult to follow, and how can it actually 00:23:19.759 --> 00:23:24.090 follow these method, method call graphs? 00:23:24.090 --> 00:23:30.929 Here's one tip. Just, puts caller from, in a 00:23:30.929 --> 00:23:35.019 file inside your application. For, example, in config dot 00:23:35.019 --> 00:23:39.919 ru file, like this. Just add a puts caller 00:23:39.919 --> 00:23:44.299 here. And run Rails server. Then, you'll get an 00:23:44.299 --> 00:23:50.870 output like this in your console. 00:23:50.870 --> 00:23:55.730 And these, these are basically what I explained. Like, 00:23:55.730 --> 00:24:02.730 we see commands/task and server and, like, Rack::Builder. 00:24:05.919 --> 00:24:12.210 So, this is very useful. Like, when you get 00:24:12.210 --> 00:24:17.940 lost in the Ruby, Ruby jungle, use puts caller 00:24:17.940 --> 00:24:22.869 in caller locations, and that'll show you the path 00:24:22.869 --> 00:24:25.690 to the place you are. 00:24:25.690 --> 00:24:32.690 All right. Anybody, but. The config dot ru file 00:24:33.759 --> 00:24:39.039 requires config/environment.rb, and this is, as you know, what's 00:24:39.039 --> 00:24:44.679 written inside the config/environment.rb. It requires config/application dot rb 00:24:44.679 --> 00:24:47.610 and calls Rails.application.initialize! 00:24:47.610 --> 00:24:54.610 So, Rails.application.initialize! is the very, very important method. As 00:24:57.809 --> 00:25:01.720 you may notice from, because it, it has an 00:25:01.720 --> 00:25:08.720 exclamation mark. This is what's written in application.rb. Since, 00:25:09.220 --> 00:25:13.529 Rails 4 point 1, it, it really has nothing 00:25:13.529 --> 00:25:16.970 in its, in its class. 00:25:16.970 --> 00:25:23.970 But, but it defines a class called Application, inheriting 00:25:24.360 --> 00:25:31.360 from Rails::Application. 00:25:33.499 --> 00:25:39.460 Before that, it requires rails/all and bundles all the 00:25:39.460 --> 00:25:45.330 bundled gems. So this is rails dot, rails/all dot 00:25:45.330 --> 00:25:51.759 rb. What it actually does is something like this. 00:25:51.759 --> 00:25:57.909 It defines, so, it requires all these railties that 00:25:57.909 --> 00:26:04.909 I showed you before. Railties defines configs and initializers, 00:26:06.330 --> 00:26:10.879 as we have seen already. 00:26:10.879 --> 00:26:17.879 And then, we call Rails.application.initialize exclamation. So, so we, 00:26:22.039 --> 00:26:27.929 we define initializers here and load all initializers and 00:26:27.929 --> 00:26:33.049 then initialize exclamation, right. 00:26:33.049 --> 00:26:40.049 So, this is the initialize method. It run_initializers, and 00:26:41.559 --> 00:26:48.559 run_initializers, it calls run, run method for, for each 00:26:53.059 --> 00:26:59.830 initialize- initializable object in the initializers collection. 00:26:59.830 --> 00:27:06.070 So initializable looks like this. Run just executes the 00:27:06.070 --> 00:27:13.070 given argument. I mean, instance_exec through stored proc inside 00:27:13.950 --> 00:27:20.950 the same context, right. I mean, in this case, 00:27:23.080 --> 00:27:27.119 at sign context is actually the application instance, so 00:27:27.119 --> 00:27:34.119 it just instance_execs the stored procs in the initializers. 00:27:34.460 --> 00:27:38.190 In the application's context. 00:27:38.190 --> 00:27:45.190 So, I said initializers collection. Initializers collection is this. 00:27:52.749 --> 00:27:59.749 And initializers, so, initializers, yeah. In rai- railties initializers 00:28:02.239 --> 00:28:07.860 was created like this. So, what, what's in the 00:28:07.860 --> 00:28:13.669 initializer's collection is instances of Rails initializer, initializable initializer. 00:28:13.669 --> 00:28:20.320 Which, each initializer just, just holds a proc instance, 00:28:20.320 --> 00:28:24.159 right. 00:28:24.159 --> 00:28:27.269 And as we've seen already these are called from 00:28:27.269 --> 00:28:33.129 each Railtie classes. 00:28:33.129 --> 00:28:40.129 So. This is, this is what initialize do. And, 00:28:41.129 --> 00:28:45.659 so let's go back to the server. As we've 00:28:45.659 --> 00:28:52.659 seen, the start method calls super. I'm sorry. I'll 00:28:52.730 --> 00:28:54.440 skip this. 00:28:54.440 --> 00:29:01.440 So. Summary. So, this is what we, what the 00:29:02.830 --> 00:29:08.210 railtie does for booting up the Rails server. Finally, 00:29:08.210 --> 00:29:13.669 it runs Rails::Application in config dot ru. Do you 00:29:13.669 --> 00:29:17.610 remember? It's written in config dot ru file. Run. 00:29:17.610 --> 00:29:20.340 Run the application. 00:29:20.340 --> 00:29:26.539 So what is run? What is run method? It's 00:29:26.539 --> 00:29:31.499 defined like this. And with this, with this definition, 00:29:31.499 --> 00:29:38.499 the Rack server calls Rails.application dot call. Rails.application responds 00:29:41.129 --> 00:29:44.789 to call. So you can call this. That means 00:29:44.789 --> 00:29:47.850 Rails.application is a Rack application. 00:29:47.850 --> 00:29:54.489 And Rails.application as a Rack application, it, it has 00:29:54.489 --> 00:29:58.759 a call method like this. It calls super. The 00:29:58.759 --> 00:30:05.759 super, the Rails.application super class was Rails::Engine. So, so 00:30:07.429 --> 00:30:08.999 jump to Rails::Engine. 00:30:08.999 --> 00:30:15.999 Rails::Engine has this like call method. OK. The call 00:30:16.080 --> 00:30:22.769 goes to app dot call. App is something like 00:30:22.769 --> 00:30:28.460 this. It's a config middleware that contains default middleware 00:30:28.460 --> 00:30:35.460 stack, and the endpoint, something called endpoint. Look at 00:30:36.039 --> 00:30:38.049 this. 00:30:38.049 --> 00:30:44.330 So, what's the end point? End point is a 00:30:44.330 --> 00:30:51.330 route. Route is a ActionDispatch::Routing::RouteSet by default. OK. 00:30:56.169 --> 00:31:03.169 So. So, the next thing is routes. Route is 00:31:06.989 --> 00:31:13.989 usually defined in config/routes dot rb like this. So, 00:31:14.379 --> 00:31:21.289 let's think of this simple route. Routes, Rails.application.routes is, 00:31:21.289 --> 00:31:28.289 is ActionDispatch::Routing::RouteSet. It's defined here in ActionDispatch gem, I'm 00:31:29.159 --> 00:31:33.749 sorry. ActionPack gem in action_dispatch directory. 00:31:33.749 --> 00:31:38.470 It, it also responds to call, as it's a 00:31:38.470 --> 00:31:45.470 Rack application. And call goes to router dot call. 00:31:47.460 --> 00:31:54.460 Router is defined like this. Journey::Router.new. So, so router 00:31:54.690 --> 00:31:57.059 is a Journey thing. 00:31:57.059 --> 00:32:04.059 Then, so Rails.application routes is also a Rack application 00:32:14.080 --> 00:32:17.989 that defines endpoint, and it finally goes to the 00:32:17.989 --> 00:32:20.679 Journey thing. But for now let's go back to 00:32:20.679 --> 00:32:25.590 the Routes.draw DSL. 00:32:25.590 --> 00:32:28.340 Here. 00:32:28.340 --> 00:32:35.340 So, routes.draw is defined like this. It just evaluates 00:32:37.059 --> 00:32:44.059 the given block. In the ActionDispatch routing mapper. So, 00:32:47.570 --> 00:32:54.519 this DSL actually calls ActionDispatch::Routing::Mapper#get method, right. And get 00:32:54.519 --> 00:32:58.799 method is like this. It calls map_method. map_method calls 00:32:58.799 --> 00:33:05.600 match. match calls decomposed_match, and decomposed_match calls add_route. 00:33:05.600 --> 00:33:12.600 So, anyway. It finally goes to Mapper#add_route. And Mapper 00:33:14.190 --> 00:33:21.190 add_route calls Mapping dot new route. And add_route returns 00:33:24.330 --> 00:33:31.330 a, finally, it returns a Journey route. And then, 00:33:31.879 --> 00:33:37.789 like, adds the given, sorry, the traded route into 00:33:37.789 --> 00:33:42.649 the collection. Journey::Routes collection. 00:33:42.649 --> 00:33:49.649 So. So, this is what the Journey does. And, 00:33:51.059 --> 00:33:58.059 what's this? Yeah. It finally calls Mapper to_route, Mapping#to_route, 00:33:58.609 --> 00:34:02.340 so to_route is defined like this. 00:34:02.340 --> 00:34:09.340 I'm just gonna skip this. It's, it just returns 00:34:11.379 --> 00:34:16.219 the given app parameter, which was the endpoint given 00:34:16.219 --> 00:34:21.239 into the methods parameter. So it finally, finally it, 00:34:21.239 --> 00:34:28.010 it returns endpoint. And endpoint is if, if it 00:34:28.010 --> 00:34:32.679 responds_to call, just returns that. And if not, call 00:34:32.679 --> 00:34:34.500 Dispatcher. 00:34:34.500 --> 00:34:41.500 So. So the, the. The hash value can, could 00:34:45.239 --> 00:34:49.750 be respond_to call, for example, like this. Or mounted 00:34:49.750 --> 00:34:53.659 engine, for example. But usually it doesn't respond to 00:34:53.659 --> 00:35:00.660 call, so it calls dispatcher. Dispatcher is a Routing::RouteSet::Dispatcher. 00:35:04.940 --> 00:35:08.080 And Dispatcher is also a Rack app, so it 00:35:08.080 --> 00:35:12.190 can be called. And the call the Dispatcher goes 00:35:12.190 --> 00:35:16.900 to, dispatch method. 00:35:16.900 --> 00:35:23.900 Dispatch, so, it finally goes at the bottom line. 00:35:25.030 --> 00:35:28.470 It finally goes to controller dot action with a 00:35:28.470 --> 00:35:35.470 given action name, dot call. So, so finally, what, 00:35:36.970 --> 00:35:42.730 what the router finally calls is this. FooController dot 00:35:42.730 --> 00:35:49.730 action method with a given action, action name. And 00:35:50.280 --> 00:35:52.830 it returns something callable. 00:35:52.830 --> 00:35:58.790 So, this is the route DSL. get "/hello" => 00:35:58.790 --> 00:36:05.790 "foo#bar" maps to FooController dot action('bar'). 00:36:10.760 --> 00:36:15.090 This is about Journey. And I think I have 00:36:15.090 --> 00:36:17.150 no time, so I'm gonna skip this. I'm gonna 00:36:17.150 --> 00:36:22.200 skip this long Journey. Anyway, it resolves the route. 00:36:22.200 --> 00:36:26.610 So, this is a summary. The, the main point 00:36:26.610 --> 00:36:29.800 is that 'foo#bar' becomes a Rack application generated by 00:36:29.800 --> 00:36:33.740 FooController dot action('bar'). 00:36:33.740 --> 00:36:39.870 So, the final chapter. Controllers and actions. This, what's 00:36:39.870 --> 00:36:42.770 gonna happen when the server got a request? It, 00:36:42.770 --> 00:36:49.770 it, it just calls this. So what is this? 00:36:50.500 --> 00:36:57.500 Firstly, the action method is defined like this. It's, 00:37:00.730 --> 00:37:06.140 it calls the new dot dispatch inside the block. 00:37:06.140 --> 00:37:09.260 New, new means Controller dot new. 00:37:09.260 --> 00:37:16.260 So, dispatch, dispatch method creates set, call set_response! and 00:37:16.430 --> 00:37:20.310 then calls super. So, what this dispatch method does 00:37:20.310 --> 00:37:27.310 is creates Rails response object. And then dispatch method, 00:37:29.720 --> 00:37:34.320 then creates request object. Whoops. 00:37:34.320 --> 00:37:39.990 All right. It sets the request object into the 00:37:39.990 --> 00:37:46.990 instance. And then calls process method. Dispatch finally calls 00:37:47.730 --> 00:37:53.940 ActionController::Metal#process. So, this is what's written inside the process 00:37:53.940 --> 00:37:58.590 method. process method calls process_action, and process_action calls send_action, 00:37:58.590 --> 00:38:05.590 and send_action is aliased to send. Ruby's send method, 00:38:07.140 --> 00:38:09.130 right. 00:38:09.130 --> 00:38:16.130 So, FooController dot action('bar') creates the Rails Request and 00:38:17.140 --> 00:38:21.780 Response objects into the controller instance. Then, it just 00:38:21.780 --> 00:38:25.370 calls, calls the method name bar via Ruby send 00:38:25.370 --> 00:38:28.810 method, OK. 00:38:28.810 --> 00:38:34.240 So, it can be like, FooController dot new, with 00:38:34.240 --> 00:38:38.400 something, dot bar. It's a very simple Ruby method 00:38:38.400 --> 00:38:43.080 call, finally. So, this is what's happening inside the 00:38:43.080 --> 00:38:50.080 HTTP request. When the Rails server accepts the HTTP 00:38:51.980 --> 00:38:57.360 request, it, it kind of, the Journey kind of, 00:38:57.360 --> 00:39:02.060 somehow, resolves the results, and finally calls this simple 00:39:02.060 --> 00:39:06.430 Ruby method. OK? 00:39:06.430 --> 00:39:10.530 And I guess it's, yeah. I guess it's time. 00:39:10.530 --> 00:39:17.530 I have thirty, thirty seconds left. So. 00:39:18.840 --> 00:39:24.270 Obviously there's more and more and more topics about 00:39:24.270 --> 00:39:29.220 Rails internals, but unfortunately I had, I just had 00:39:29.220 --> 00:39:32.090 forty minutes. Forty minutes was not enough to talk 00:39:32.090 --> 00:39:37.740 about, like, ActiveRecord internals or helpers or ActionPack, ActionController 00:39:37.740 --> 00:39:40.940 - these things. 00:39:40.940 --> 00:39:44.920 So, this is the end of my talk. But 00:39:44.920 --> 00:39:49.930 I'm kind of planning to write a book titled 00:39:49.930 --> 00:39:56.930 Ruby on Rails Hacking Guide. So, so hopefully. I'm 00:39:57.580 --> 00:39:59.810 planning to write it in Japanese first and then 00:39:59.810 --> 00:40:03.760 translate it into English later. So, hopefully can find 00:40:03.760 --> 00:40:07.330 it, like, next year, maybe. 00:40:07.330 --> 00:40:12.900 And, yeah. I'll write everything in that book. So 00:40:12.900 --> 00:40:19.900 please wait until the book is coming. Thank you 00:40:19.900 --> 00:40:21.140 very much.