Rails mountable engine: how should apps set configuration variables?
Figured out an even better solution that also allows you to set default values (incase the app using the engine doesn't specify a config)...
- Create config variables in your app's
/config/initializers/blog.rb
like this:
Blog.setup do |config|
config.site_name = "My Site Name"
end
- In your engine's
/lib/blog/engine.rb
set default values like this:
module Blog
self.mattr_accessor :site_name
self.site_name = "Site Name"
# add default values of more config vars here
# this function maps the vars from your app into your engine
def self.setup(&block)
yield self
end
end
- Now you can simply access the config variables in your engine like this:
Blog.site_name
Much cleaner.
After a lot of testing and looking into existing gems, here is what works in Rails 4:
Considering your engine's name is Blog
:
In your engine's /lib/blog/engine.rb
put in this:
module Blog
def self.setup(&block)
@config ||= Blog::Engine::Configuration.new
yield @config if block
@config
end
def self.config
Rails.application.config
end
end
In your app, create a file called /config/initalizers/blog.rb
and set config vars like this:
Blog.setup do |config|
config.testingvar = "asdfasdf"
end
Then you can access these config variables ANYWHERE in your engine like this:
Blog.config.testingvar
Hope this helps someone. There is very little documentation on this right now so it was all trial and error.