Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How are rails initializers defined in ruby on rails?


Asked by Iliana Dudley on Dec 11, 2021 Ruby



The bootstrap initializers prepare the application (like initializing the logger) while the finisher initializers (like building the middleware stack) are run last. The railtie initializers are the initializers which have been defined on the Rails::Application itself and are run between the bootstrap and finishers.
In this manner,
The Railtie initializers are run through the run_initializers method which is defined in railties/lib/rails/initializable.rb: The run_initializers code itself is tricky. What Rails is doing here is traversing all the class ancestors looking for those that respond to an initializers method.
And, Rails::Application is responsible for executing all railties and engines initializers. It also executes some bootstrap initializers (check Rails::Application::Bootstrap) and finishing initializers, after all the others are executed (check Rails::Application::Finisher ).
Thereof,
The gem takes advantage of Ruby’s ability to set environment variables as well as read them. The gem reads a config/application.yml file and sets environment variables before anything else is configured in the Rails application.
Also,
If we had used s rather than server, Rails would have used the aliases defined here to find the matching command. When one types a Rails command, invoke tries to lookup a command for the given namespace and executes the command if found. If Rails doesn't recognize the command, it hands the reins over to Rake to run a task of the same name.