I am working on releasing my first Gem for Rails. It is pretty simple, but I am needing to provide some access to setting config options. I’d like to have the user put something in ‘config/initializers’ and tie into that with my gem.
So, the question is: Is there a best-practice for providing config options in a Rails gem?
In an engine I help develop, Forem, we use
mattr_accessorson the top-level constant like this:lib/forem.rb
Then inside
config/initializerswe ask users to set them up like this:With a short gem name, there’s not much difference between this solution and the other one I will propose.
Solution #2
Still use
mattr_accessorson your top-level constant but offer aconfigmethod on this module that takes a block and yields the object:This way people can do: