Managing Rails Plugins dependencies 1

Posted by Daniel Wanja Sat, 04 Mar 2006 09:26:00 GMT

Rails has a nice plugin system allowing to add common code to a project. A plugin should really be independent from any other plugins. But we also use plugins to share code among different projects we are working on and our code depends on existing plugins. The Rails development team want’s to keep the plugin system simple and didn’t provide an explicit way to handle these dependencies, which I believe is a good decision. There is a solution. Simply name the plugins in order off the dependencies you have. Let’s assume you want to add “my_very_own_plugin” plugin that depends on the enumation_mixin, then simply organize the /vendor/plugins folder as follows, et Voila!:.

/myrailsapp
    /vendor
        /plugins
            /01_acts_as_taggable
            /01_enumations_mixin 
            /01_acts_as_versioned
            /02_my_very_own_plugin

If we look at the Rails::Initializer we can see why this works. Note, this is only an extract of the full class that Rails provides to bootstrap your rails applicaton. The sort on line 4 here after allows this trick.

1
2
3
4
5
6
7
8
module Rails
  class Initializer
    def load_plugins
      find_plugins(configuration.plugin_paths).sort.each { |path| load_plugin path }
      $LOAD_PATH.uniq!
    end
  end
end
Comments

Leave a response

  1. Doug Wed, 04 Jun 2008 01:02:57 GMT

    I just realized that you can explicitly load a plugin from within your plugin’s init.rb by just calling load_plugin:

    load_plugin ‘vendor/plugins/acts_as_taggable’

    Little bit of a hack I suppose but it works.

Comments