Plugins - plugin_dependencies
Add to favoritesplugin_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
- http://www.pluginaweek.org/2006/11/05/plugin-dependencies-revisited
- http://www.pluginaweek.org/2006/10/31/1-plugin_dependencies-who-do-you-depend-on
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:
- What happens when your dependencies become numerous and complex?
- 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:
- Ensure that the encrypted_strings plugin is present. If it’s not, an
exception will be thrown.
- 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.
http://wiki.pluginaweek.org/Plugin_dependencies
http://svn.pluginaweek.org/trunk/plugins/rails/plugin_dependencies/
Rails' (MIT)
Misc. Enhancements
