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

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.

Rails: using ZenTest / autospec to test specs in new directories


How to build good documentation with REST API in Rails?
I would go with the config/initializers route.

Partial with Multiple Variables
That is the standard folder to put plugin-specific config code and it will get loaded at the correct time..
ActiveRecord: Cache and automatically update (redundant) attributes - Best Practice
For implementation, I would try my hardest to choose sensible defaults for everything that allowed me to not have a config file.

Capistrano's Failure
(I understand this is not always possible.).
Lifestream in Rails
Next I would create a generator with the plugin that would automatically create the config file in config/initializers using:.
Attachment_fu failing to validate on update
./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.

global access to a collection in rails
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..


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/.


You can easily add this line to init.rb (under your plugin directory).
config.gem 'quick_magick' 
I tried it with rails 2.3.5 and it worked like magic..

79 out of 100 based on 89 user ratings 1189 reviews