6 attr_reader
:initializer
8 # Creates a new Plugin::Loader instance, associated with the given
9 # Rails::Initializer. This default implementation automatically locates
10 # all plugins, and adds all plugin load paths, when it is created. The plugins
11 # are then fully loaded (init.rb is evaluated) when load_plugins is called.
13 # It is the loader's responsibility to ensure that only the plugins specified
14 # in the configuration are actually loaded, and that the order defined
16 def initialize(initializer
)
17 @initializer = initializer
20 # Returns the plugins to be loaded, in the order they should be loaded.
22 @plugins ||= all_plugins
.select
{ |plugin
| should_load
?(plugin
) }.sort
{ |p1
, p2
| order_plugins(p1
, p2
) }
25 # Returns all the plugins that could be found by the current locators.
27 @all_plugins ||= locate_plugins
32 plugins
.each
do |plugin
|
33 plugin
.load(initializer
)
34 register_plugin_as_loaded(plugin
)
36 ensure_all_registered_plugins_are_loaded
!
39 # Adds the load paths for every plugin into the $LOAD_PATH. Plugin load paths are
40 # added *after* the application's <tt>lib</tt> directory, to ensure that an application
41 # can always override code within a plugin.
43 # Plugin load paths are also added to Dependencies.load_paths, and Dependencies.load_once_paths.
44 def add_plugin_load_paths
45 plugins
.each
do |plugin
|
46 plugin
.load_paths
.each
do |path
|
47 $LOAD_PATH.insert(application_lib_index
+ 1, path
)
48 ActiveSupport
::Dependencies.load_paths
<< path
49 unless Rails
.configuration
.reload_plugins
?
50 ActiveSupport
::Dependencies.load_once_paths
<< path
59 # The locate_plugins method uses each class in config.plugin_locators to
60 # find the set of all plugins available to this Rails application.
62 configuration
.plugin_locators
.map
{ |locator
|
63 locator
.new(initializer
).plugins
65 # TODO: sorting based on config.plugins
68 def register_plugin_as_loaded(plugin
)
69 initializer
.loaded_plugins
<< plugin
73 initializer
.configuration
76 def should_load
?(plugin
)
77 # uses Plugin#name and Plugin#valid?
78 enabled
?(plugin
) && plugin
.valid
?
81 def order_plugins(plugin_a
, plugin_b
)
82 if !explicit_plugin_loading_order
?
85 if !explicitly_enabled
?(plugin_a
) && !explicitly_enabled
?(plugin_b
)
88 effective_order_of(plugin_a
) <=> effective_order_of(plugin_b
)
93 def effective_order_of(plugin
)
94 if explicitly_enabled
?(plugin
)
95 registered_plugin_names
.index(plugin
.name
)
97 registered_plugin_names
.index('all')
101 def application_lib_index
102 $LOAD_PATH.index(File
.join(RAILS_ROOT
, 'lib')) || 0
106 !explicit_plugin_loading_order
? || registered
?(plugin
)
109 def explicit_plugin_loading_order
?
110 !registered_plugin_names
.nil?
113 def registered
?(plugin
)
114 explicit_plugin_loading_order
? && registered_plugins_names_plugin
?(plugin
)
117 def explicitly_enabled
?(plugin
)
118 !explicit_plugin_loading_order
? || explicitly_registered
?(plugin
)
121 def explicitly_registered
?(plugin
)
122 explicit_plugin_loading_order
? && registered_plugin_names
.include?(plugin
.name
)
125 def registered_plugins_names_plugin
?(plugin
)
126 registered_plugin_names
.include?(plugin
.name
) || registered_plugin_names
.include?('all')
129 # The plugins that have been explicitly listed with config.plugins. If this list is nil
130 # then it means the client does not care which plugins or in what order they are loaded,
131 # so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is
132 # non empty, we load the named plugins in the order specified.
133 def registered_plugin_names
134 configuration
.plugins
? configuration
.plugins
.map(&:to_s) : nil
137 def loaded
?(plugin_name
)
138 initializer
.loaded_plugins
.detect
{ |plugin
| plugin
.name
== plugin_name
.to_s
}
141 def ensure_all_registered_plugins_are_loaded
!
142 if explicit_plugin_loading_order
?
143 if configuration
.plugins
.detect
{|plugin
| plugin
!= :all && !loaded
?(plugin
) }
144 missing_plugins
= configuration
.plugins
- (plugins
+ [:all])
145 raise LoadError
, "Could not locate the following plugins: #{missing_plugins.to_sentence}"