Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create my own initializer for a module?

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?

like image 253
Blankman Avatar asked Nov 02 '10 14:11

Blankman


1 Answers

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.

like image 175
tadman Avatar answered Oct 13 '22 09:10

tadman