In our Rails app we need to fiddle a bit with the plugin loading order because the default, loading them alphabetically, doesn’t work for our dependencies. Normally that would look like this in your environment.rb’s run block:
config.plugins = [ :plugin_to_load_first, :plugin_to_load_second, :all ]
Excluding is a bit trickier though. We would like to do something like this:
config.plugins -= [:do_not_load_plugin_1, :do_not_load_plugin_2]
But that won’t work unless :do_not_load_plugin_1 and do_not_load_plugin_1 are explicitly listed in config.plugins, which they ain’t. Dan Menges had the inspiration to ask rails to explicitly set config.plugins to all the plugins that can be found, then remove the ones we don’t want. Here’s an updated version of Dan’s idea for Rails 2.0:
config.plugins = config.plugin_locators.map do |locator|
locator.new(Rails::Initializer.new(config)).plugins
end.flatten.map{|p| p.name.to_sym}
config.plugins -= [:do_not_load_plugin_1, :do_not_load_plugin_2]
That’s pretty close to what we want, now we just need to make sure a couple of the plugins are loaded before the others. We might also want to exclude certain plugins based on what environment we are in. The full code listing for us looks like this:
# explicitly set the plugins to all plugins found
config.plugins = config.plugin_locators.map do |locator|
locator.new(Rails::Initializer.new(config)).plugins
end.flatten.map{|p| p.name.to_sym}
# move these two to the front due to loading dependencies
[:plugin_one, :plugin_two].each do |obj|
raise "#{obj.inspect} not found in plugins" if config.plugins.delete(obj).nil?
config.plugins.unshift(obj)
end
# exclude these plugins from being loaded in certain environments
if RAILS_ENV =~ /production|staging/
config.plugins -= [
:query_analyzer,
:annotate_models,
:html_test,
:mocha,
:query_analyzer,
:rails_rcov,
:spider_test
]
end
0 Responses to “Plugin ordering and exclusion in Rails 2.0”