Rails 3 Engine - Provide Configuration for Users

I can not find any documentation on how to do the following: I need to provide a configuration variable for any applications using my engine so that they can easily transfer settings to my engine.

Does anyone have any links to the correct or acceptable way to do this?

EDIT: As an update, I came up with a decent way to do this. The code is below.

# file: lib/my_engine.rb module MyEngine class Engine < Rails::Engine initializer "my_engine.configure_rails_initialization" do |app| # Engine configures Rails app here, this is not what my question was about end end # This is what I was trying to figure out def self.config(&block) @@config ||= MyEngine::Configuration.new yield @@config if block return @@config end end 

This allows any application using my engine to configure it, as shown below, in any of their initializers or in the environment.rb file, by calling any methods defined in the MyEngine::Configuration class:

 MyEngine.config do |config| config.some_configuration_option = "Whatever" end 
+7
ruby-on-rails-3 configuration rails-engines
source share
3 answers

Short answer:

Create a file called your_engine_class.rb and enter your hosting configuration / initializers. Here is an example of this file.

 module YourEngineClass class Engine < Rails::Engine config.param_name = 'value' end end 

Inside your main engine.rb you can access:

 config.param_name 

Longer answer: I created a stub mechanism that includes this and many other standard settings that you will need, you can use it as a starting point:

http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/

+6
source share

I personally like the Devise template, which defines the class method in your engine module, and then the initializer in the parent application.

Create a class method to call from the parent application

 module YourEngine class << self attr_accessor :your_config_var end def self.setup(&block) # You can yield your own object encapsulating your configuration logic/state yield self end end 

Call the class method in the initializer in the parent application

 YourEngine.setup do |config| config.your_config_var = "nyan cat" end 

Now you can access your_config_var during the initialization process of your engine. You can create a file in your config/initializers directory or use the Rails Initializer .

I like this approach because you have more control over which configuration object you expose to clients and do not override the config engine method.

+5
source share

It seems to me better:

 #lib/my_engine.rb require 'rails' module MyEngine class Engine < Rails::Engine end def self.config(&block) yield Engine.config if block Engine.config end end 
+4
source share

All Articles