1

I was setting some global variables (for setting devise gem authorization customization). So, I put some file auth_provider.rb in the config/auth folder where I declare some module MyAuth (in some myauth.rb file). The problem is that when this module is called by some file in the initializers folder, call it caller.rb, so that I had to put in the application.rb file

config.before_initialize do
      Dir["#{config.root}/config/auth/*.rb"].each {|file|
        require file
      }
end

But I don't find it really natural. And, if I put auth_provider.rb in the initializers/auth folder, then caller.rb seems to be loaded before auth_provider.rb, so I get some namespace error (I presume the files are loaded by alphabetical order). So how, to load the auth_provider.rb before caller.rb in a more straightforward way ? Should I create some initializers/0_auth folder in order to be loaded in first ? (By the way, i want this configuration modular and to keep it clearly seperated from the rest of the 'proper' rails app config variable declaration)

1 Answer 1

1

If the order of your initializers is important, you have to name them appropriately (they are applied in alphabetical order). The Rails Guide about this (http://guides.rubyonrails.org/configuring.html#using-initializer-files) propose to use numbers, so doing something like you proposed:

01_auth_provider.rb
02_caller.rb
...

is actually a good way.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you ! do you think there is some other way to do that ?What do you think of what I made calling some load in the application.rb ?
Why are you looking for some other way? Even if you import them manually (via a custom script), you'll have to define an order yourself. Using the file names for this may look arbitrary, but the numbers make it very easy for a human to evaluate which script is called first, just by looking at the files in the directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.