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 the plugins that are in engine-form (have an app/ directory)
27 @engines ||= plugins
.select(&:engine?)
30 # Returns all the plugins that could be found by the current locators.
32 @all_plugins ||= locate_plugins
37 plugins
.each
do |plugin
|
38 plugin
.load(initializer
)
39 register_plugin_as_loaded(plugin
)
44 ensure_all_registered_plugins_are_loaded
!
47 # Adds the load paths for every plugin into the $LOAD_PATH. Plugin load paths are
48 # added *after* the application's <tt>lib</tt> directory, to ensure that an application
49 # can always override code within a plugin.
51 # Plugin load paths are also added to Dependencies.load_paths, and Dependencies.load_once_paths.
52 def add_plugin_load_paths
53 plugins
.each
do |plugin
|
54 plugin
.load_paths
.each
do |path
|
55 $LOAD_PATH.insert(application_lib_index
+ 1, path
)
57 ActiveSupport
::Dependencies.load_paths
<< path
59 unless configuration
.reload_plugins
?
60 ActiveSupport
::Dependencies.load_once_paths
<< path
68 def engine_metal_paths
69 engines
.collect(&:metal_path)
75 add_engine_routing_configurations
76 add_engine_controller_paths
81 def add_engine_routing_configurations
82 engines
.select(&:routed?).collect(&:routing_file).each
do |routing_file
|
83 ActionController
::Routing::Routes.add_configuration_file(routing_file
)
87 def add_engine_controller_paths
88 ActionController
::Routing.controller_paths
+= engines
.collect(&:controller_path)
91 def add_engine_view_paths
92 # reverse it such that the last engine can overwrite view paths from the first, like with routes
93 paths
= ActionView
::PathSet.new(engines
.collect(&:view_path).reverse
)
94 ActionController
::Base.view_paths
.concat(paths
)
95 ActionMailer
::Base.view_paths
.concat(paths
) if configuration
.frameworks
.include?(:action_mailer)
98 # The locate_plugins method uses each class in config.plugin_locators to
99 # find the set of all plugins available to this Rails application.
101 configuration
.plugin_locators
.map
do |locator
|
102 locator
.new(initializer
).plugins
104 # TODO: sorting based on config.plugins
107 def register_plugin_as_loaded(plugin
)
108 initializer
.loaded_plugins
<< plugin
112 initializer
.configuration
115 def should_load
?(plugin
)
116 # uses Plugin#name and Plugin#valid?
117 enabled
?(plugin
) && plugin
.valid
?
120 def order_plugins(plugin_a
, plugin_b
)
121 if !explicit_plugin_loading_order
?
122 plugin_a
<=> plugin_b
124 if !explicitly_enabled
?(plugin_a
) && !explicitly_enabled
?(plugin_b
)
125 plugin_a
<=> plugin_b
127 effective_order_of(plugin_a
) <=> effective_order_of(plugin_b
)
132 def effective_order_of(plugin
)
133 if explicitly_enabled
?(plugin
)
134 registered_plugin_names
.index(plugin
.name
)
136 registered_plugin_names
.index('all')
140 def application_lib_index
141 $LOAD_PATH.index(File
.join(RAILS_ROOT
, 'lib')) || 0
145 !explicit_plugin_loading_order
? || registered
?(plugin
)
148 def explicit_plugin_loading_order
?
149 !registered_plugin_names
.nil?
152 def registered
?(plugin
)
153 explicit_plugin_loading_order
? && registered_plugins_names_plugin
?(plugin
)
156 def explicitly_enabled
?(plugin
)
157 !explicit_plugin_loading_order
? || explicitly_registered
?(plugin
)
160 def explicitly_registered
?(plugin
)
161 explicit_plugin_loading_order
? && registered_plugin_names
.include?(plugin
.name
)
164 def registered_plugins_names_plugin
?(plugin
)
165 registered_plugin_names
.include?(plugin
.name
) || registered_plugin_names
.include?('all')
168 # The plugins that have been explicitly listed with config.plugins. If this list is nil
169 # then it means the client does not care which plugins or in what order they are loaded,
170 # so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is
171 # non empty, we load the named plugins in the order specified.
172 def registered_plugin_names
173 configuration
.plugins
? configuration
.plugins
.map(&:to_s) : nil
176 def loaded
?(plugin_name
)
177 initializer
.loaded_plugins
.detect
{ |plugin
| plugin
.name
== plugin_name
.to_s
}
180 def ensure_all_registered_plugins_are_loaded
!
181 if explicit_plugin_loading_order
?
182 if configuration
.plugins
.detect
{|plugin
| plugin
!= :all && !loaded
?(plugin
) }
183 missing_plugins
= configuration
.plugins
- (plugins
.map
{|p
| p
.name
.to_sym
} + [:all])
184 raise LoadError
, "Could not locate the following plugins: #{missing_plugins.to_sentence(:locale => :en)}"