I want to grab config settings for a module, and I was told to use an initializer for this.
Can someone show me how to do this and how I will reference the config settings from the initializer?
The initializer facility of Ruby on Rails allows you to run arbitrary bits of code after the main application has been loaded but before any pages have been rendered. These are one-shot things, too, and the application has to be restarted if any are changed.
If you want to configure or reconfigure a module, you just sort of do it. There's nothing fancy required. Typically they look something like this:
# config/initializers/mymodule.rb
MyModule.widgets_per_second = 100
MyModule.reject_invalid_foobars = true
It will depend on what you are trying to configure of course. You can declare extensions to classes, load in additional modules, anything that might be required for your application to work properly.
Generally it's bad form to force-load a plugin or gem that was not loaded automatically, but as with anything, sometimes you need to break the rules to get your app working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With