Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify Rails' config within an engine plugin?

I'm working on an engine for Rails as a plugin. I'd like it to be able to make necessary changes to Rails' configuration when it's loaded so it can specify its Gem dependencies as well as add some load paths.

The plugin's init.rb file has access to the config object but this is effectively read-only, you can specify a gem but it makes no difference, the initializer must have run already at this point.

I've got around this for now by requiring a file with a new Rails::Initializer block like so:

Rails::Initializer.run do |config|
  config.gem "authlogic", :version => ">= 2.0.9"
  # etc
end

This works but wipes out any existing configuration in the main application's environment.rb.

Maybe I can solve this by having a generator in the engine that adds something to environment.rb that loads the plugin's config at the right stage, or maybe there is a way of adding a file to config/initializers to do this job. Not sure how best to go about this though.

like image 987
DavidNorth Avatar asked Nov 05 '22 20:11

DavidNorth


2 Answers

I would go with the config/initializers route. That is the standard folder to put plugin-specific config code and it will get loaded at the correct time.

For implementation, I would try my hardest to choose sensible defaults for everything that allowed me to not have a config file. (I understand this is not always possible.)

Next I would create a generator with the plugin that would automatically create the config file in config/initializers using:

./script/generate plugin MyPlugin --with-generator

Finally, I would put something in install.rb of my plugin to run the generator script when the plugin is installed. This way the config file is generated automatically with the install, and the user still has an easy way to regenerate if he wants to restore the default configuration.

like image 73
erik Avatar answered Nov 12 '22 12:11

erik


Are you sure you want to distribute this as a plugin rather than a gem? If you package your engine as a gem then you can specify gem dependencies as part of your gem build process. For example, if you use Jeweler to create your gem you just add a single line:

s.add_dependency 'authlogic'

When your gem is installed it will make sure all dependencies are installed. Google 'jeweler gem dependency' for a full Jeweler config example.

Also, I've been doing a lot of work on my own rails engine and recently extracted a lot of useful base functionality. You may find this helpful for other engine issues:

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

like image 21
Keith Schacht Avatar answered Nov 12 '22 14:11

Keith Schacht