Currently under development!
•
14 March 2024
•
6 mins
If you have ever worked with Rails, you would have seen the application file. Located in the config folder (/config/application.rb). This file is where we register rails gems that need to load some code on rails initialization. These files are called Railties
In a scenario where you want to load some code during rails initialization, you can consider writing your own railtie. You can use this article as a guide.
When we see this file, we add our railties there and be done with it. But, how about we ask, how is it possible that Rails can run the code with just requires to class definitions? Where is Rails initializing these classes? Is Rails checking this specific folder?
Let’s try to answer these questions and have clarity.
The genesis of answering this question is from the environment file. Which is also located in the config folder (/config/environment.rb). This file calls the application file and initialize! function.
The first line calls the file which registers all the railties. The file that made us interested in this article.
The second line is the start of the rabbit hole. this line calls the initialize! method defined in Rails source code. It is defined in the Application class which our application inherits from. the source.
As you can see, intialize! method in turn calls the run_initializers. this method is in a module file in Rails. it is accessible to this call because the superclass (Rails::Railtie) included the module in its definition. the source.
Moving forward. As you will notice in the earlier block of code, I added another method initializers. I added that because it caused me a whole lot of confusion. Let me explain.
In the method run_initializers, we call initializers. You would think that the method I added was what is being called, but it is not. Ideally, you would be correct but, Application class has overridden this method. So the other method was what was being called. the source.
This caused me a lot of frustration. This method then calls the railties_initializers method. the source.
railties_initializers method calls ordered_railties method. the source.
Following the pattern, ordered_railties then calls railties method. the call is on the line 13 (all = (railties — order)). the source.
At last, we meet the real culprit. Railties.new. This code is initializing a class. Let see the class. the source.
From the class, you can see in the initialize method, that Rails is calling the subclasses method of Rails::Railtie. Which, if you remember is a pre-requisite of creating a railtie. the subclasses method will return all the classes we registered. Instantiate it by calling the instance method on it and then rails run the codes you add on it.
NB: The subclasses method is defined in rails (ActiveSupport). But as of ruby 3.1, ruby have added subclasses to classes. Another article to dig into that.
It’s time we climb out of the rabit hole. We can see that rails is using the base class to fetch all the subclasses and then initialize them and run code in them. So no, rails is not checking a specific folder.
Thanks for your time and happy coding.