1 00:00:18,560 --> 00:00:23,780 AKIRA MATSUDA: So. Welcome to Ruby on Rails internal talk. 2 00:00:24,449 --> 00:00:26,750 This is titled Ruby on Rails Hacking Guide, 3 00:00:26,750 --> 00:00:30,989 which is deeply inspired by Minero Alki's Legendary Ruby 4 00:00:30,989 --> 00:00:34,579 book titles Rails- for, Ruby Hacking Guide. 5 00:00:34,579 --> 00:00:38,719 So, before I get started, let me just introduce 6 00:00:38,719 --> 00:00:43,590 myself. I am Akira Matsuda - amatsuda on GitHub 7 00:00:43,590 --> 00:00:47,410 and Twitter. I'm a committer of Ruby and Ruby 8 00:00:47,410 --> 00:00:52,000 on Rails projects and I'm from Japan. 9 00:00:52,000 --> 00:00:55,579 As a individual on GitHub, I created a pagination 10 00:00:55,579 --> 00:01:01,489 library called kaminari and active_decoractor, action_args and et cetera, 11 00:01:01,489 --> 00:01:04,599 et cetera. Such libraries. 12 00:01:04,599 --> 00:01:08,149 As a Rails committer, ranked in here, in the 13 00:01:08,149 --> 00:01:15,149 bottom, like, twenty-seventh. I've done 374 commits so far 14 00:01:15,310 --> 00:01:19,600 and, since I made my first commit about six 15 00:01:19,600 --> 00:01:20,939 years ago. 16 00:01:20,939 --> 00:01:23,509 I initially titled this talk as Ruby on Rails 17 00:01:23,509 --> 00:01:29,429 Hacking Guide, but I slightly changed my mind after 18 00:01:29,429 --> 00:01:34,109 I listened to DHH's keynote. This talk should be 19 00:01:34,109 --> 00:01:38,549 titled like Ruby on Rails Reading Guide. Since we're 20 00:01:38,549 --> 00:01:42,209 actually gonna spend the whole forty minutes just for 21 00:01:42,209 --> 00:01:46,249 reading the whole framework code. 22 00:01:46,249 --> 00:01:50,259 And, not gonna talk anything about, like, science. But 23 00:01:50,259 --> 00:01:57,119 it's like, reading a novel, reading a story. So, 24 00:01:57,119 --> 00:01:59,489 why should we, why should we read Rails? I 25 00:01:59,489 --> 00:02:04,179 think we have three big reasons here. 26 00:02:04,179 --> 00:02:11,179 Firstly, as DHH defined, we're software writers, and you 27 00:02:11,700 --> 00:02:13,520 need to read a lot in order to be 28 00:02:13,520 --> 00:02:17,610 a good writer, right. This is obvious. So, we 29 00:02:17,610 --> 00:02:20,770 need to read Rails to be a good Rails 30 00:02:20,770 --> 00:02:27,200 programmer. And if you don't understand any piece of 31 00:02:27,200 --> 00:02:31,069 code in your application, that means you don't really 32 00:02:31,069 --> 00:02:34,140 understand your application. 33 00:02:34,140 --> 00:02:38,860 So, don't let a magical piece of code exist 34 00:02:38,860 --> 00:02:43,040 in your app. I know that Rails applications are 35 00:02:43,040 --> 00:02:50,040 filled with such magics like, for example, like this. 36 00:02:50,680 --> 00:02:55,510 It's such simple, just one line of Ruby code, 37 00:02:55,510 --> 00:02:58,620 in just one method. But it, it really does 38 00:02:58,620 --> 00:03:03,000 a lot of things for you, right. But we 39 00:03:03,000 --> 00:03:06,109 need to be able to explain why, why is 40 00:03:06,109 --> 00:03:13,109 this controller inheriting from ApplicationController inheriting from ActionController::Base? We. 41 00:03:13,489 --> 00:03:18,129 Or, how the http request actually calls this in 42 00:03:18,129 --> 00:03:22,650 index Ruby method. Or why are we setting this 43 00:03:22,650 --> 00:03:28,269 u- users relation into a controller instance variable. Et 44 00:03:28,269 --> 00:03:30,790 cetera, et cetera. 45 00:03:30,790 --> 00:03:36,219 Or, think about this example. This, this, this is 46 00:03:36,219 --> 00:03:41,709 a typical Rails-ish DSL. This reads good. It's pretty 47 00:03:41,709 --> 00:03:46,950 straight forward for readers, as a documentation. But, you're 48 00:03:46,950 --> 00:03:51,230 not an application reader, right. We, we're application writer. 49 00:03:51,230 --> 00:03:54,879 So you have to write this. And as a 50 00:03:54,879 --> 00:03:57,840 writer, you don't, don't usually write when you don't 51 00:03:57,840 --> 00:04:02,200 understand what it means, right. 52 00:04:02,200 --> 00:04:07,400 So, so a question. What, what, what is this 53 00:04:07,400 --> 00:04:14,400 Rails.application.routes.draw, or get in the second line. Looks like 54 00:04:15,219 --> 00:04:21,029 a method call taking a hash parameter. What is 55 00:04:21,029 --> 00:04:26,430 it doing? Who actually knows, who here actually knows 56 00:04:26,430 --> 00:04:30,430 what's gonna happen with this code? Can explain? 57 00:04:30,430 --> 00:04:37,430 Right. So, if you think, if you think you 58 00:04:41,870 --> 00:04:45,970 can explain how this define a route, if, if 59 00:04:45,970 --> 00:04:50,319 you cannot explain. Sorry. If you cannot explain that 60 00:04:50,319 --> 00:04:54,430 means for, it's just a magic. And that's why 61 00:04:54,430 --> 00:04:58,960 you're, you're in this room now, right. So, you'll 62 00:04:58,960 --> 00:05:03,370 soon get to know what the get means. 63 00:05:03,370 --> 00:05:08,400 And as the third reason to read Rails code 64 00:05:08,400 --> 00:05:15,400 is because Rails.has_many :bugs. So, as I told you, 65 00:05:16,569 --> 00:05:19,050 I patched a lot. And mostly I, I patch 66 00:05:19,050 --> 00:05:23,190 Rails because I hit bugs in my production application. 67 00:05:23,190 --> 00:05:29,139 And I guess, you all have the same experience. 68 00:05:29,139 --> 00:05:34,280 I'm sure. Rails is basically buggy. And we need 69 00:05:34,280 --> 00:05:37,319 to deal with them. With the bugs. 70 00:05:37,319 --> 00:05:41,150 So, naturally, Rails programmer job is not just to 71 00:05:41,150 --> 00:05:45,349 maintain the application, but you also need to maintain 72 00:05:45,349 --> 00:05:49,080 Rails framework as well. It's our job. And it's 73 00:05:49,080 --> 00:05:51,280 your job. So we need to be familiar with 74 00:05:51,280 --> 00:05:55,250 the code base. 75 00:05:55,250 --> 00:05:59,710 And strangely, this talk is kind of categorized as 76 00:05:59,710 --> 00:06:06,660 a novice talk. But, I'm not sure. I'm, I'm 77 00:06:06,660 --> 00:06:13,660 gonna talk about the Rails internal. So. I hope 78 00:06:14,110 --> 00:06:15,509 you enjoy. 79 00:06:15,509 --> 00:06:22,509 Let's begin. This is what I'm gonna talk. So, 80 00:06:25,889 --> 00:06:29,470 first of all, Ruby on Rails is a open-source 81 00:06:29,470 --> 00:06:36,470 software on GitHub. There's no C-extension inside. So. I 82 00:06:36,639 --> 00:06:42,330 mean, there's, there's no C-code. 100% Ruby. That means, 83 00:06:42,330 --> 00:06:44,940 if you're a Ruby programmer, you just can read 84 00:06:44,940 --> 00:06:51,800 it and, and you can write it if necessary. 85 00:06:51,800 --> 00:06:54,750 You can get its source code here, by git 86 00:06:54,750 --> 00:07:01,750 cloning from GitHub. By the way, here's a tiny 87 00:07:02,319 --> 00:07:09,319 tip. How you can find the GitHub repository for 88 00:07:10,240 --> 00:07:14,919 the gem you use. You can use, you can 89 00:07:14,919 --> 00:07:20,090 use a gem named gem-src. It's on, it's here 90 00:07:20,090 --> 00:07:24,759 on GitHub. And please take a look at this 91 00:07:24,759 --> 00:07:27,970 repository. This is how you can install it and 92 00:07:27,970 --> 00:07:29,599 how you can use it. 93 00:07:29,599 --> 00:07:35,139 I'm not gonna explain here. But. So, what this 94 00:07:35,139 --> 00:07:40,569 does is after this gem installed, it will automatically 95 00:07:40,569 --> 00:07:45,560 git, git clone this source from GitHub repository. So, 96 00:07:45,560 --> 00:07:49,879 you, you need not to memorize the author's name 97 00:07:49,879 --> 00:07:52,330 or you can not, you don't need to search 98 00:07:52,330 --> 00:07:58,629 on GitHub. As a gem install, it automatically gets 99 00:07:58,629 --> 00:08:01,699 the code from GitHub. It's so useful for social 100 00:08:01,699 --> 00:08:07,169 coders. 101 00:08:07,169 --> 00:08:14,169 Anyway. Let's go back to the Rails source. Firstly, 102 00:08:17,539 --> 00:08:21,319 let's see what's included in the gem. To know 103 00:08:21,319 --> 00:08:24,250 what's included in gem, take a look at the 104 00:08:24,250 --> 00:08:31,250 gemspec. So this is taken from the file rails.gemspec. 105 00:08:32,299 --> 00:08:36,919 Here it defines the files included. It looks like 106 00:08:36,919 --> 00:08:42,729 it doesn't really include any program. It's just documents, 107 00:08:42,729 --> 00:08:45,000 right. 108 00:08:45,000 --> 00:08:49,110 And you can find these declarations. A bunch of 109 00:08:49,110 --> 00:08:56,110 add_dependencies. So that means Rails has no code inside, 110 00:08:58,060 --> 00:09:03,760 but instead if defines several dependencies. It means that 111 00:09:03,760 --> 00:09:06,750 this is a meta, meta-gem, meta-package, to install these 112 00:09:06,750 --> 00:09:12,210 seven gems, right. 113 00:09:12,210 --> 00:09:19,210 And these seven gems are in the Rails repository. 114 00:09:21,390 --> 00:09:25,550 And Rails is a full-stack framework, a full-stack MVC 115 00:09:25,550 --> 00:09:28,410 framework. And all these M and V and C 116 00:09:28,410 --> 00:09:34,850 are included in this one repository. So we have 117 00:09:34,850 --> 00:09:39,130 model, view, and controller. 118 00:09:39,130 --> 00:09:42,070 And we have these gems. I'm sorry it's, can 119 00:09:42,070 --> 00:09:44,800 you read it? I'm sorry it's hard to read. 120 00:09:44,800 --> 00:09:51,800 Anyway. We have ActiveRecord, ActionPack and ActionView. And this 121 00:09:52,380 --> 00:09:59,380 is something called model, and these are views, and 122 00:09:59,390 --> 00:10:02,850 these are controllers. 123 00:10:02,850 --> 00:10:07,100 So, Rails gem is a meta-package that depends on 124 00:10:07,100 --> 00:10:13,890 these seven gems. 125 00:10:13,890 --> 00:10:16,510 So let's take a look at the Rails server 126 00:10:16,510 --> 00:10:23,170 command and, and let me explain how, how the 127 00:10:23,170 --> 00:10:27,420 Rails actually boots. For, starting up the Rails server, 128 00:10:27,420 --> 00:10:31,010 you just need to run Rails server command. So, 129 00:10:31,010 --> 00:10:34,720 what's happening internally when you execute this command? 130 00:10:34,720 --> 00:10:39,330 When I hit the Rails command it actually executes 131 00:10:39,330 --> 00:10:43,820 bin slash rails in your application. So, let's read 132 00:10:43,820 --> 00:10:48,530 that file first. That file should look like this. 133 00:10:48,530 --> 00:10:52,510 It requires config/boot inside your application first, and then 134 00:10:52,510 --> 00:10:55,040 requires rails/commands. 135 00:10:55,040 --> 00:11:01,210 Rails/boot looks like this. It just bundles something. I'm 136 00:11:01,210 --> 00:11:04,260 gonna skip the bundler part, because it's, it's out 137 00:11:04,260 --> 00:11:08,910 of today's scope, but it, it, it kind of 138 00:11:08,910 --> 00:11:12,200 tweaks the $LOAD_PATH nicely. 139 00:11:12,200 --> 00:11:19,200 And so the second line. Rails/commands. It requires rails/commands. 140 00:11:24,880 --> 00:11:29,240 So where is "rails" directory? You can find "rails" 141 00:11:29,240 --> 00:11:34,740 directory in the railties gem. There's a gem called 142 00:11:34,740 --> 00:11:40,460 railties, which I, I didn't explain. So, next chapter 143 00:11:40,460 --> 00:11:42,090 is about railties. 144 00:11:42,090 --> 00:11:49,090 What, what actually railties is? Railties is a very 145 00:11:49,870 --> 00:11:56,570 wisely-crafted tiny library, initially created by Yehuda Katz and 146 00:11:56,570 --> 00:12:02,260 Carl, Carl Lerche. So let's dive into it. 147 00:12:02,260 --> 00:12:09,260 Here's the gemspec. The gemspec says this is about 148 00:12:10,630 --> 00:12:17,130 Rails internals. And the gemspec shows that now this 149 00:12:17,130 --> 00:12:24,130 gem includes some libraries. Ruby files. Actually, it includes 150 00:12:25,920 --> 00:12:30,880 like this. So, when you step inside the railties 151 00:12:30,880 --> 00:12:36,060 lib directory, you'll see these. One directory and one 152 00:12:36,060 --> 00:12:41,690 file called rails dot rb. 153 00:12:41,690 --> 00:12:47,080 So these's the rails directory. And this is how 154 00:12:47,080 --> 00:12:51,990 Rails/commands dot rb looks like in the railties gem. 155 00:12:51,990 --> 00:12:58,640 It requires commands_tasks, and then invokes a CommandTask with 156 00:12:58,640 --> 00:13:05,640 a given command name. ARGV, right. 157 00:13:07,520 --> 00:13:14,520 In this case, so, in this case server, right. 158 00:13:14,810 --> 00:13:18,060 So this is the file named commands_tasks dot rb. 159 00:13:18,060 --> 00:13:21,950 It requires rails/commands/server first. So let's take a look 160 00:13:21,950 --> 00:13:28,950 at rails/commands/server. And this is rails/commands/server. It requires, at 161 00:13:32,020 --> 00:13:39,020 very top, it requires rails. So, then jump to 162 00:13:40,480 --> 00:13:43,810 rails dot rb. Rails dot rb was at the 163 00:13:43,810 --> 00:13:48,450 very top of the repository, right. 164 00:13:48,450 --> 00:13:51,580 So this is how rails dot rb looks like. 165 00:13:51,580 --> 00:13:56,770 It requires rails/application. So, let's go to rails/application. And 166 00:13:56,770 --> 00:14:01,830 it defines a top-level module named Rails. 167 00:14:01,830 --> 00:14:08,830 So rails/application, it requires rails/engine, and it defines a 168 00:14:09,130 --> 00:14:14,070 Rails Application class, inheriting Rails Engine. 169 00:14:14,070 --> 00:14:21,070 So, jump to rails/engine. Now, rails/engine requires rails/railtie and 170 00:14:23,190 --> 00:14:28,170 it, Rails Engine class inherits from Rails Railtie. So, 171 00:14:28,170 --> 00:14:31,360 next file is rails/railtie. 172 00:14:31,360 --> 00:14:38,080 Railtie, it looks like this. It requires rails/initializable and 173 00:14:38,080 --> 00:14:41,510 rails/configuration. And defines Rails Railtie class, that includes Rails 174 00:14:41,510 --> 00:14:48,510 Initializable. So next follow the, next file is rails/initializable. 175 00:14:48,610 --> 00:14:55,610 It defines a Rails Initizable- Initializer. 176 00:14:58,980 --> 00:15:05,080 So, what we learned so far is, railties defines 177 00:15:05,080 --> 00:15:12,080 these core classes, like Application, Engine, Railtie, Initializable. Which 178 00:15:16,520 --> 00:15:23,520 are very, very important. Actually, the core of Rails. 179 00:15:24,410 --> 00:15:28,870 So railties is like this. It's the core of 180 00:15:28,870 --> 00:15:32,200 Rails, in the middle. 181 00:15:32,200 --> 00:15:34,970 And we had something called Plugin as well, but 182 00:15:34,970 --> 00:15:38,570 it was gone since Rails 4. 183 00:15:38,570 --> 00:15:45,570 So, let's skip it. So railties is a library 184 00:15:48,910 --> 00:15:55,910 providing something underneath the Rails Application. And I have 185 00:15:57,180 --> 00:16:04,180 another answer for you. Railties is, according to Wikipedia, 186 00:16:08,410 --> 00:16:13,270 this is the definition of something called Railroad tie. 187 00:16:13,270 --> 00:16:20,270 It's a rectangular piece of wood which is laid 188 00:16:21,520 --> 00:16:28,520 underneath the rails, sometimes called sleeper, railway sleeper. 189 00:16:29,070 --> 00:16:35,830 So this is rail ties. Consider this. The applications 190 00:16:35,830 --> 00:16:42,200 are the trains that run on Rails, and railties, 191 00:16:42,200 --> 00:16:46,420 Rails is the framework, and railties is something lies 192 00:16:46,420 --> 00:16:50,510 underneath the Rails framework. This is why they named 193 00:16:50,510 --> 00:16:55,450 it railties, I guess. This is a beautiful metaphor, 194 00:16:55,450 --> 00:16:58,190 isn't it? 195 00:16:58,190 --> 00:17:01,500 So how does it work? Or, how can we 196 00:17:01,500 --> 00:17:05,939 use it? This is how, how, how it's work. 197 00:17:05,939 --> 00:17:10,169 We can git grep the whole railtie repository, and 198 00:17:10,169 --> 00:17:15,900 you can find some uses of railtie. These are 199 00:17:15,900 --> 00:17:20,939 in, not only in railties, they're actually, these are 200 00:17:20,939 --> 00:17:25,339 in mailers, ActiveRecord, ActionPack, blah, blah, blah. 201 00:17:25,339 --> 00:17:30,379 We're gonna take it. So, so let's take a, 202 00:17:30,379 --> 00:17:34,820 take a look at it, like this. So, each 203 00:17:34,820 --> 00:17:39,159 component has its own railtie, inheriting from Rails::Railtie, like 204 00:17:39,159 --> 00:17:43,250 this. This is how we use railtie. 205 00:17:43,250 --> 00:17:48,690 So, railtie is not only in the core, but 206 00:17:48,690 --> 00:17:55,690 in, we can find railties everywhere. Railtie. It's inside 207 00:17:55,690 --> 00:18:00,779 each component, right. And, so, this is what's written 208 00:18:00,779 --> 00:18:03,580 inside the railtie class. 209 00:18:03,580 --> 00:18:06,179 It has some, mainly we, we see two kinds 210 00:18:06,179 --> 00:18:13,179 of class method calls, config and initializer, right. These 211 00:18:14,429 --> 00:18:21,429 are class method calls to railtie class. So, what's 212 00:18:22,799 --> 00:18:24,299 config? 213 00:18:24,299 --> 00:18:31,299 This is the definition. Railtie config create something. Instantiates 214 00:18:31,679 --> 00:18:38,679 something like Railtie::Configuration. And so config is a instance 215 00:18:41,639 --> 00:18:47,070 of Railtie::Configuration. Railtie::Configuration is something like this. It, it 216 00:18:47,070 --> 00:18:52,220 has one method_missing method. And it accepts any method 217 00:18:52,220 --> 00:18:55,860 call and just stores the given name and arguments 218 00:18:55,860 --> 00:19:02,570 and block into a class-level hash. 219 00:19:02,570 --> 00:19:09,570 So, what is initializer? Initializer looks like this. The, 220 00:19:10,590 --> 00:19:15,600 it just keeps the given block as a proc 221 00:19:15,600 --> 00:19:22,600 instance, with a name, with a given name, right. 222 00:19:26,389 --> 00:19:32,610 So, this is everything. This is what railties is 223 00:19:32,610 --> 00:19:38,019 doing. It, it sets something, some values via config 224 00:19:38,019 --> 00:19:42,950 method call and initializer method call. And to, to 225 00:19:42,950 --> 00:19:49,950 use them for the later use. 226 00:19:51,299 --> 00:19:54,370 And railties is actually class, but it's prohibited to 227 00:19:54,370 --> 00:19:59,610 instantiate. So, you cannot make a instance of the 228 00:19:59,610 --> 00:20:06,610 railtie class. Instead, you just can inherit and, when 229 00:20:07,809 --> 00:20:13,960 inherited, it, it kind of memorizes the, the children 230 00:20:13,960 --> 00:20:17,889 class names. 231 00:20:17,889 --> 00:20:24,759 So, this is a summary what, of what railties 232 00:20:24,759 --> 00:20:27,759 is doing. Railties is a core of Rails that 233 00:20:27,759 --> 00:20:30,779 provides Railtie, Engine, Application, et cetera, and we can 234 00:20:30,779 --> 00:20:34,870 find Railtie in other Rails components like ActiveRecord, ActionPack. 235 00:20:34,870 --> 00:20:38,580 And a railtie can keep config and initializer values. 236 00:20:38,580 --> 00:20:43,499 And Railtie ties up these railties, all right. 237 00:20:43,499 --> 00:20:47,009 So, let's see how, how actually Rails server ties 238 00:20:47,009 --> 00:20:53,999 these up. And how, how Rails server boots. So, 239 00:20:53,999 --> 00:20:58,169 let's go back to rails/commands/commands_tasks dot rb. After it 240 00:20:58,169 --> 00:21:03,929 requiring rails/commands/server, it executes, it requires the application and 241 00:21:03,929 --> 00:21:07,340 then executes server dot start. 242 00:21:07,340 --> 00:21:14,340 Server dot start is defined here, because it was 243 00:21:15,909 --> 00:21:22,909 defined in Rails/commands/server. Server dot start calls, just calls 244 00:21:23,870 --> 00:21:28,470 super. And super is, super class of this class 245 00:21:28,470 --> 00:21:30,669 is Rack::Server. 246 00:21:30,669 --> 00:21:36,870 So, welcome to the Rack world. Now, now we 247 00:21:36,870 --> 00:21:43,870 need to read the Rack code. Oh my god. 248 00:21:48,330 --> 00:21:51,970 This is not Rack hacking guide, so I'm just 249 00:21:51,970 --> 00:21:58,970 gonna skip it. It. Anyway. Finally calls something like 250 00:22:04,019 --> 00:22:11,019 this, via rack/builder. It evaluates this Ruby string. Like, 251 00:22:16,610 --> 00:22:22,549 Rack::Builder dot new taking contents of config dot ru 252 00:22:22,549 --> 00:22:26,610 in your application. 253 00:22:26,610 --> 00:22:33,610 And Rack::Builder just instance_evals the given block. So, this 254 00:22:35,110 --> 00:22:42,110 is what's written inside your application's config dot ru. 255 00:22:43,940 --> 00:22:50,940 It requires config/environment dot rb first, and runs Rails.application. 256 00:22:52,299 --> 00:22:55,220 Run is also a Rack command, I mean, Rack 257 00:22:55,220 --> 00:22:58,379 method. 258 00:22:58,379 --> 00:23:05,379 So, I just, I just, like, explained very fast, 259 00:23:07,519 --> 00:23:11,730 like, how the method, method call goes. How method 260 00:23:11,730 --> 00:23:15,409 call graph looks like. So, I guess you find 261 00:23:15,409 --> 00:23:19,759 it difficult to follow, and how can it actually 262 00:23:19,759 --> 00:23:24,090 follow these method, method call graphs? 263 00:23:24,090 --> 00:23:30,929 Here's one tip. Just, puts caller from, in a 264 00:23:30,929 --> 00:23:35,019 file inside your application. For, example, in config dot 265 00:23:35,019 --> 00:23:39,919 ru file, like this. Just add a puts caller 266 00:23:39,919 --> 00:23:44,299 here. And run Rails server. Then, you'll get an 267 00:23:44,299 --> 00:23:50,870 output like this in your console. 268 00:23:50,870 --> 00:23:55,730 And these, these are basically what I explained. Like, 269 00:23:55,730 --> 00:24:02,730 we see commands/task and server and, like, Rack::Builder. 270 00:24:05,919 --> 00:24:12,210 So, this is very useful. Like, when you get 271 00:24:12,210 --> 00:24:17,940 lost in the Ruby, Ruby jungle, use puts caller 272 00:24:17,940 --> 00:24:22,869 in caller locations, and that'll show you the path 273 00:24:22,869 --> 00:24:25,690 to the place you are. 274 00:24:25,690 --> 00:24:32,690 All right. Anybody, but. The config dot ru file 275 00:24:33,759 --> 00:24:39,039 requires config/environment.rb, and this is, as you know, what's 276 00:24:39,039 --> 00:24:44,679 written inside the config/environment.rb. It requires config/application dot rb 277 00:24:44,679 --> 00:24:47,610 and calls Rails.application.initialize! 278 00:24:47,610 --> 00:24:54,610 So, Rails.application.initialize! is the very, very important method. As 279 00:24:57,809 --> 00:25:01,720 you may notice from, because it, it has an 280 00:25:01,720 --> 00:25:08,720 exclamation mark. This is what's written in application.rb. Since, 281 00:25:09,220 --> 00:25:13,529 Rails 4 point 1, it, it really has nothing 282 00:25:13,529 --> 00:25:16,970 in its, in its class. 283 00:25:16,970 --> 00:25:23,970 But, but it defines a class called Application, inheriting 284 00:25:24,360 --> 00:25:31,360 from Rails::Application. 285 00:25:33,499 --> 00:25:39,460 Before that, it requires rails/all and bundles all the 286 00:25:39,460 --> 00:25:45,330 bundled gems. So this is rails dot, rails/all dot 287 00:25:45,330 --> 00:25:51,759 rb. What it actually does is something like this. 288 00:25:51,759 --> 00:25:57,909 It defines, so, it requires all these railties that 289 00:25:57,909 --> 00:26:04,909 I showed you before. Railties defines configs and initializers, 290 00:26:06,330 --> 00:26:10,879 as we have seen already. 291 00:26:10,879 --> 00:26:17,879 And then, we call Rails.application.initialize exclamation. So, so we, 292 00:26:22,039 --> 00:26:27,929 we define initializers here and load all initializers and 293 00:26:27,929 --> 00:26:33,049 then initialize exclamation, right. 294 00:26:33,049 --> 00:26:40,049 So, this is the initialize method. It run_initializers, and 295 00:26:41,559 --> 00:26:48,559 run_initializers, it calls run, run method for, for each 296 00:26:53,059 --> 00:26:59,830 initialize- initializable object in the initializers collection. 297 00:26:59,830 --> 00:27:06,070 So initializable looks like this. Run just executes the 298 00:27:06,070 --> 00:27:13,070 given argument. I mean, instance_exec through stored proc inside 299 00:27:13,950 --> 00:27:20,950 the same context, right. I mean, in this case, 300 00:27:23,080 --> 00:27:27,119 at sign context is actually the application instance, so 301 00:27:27,119 --> 00:27:34,119 it just instance_execs the stored procs in the initializers. 302 00:27:34,460 --> 00:27:38,190 In the application's context. 303 00:27:38,190 --> 00:27:45,190 So, I said initializers collection. Initializers collection is this. 304 00:27:52,749 --> 00:27:59,749 And initializers, so, initializers, yeah. In rai- railties initializers 305 00:28:02,239 --> 00:28:07,860 was created like this. So, what, what's in the 306 00:28:07,860 --> 00:28:13,669 initializer's collection is instances of Rails initializer, initializable initializer. 307 00:28:13,669 --> 00:28:20,320 Which, each initializer just, just holds a proc instance, 308 00:28:20,320 --> 00:28:24,159 right. 309 00:28:24,159 --> 00:28:27,269 And as we've seen already these are called from 310 00:28:27,269 --> 00:28:33,129 each Railtie classes. 311 00:28:33,129 --> 00:28:40,129 So. This is, this is what initialize do. And, 312 00:28:41,129 --> 00:28:45,659 so let's go back to the server. As we've 313 00:28:45,659 --> 00:28:52,659 seen, the start method calls super. I'm sorry. I'll 314 00:28:52,730 --> 00:28:54,440 skip this. 315 00:28:54,440 --> 00:29:01,440 So. Summary. So, this is what we, what the 316 00:29:02,830 --> 00:29:08,210 railtie does for booting up the Rails server. Finally, 317 00:29:08,210 --> 00:29:13,669 it runs Rails::Application in config dot ru. Do you 318 00:29:13,669 --> 00:29:17,610 remember? It's written in config dot ru file. Run. 319 00:29:17,610 --> 00:29:20,340 Run the application. 320 00:29:20,340 --> 00:29:26,539 So what is run? What is run method? It's 321 00:29:26,539 --> 00:29:31,499 defined like this. And with this, with this definition, 322 00:29:31,499 --> 00:29:38,499 the Rack server calls Rails.application dot call. Rails.application responds 323 00:29:41,129 --> 00:29:44,789 to call. So you can call this. That means 324 00:29:44,789 --> 00:29:47,850 Rails.application is a Rack application. 325 00:29:47,850 --> 00:29:54,489 And Rails.application as a Rack application, it, it has 326 00:29:54,489 --> 00:29:58,759 a call method like this. It calls super. The 327 00:29:58,759 --> 00:30:05,759 super, the Rails.application super class was Rails::Engine. So, so 328 00:30:07,429 --> 00:30:08,999 jump to Rails::Engine. 329 00:30:08,999 --> 00:30:15,999 Rails::Engine has this like call method. OK. The call 330 00:30:16,080 --> 00:30:22,769 goes to app dot call. App is something like 331 00:30:22,769 --> 00:30:28,460 this. It's a config middleware that contains default middleware 332 00:30:28,460 --> 00:30:35,460 stack, and the endpoint, something called endpoint. Look at 333 00:30:36,039 --> 00:30:38,049 this. 334 00:30:38,049 --> 00:30:44,330 So, what's the end point? End point is a 335 00:30:44,330 --> 00:30:51,330 route. Route is a ActionDispatch::Routing::RouteSet by default. OK. 336 00:30:56,169 --> 00:31:03,169 So. So, the next thing is routes. Route is 337 00:31:06,989 --> 00:31:13,989 usually defined in config/routes dot rb like this. So, 338 00:31:14,379 --> 00:31:21,289 let's think of this simple route. Routes, Rails.application.routes is, 339 00:31:21,289 --> 00:31:28,289 is ActionDispatch::Routing::RouteSet. It's defined here in ActionDispatch gem, I'm 340 00:31:29,159 --> 00:31:33,749 sorry. ActionPack gem in action_dispatch directory. 341 00:31:33,749 --> 00:31:38,470 It, it also responds to call, as it's a 342 00:31:38,470 --> 00:31:45,470 Rack application. And call goes to router dot call. 343 00:31:47,460 --> 00:31:54,460 Router is defined like this. Journey::Router.new. So, so router 344 00:31:54,690 --> 00:31:57,059 is a Journey thing. 345 00:31:57,059 --> 00:32:04,059 Then, so Rails.application routes is also a Rack application 346 00:32:14,080 --> 00:32:17,989 that defines endpoint, and it finally goes to the 347 00:32:17,989 --> 00:32:20,679 Journey thing. But for now let's go back to 348 00:32:20,679 --> 00:32:25,590 the Routes.draw DSL. 349 00:32:25,590 --> 00:32:28,340 Here. 350 00:32:28,340 --> 00:32:35,340 So, routes.draw is defined like this. It just evaluates 351 00:32:37,059 --> 00:32:44,059 the given block. In the ActionDispatch routing mapper. So, 352 00:32:47,570 --> 00:32:54,519 this DSL actually calls ActionDispatch::Routing::Mapper#get method, right. And get 353 00:32:54,519 --> 00:32:58,799 method is like this. It calls map_method. map_method calls 354 00:32:58,799 --> 00:33:05,600 match. match calls decomposed_match, and decomposed_match calls add_route. 355 00:33:05,600 --> 00:33:12,600 So, anyway. It finally goes to Mapper#add_route. And Mapper 356 00:33:14,190 --> 00:33:21,190 add_route calls Mapping dot new route. And add_route returns 357 00:33:24,330 --> 00:33:31,330 a, finally, it returns a Journey route. And then, 358 00:33:31,879 --> 00:33:37,789 like, adds the given, sorry, the traded route into 359 00:33:37,789 --> 00:33:42,649 the collection. Journey::Routes collection. 360 00:33:42,649 --> 00:33:49,649 So. So, this is what the Journey does. And, 361 00:33:51,059 --> 00:33:58,059 what's this? Yeah. It finally calls Mapper to_route, Mapping#to_route, 362 00:33:58,609 --> 00:34:02,340 so to_route is defined like this. 363 00:34:02,340 --> 00:34:09,340 I'm just gonna skip this. It's, it just returns 364 00:34:11,379 --> 00:34:16,219 the given app parameter, which was the endpoint given 365 00:34:16,219 --> 00:34:21,239 into the methods parameter. So it finally, finally it, 366 00:34:21,239 --> 00:34:28,010 it returns endpoint. And endpoint is if, if it 367 00:34:28,010 --> 00:34:32,679 responds_to call, just returns that. And if not, call 368 00:34:32,679 --> 00:34:34,500 Dispatcher. 369 00:34:34,500 --> 00:34:41,500 So. So the, the. The hash value can, could 370 00:34:45,239 --> 00:34:49,750 be respond_to call, for example, like this. Or mounted 371 00:34:49,750 --> 00:34:53,659 engine, for example. But usually it doesn't respond to 372 00:34:53,659 --> 00:35:00,660 call, so it calls dispatcher. Dispatcher is a Routing::RouteSet::Dispatcher. 373 00:35:04,940 --> 00:35:08,080 And Dispatcher is also a Rack app, so it 374 00:35:08,080 --> 00:35:12,190 can be called. And the call the Dispatcher goes 375 00:35:12,190 --> 00:35:16,900 to, dispatch method. 376 00:35:16,900 --> 00:35:23,900 Dispatch, so, it finally goes at the bottom line. 377 00:35:25,030 --> 00:35:28,470 It finally goes to controller dot action with a 378 00:35:28,470 --> 00:35:35,470 given action name, dot call. So, so finally, what, 379 00:35:36,970 --> 00:35:42,730 what the router finally calls is this. FooController dot 380 00:35:42,730 --> 00:35:49,730 action method with a given action, action name. And 381 00:35:50,280 --> 00:35:52,830 it returns something callable. 382 00:35:52,830 --> 00:35:58,790 So, this is the route DSL. get "/hello" => 383 00:35:58,790 --> 00:36:05,790 "foo#bar" maps to FooController dot action('bar'). 384 00:36:10,760 --> 00:36:15,090 This is about Journey. And I think I have 385 00:36:15,090 --> 00:36:17,150 no time, so I'm gonna skip this. I'm gonna 386 00:36:17,150 --> 00:36:22,200 skip this long Journey. Anyway, it resolves the route. 387 00:36:22,200 --> 00:36:26,610 So, this is a summary. The, the main point 388 00:36:26,610 --> 00:36:29,800 is that 'foo#bar' becomes a Rack application generated by 389 00:36:29,800 --> 00:36:33,740 FooController dot action('bar'). 390 00:36:33,740 --> 00:36:39,870 So, the final chapter. Controllers and actions. This, what's 391 00:36:39,870 --> 00:36:42,770 gonna happen when the server got a request? It, 392 00:36:42,770 --> 00:36:49,770 it, it just calls this. So what is this? 393 00:36:50,500 --> 00:36:57,500 Firstly, the action method is defined like this. It's, 394 00:37:00,730 --> 00:37:06,140 it calls the new dot dispatch inside the block. 395 00:37:06,140 --> 00:37:09,260 New, new means Controller dot new. 396 00:37:09,260 --> 00:37:16,260 So, dispatch, dispatch method creates set, call set_response! and 397 00:37:16,430 --> 00:37:20,310 then calls super. So, what this dispatch method does 398 00:37:20,310 --> 00:37:27,310 is creates Rails response object. And then dispatch method, 399 00:37:29,720 --> 00:37:34,320 then creates request object. Whoops. 400 00:37:34,320 --> 00:37:39,990 All right. It sets the request object into the 401 00:37:39,990 --> 00:37:46,990 instance. And then calls process method. Dispatch finally calls 402 00:37:47,730 --> 00:37:53,940 ActionController::Metal#process. So, this is what's written inside the process 403 00:37:53,940 --> 00:37:58,590 method. process method calls process_action, and process_action calls send_action, 404 00:37:58,590 --> 00:38:05,590 and send_action is aliased to send. Ruby's send method, 405 00:38:07,140 --> 00:38:09,130 right. 406 00:38:09,130 --> 00:38:16,130 So, FooController dot action('bar') creates the Rails Request and 407 00:38:17,140 --> 00:38:21,780 Response objects into the controller instance. Then, it just 408 00:38:21,780 --> 00:38:25,370 calls, calls the method name bar via Ruby send 409 00:38:25,370 --> 00:38:28,810 method, OK. 410 00:38:28,810 --> 00:38:34,240 So, it can be like, FooController dot new, with 411 00:38:34,240 --> 00:38:38,400 something, dot bar. It's a very simple Ruby method 412 00:38:38,400 --> 00:38:43,080 call, finally. So, this is what's happening inside the 413 00:38:43,080 --> 00:38:50,080 HTTP request. When the Rails server accepts the HTTP 414 00:38:51,980 --> 00:38:57,360 request, it, it kind of, the Journey kind of, 415 00:38:57,360 --> 00:39:02,060 somehow, resolves the results, and finally calls this simple 416 00:39:02,060 --> 00:39:06,430 Ruby method. OK? 417 00:39:06,430 --> 00:39:10,530 And I guess it's, yeah. I guess it's time. 418 00:39:10,530 --> 00:39:17,530 I have thirty, thirty seconds left. So. 419 00:39:18,840 --> 00:39:24,270 Obviously there's more and more and more topics about 420 00:39:24,270 --> 00:39:29,220 Rails internals, but unfortunately I had, I just had 421 00:39:29,220 --> 00:39:32,090 forty minutes. Forty minutes was not enough to talk 422 00:39:32,090 --> 00:39:37,740 about, like, ActiveRecord internals or helpers or ActionPack, ActionController 423 00:39:37,740 --> 00:39:40,940 - these things. 424 00:39:40,940 --> 00:39:44,920 So, this is the end of my talk. But 425 00:39:44,920 --> 00:39:49,930 I'm kind of planning to write a book titled 426 00:39:49,930 --> 00:39:56,930 Ruby on Rails Hacking Guide. So, so hopefully. I'm 427 00:39:57,580 --> 00:39:59,810 planning to write it in Japanese first and then 428 00:39:59,810 --> 00:40:03,760 translate it into English later. So, hopefully can find 429 00:40:03,760 --> 00:40:07,330 it, like, next year, maybe. 430 00:40:07,330 --> 00:40:12,900 And, yeah. I'll write everything in that book. So 431 00:40:12,900 --> 00:40:19,900 please wait until the book is coming. Thank you 432 00:40:19,900 --> 00:40:21,140 very much.