Plugins - plugin_dependencies

StarAdd to favorites

plugin_dependencies

plugin_dependencies adds dependency support for using require with plugins and for loading plugins (and their tasks) installed in the gem repository.

Resources

Announcement

Wiki

API

Development

Source

Description

Advanced Rails plugins can begin to increase in complexity as they begin to depend on the presence of other plugins. For example, a plugin like encrypted_attributes[http://wiki.pluginaweek.org/Encrypted_attributes] depends on the presence of encrypted_strings[http://wiki.pluginaweek.org/Encrypted_strings]. This results in a vendor/plugins directory structure like so:

  vendor/
  vendor/plugins
  vendor/plugins/encrypted_attributes
  vendor/plugins/encrypted_strings

Ordered alphabetically, encrypted_attributes will be loaded before encrypted_strings. This could potentially cause issues if the assumption is that the plugin’s dependencies have already been loaded.

As of Rails 1.2, one solution is to explicitly specify the order of plugins within your environment configuration, like so:

  Rails::Initializer.run do |config|
    config.plugins = [
      'encrypted_strings',
      'encrypted_attributes'
    ]
  end

While this solution may be sufficient for projects using very few plugins, there are two major issues:

  1. What happens when your dependencies become numerous and complex?
  2. What happens to your dependencies if you want to release your plugin as a gem?

This issue causes the above solution to no longer be a maintainable design.

This plugin addresses this issue by giving all plugins/gems the ability to define what other plugins they depend on using a similar api to that of gems.

Usage

Defining dependencies

For example, encrypted_attributes.rb would look like the following:

encrypted_attributes/lib/encrypted_attributes.rb:

  require 'encrypted_strings'
  ...

When Rails is initialized, even though encrypted_attributes.rb will be executed first, require does two things:

  1. Ensure that the encrypted_strings plugin is present. If it’s not, an

exception will be thrown.

  1. Load the encrypted_strings plugin and then continue initialization of the

encrypted_attributes plugin.

This gives plugin developers the ability to add descriptive information about dependencies directly within their plugins rather than forcing the load order in the environment configuration.

Dependency loading alternatives

In addition to require, you can also use plugin. For example:

encrypted_attributes/lib/encrypted_attributes.rb:

  plugin 'encrypted_strings'

The difference is that it will only search for plugins in your project or gems that have an init.rb file (indicating that it can also be a plugin).

Loading plugins from the gem repository

config/environment.rb:

  ...
  Rails::Initializer.run do |config|
    config.plugins = [
      'encrypted_attributes',
      ['fixture_references', '>= 0.0.1']
    ]
  end

In the above example, all plugins specified in config.plugins will either be loaded in from the application’s installed plugins or from the your Ruby’s gem path.

Loading tasks from a gem

If you’ve installed plugin_dependencies as a gem, you will need to add tasks/plugin_dependencies_tasks.rake to your application’s lib/tasks folder. Otherwise, if plugin_dependencies is installed as a plugin within your application, you will not need to make any changes.

Installation

To use this plugin, you can either install it as a gem or use it as a plugin within your project.

Installing as a gem (recommended)

If installed as a gem, you must modify your config/environment.rb to require the plugin before you run Rails::Initializer. For example,

  require 'plugin_dependencies'

  Rails::Initializer.run do |config|
    ...
  end

Installing as a plugin

If you’re installing plugin_dependencies a plugin within your project, you can explicitly set the plugin loading order by adding the following configuration in your environment.rb:

config/environment.rb:

  ...
  Rails::Initializer.run do |config|
    config.plugins = [
      'plugin_dependencies',
      ...
    ]
  end

NOTE: If installing as a plugin, you will not be able to reference gem plugins in your config.plugins configuration.

Dependencies

This plugin does not depend on the presence of the any other plugin.

Aaron Pfeifer, Neil Abraham

http://wiki.pluginaweek.org/Plugin_dependencies

http://svn.pluginaweek.org/trunk/plugins/rails/plugin_dependencies/

Rails' (MIT)

  • Currently 4.0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Misc. Enhancements

Tags

Comments

Add a comment

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.

Have a comment?