How can I change the Rails configuration in the engine plugin?

I am working on an engine for Rails as a plugin. I would like him to be able to make the necessary changes to the Rails configuration at boot, so that he can define his Gem dependencies as well as add some boot paths.

The plugin init.rb file has access to the configuration object, but it is effectively read-only, you can specify a gem, but it does not matter, the initializer should already be running at this point.

I have this now, requiring a file with a new Rails :: Initializer block as follows:

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

This works, but crowds out any existing configuration in the main application environment. rb.

Maybe I can solve this problem by adding a generator to the engine that adds something to environment.rb that loads the plugin configuration at the right stage, or maybe there is a way to add the file to config / initializers to complete this task. Not sure how best to do it.

+5
source share
3 answers

I would go with the config / initializers route. This is the default folder for hosting the plugin-specific configuration code, and it will be loaded at the right time.

For implementation, I would try my best to choose reasonable default values ​​for everything, which allowed me not to have a configuration file. (I understand that this is not always possible.)

, config/initializers, :

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

, - install.rb script . , , , .

+1

, , ? , . , Jeweler , :

s.add_dependency 'authlogic'

, , . Google 'jeweler gem dependency' Jeweler.

, rails . :

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

+1

You can easily add this line to init.rb (in your plugins directory)

config.gem 'quick_magick'

I tried it with rails 2.3.5 and it worked like magic.

0
source

All Articles