Railtie: How to access initializer and lib boot hooks?

I am developing a stone for my Rails application that loads into it through Railtie. I basically embed models, plus libraries and several initializers, in the old style of the Rails application. My main problem is not knowing exactly in the whole logic of loading Rails applications where I am best off putting them in. My requirement for this is: gem initializers must be loaded before application initializers, the same with libraries, and initializers access the lib information. In the Rails application workflow, this somehow works. My short-term solution was this:

module Gemname def self.initialize_railtie ActiveSupport.on_load :active_record do require 'gemname/lib' require 'gemname/initializers' end end class Railtie < Rails::Railtie initializer 'gemname.insert_into_app' do Gemfile.initialize_railtie end end 

Thus, I am sure that libs are loaded before initializers. I'm just sure that there is a better way to do this, namely, to access some railtie hook that allows me to load my libraries using application libraries and initializers using application initializers. I just can't find them.

+4
source share
1 answer

I think you want config.after_initialize . According to here :

The last custom block to run. Called after framework initialization.

and here :

after_initialize: starts immediately after application initialization, but before application initializers start.

So you will have:

 module Gemname class MyCoolRailtie < ::Rails::Railtie config.after_initialize do require 'gemname/lib' require 'gemname/initializers' end end end 
+11
source

Source: https://habr.com/ru/post/1438429/


All Articles