0
- attr_reader :initializer, :directory, :name
0
- def initialize(initializer, directory)
0
+ attr_reader :initializer
0
+ # Creates a new Plugin::Loader instance, associated with the given
0
+ # Rails::Initializer. This default implementation automatically locates
0
+ # all plugins, and adds all plugin load paths, when it is created. The plugins
0
+ # are then fully loaded (init.rb is evaluated) when load_plugins is called.
0
+ # It is the loader's responsibilty to ensure that only the plugins specified
0
+ # in the configuration are actually loaded, and that the order defined
0
+ def initialize(initializer)
0
@initializer = initializer
0
- @directory = directory
0
- @name = File.basename(directory).to_sym
0
- return false if loaded?
0
- report_nonexistant_or_empty_plugin!
0
- register_plugin_as_loaded
0
+ # Returns the plugins to be loaded, in the order they should be loaded.
0
+ @plugins ||= all_plugins.select { |plugin| should_load?(plugin) }.sort { |p1, p2| order_plugins(p1, p2) }
0
- initializer.loaded_plugins.include?(name)
0
+ # Returns all the plugins that could be found by the current locators.
0
+ @all_plugins ||= locate_plugins
0
- File.directory?(directory) && (has_lib_directory? || has_init_file?)
0
+ plugins.each do |plugin|
0
+ plugin.load(initializer)
0
+ register_plugin_as_loaded(plugin)
0
+ ensure_all_registered_plugins_are_loaded!
0
- !explicit_plugin_loading_order? || registered?
0
+ # Adds the load paths for every plugin into the $LOAD_PATH. Plugin load paths are
0
+ # added *after* the application's <tt>lib</tt> directory, to ensure that an application
0
+ # can always override code within a plugin.
0
+ # Plugin load paths are also added to Dependencies.load_paths, and Dependencies.load_once_paths.
0
+ def add_plugin_load_paths
0
+ plugins.each do |plugin|
0
+ plugin.load_paths.each do |path|
0
+ $LOAD_PATH.insert(application_lib_index + 1, path)
0
+ Dependencies.load_paths << path
0
+ Dependencies.load_once_paths << path
0
- def explicitly_enabled?
0
- !explicit_plugin_loading_order? || explicitly_registered?
0
- explicit_plugin_loading_order? && registered_plugins_names_plugin?(name)
0
+ # The locate_plugins method uses each class in config.plugin_locators to
0
+ # find the set of all plugins available to this Rails application.
0
+ configuration.plugin_locators.map { |locator|
0
+ locator.new(initializer).plugins
0
+ # TODO: sorting based on config.plugins
0
- def explicitly_registered?
0
- explicit_plugin_loading_order? && registered_plugins.include?(name)
0
- def plugin_does_not_exist!(plugin_name = name)
0
- raise LoadError, "Can not find the plugin named: #{plugin_name}"
0
- # The plugins that have been explicitly listed with config.plugins. If this list is nil
0
- # then it means the client does not care which plugins or in what order they are loaded,
0
- # so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is
0
- # non empty, we load the named plugins in the order specified.
0
- def registered_plugins
0
+ def register_plugin_as_loaded(plugin)
0
+ initializer.loaded_plugins << plugin
0
- def registered_plugins_names_plugin?(plugin_name)
0
- registered_plugins.include?(plugin_name) || registered_plugins.include?(:all)
0
+ initializer.configuration
0
- def explicit_plugin_loading_order?
0
- !registered_plugins.nil?
0
+ def should_load?(plugin)
0
+ # uses Plugin#name and Plugin#valid?
0
+ enabled?(plugin) && plugin.valid?
0
- def report_nonexistant_or_empty_plugin!
0
- plugin_does_not_exist! unless plugin_path?
0
+ def order_plugins(plugin_a, plugin_b)
0
+ if !explicit_plugin_loading_order?
0
+ if !explicitly_enabled?(plugin_a) && !explicitly_enabled?(plugin_b)
0
+ effective_order_of(plugin_a) <=> effective_order_of(plugin_b)
0
- File.join(directory, 'lib')
0
+ def effective_order_of(plugin)
0
+ if explicitly_enabled?(plugin)
0
+ registered_plugin_names.index(plugin.name)
0
+ registered_plugin_names.index('all')
0
- File.join(directory, 'init.rb')
0
+ def application_lib_index
0
+ $LOAD_PATH.index(File.join(RAILS_ROOT, 'lib')) || 0
0
+ !explicit_plugin_loading_order? || registered?(plugin)
0
- def has_lib_directory?
0
- File.directory?(lib_path)
0
+ def explicit_plugin_loading_order?
0
+ !registered_plugin_names.nil?
0
+ def registered?(plugin)
0
+ explicit_plugin_loading_order? && registered_plugins_names_plugin?(plugin)
0
- # Add lib to load path *after* the application lib, to allow
0
- # application libraries to override plugin libraries.
0
- application_lib_index = $LOAD_PATH.index(application_library_path) || 0
0
- $LOAD_PATH.insert(application_lib_index + 1, lib_path)
0
- Dependencies.load_paths << lib_path
0
- Dependencies.load_once_paths << lib_path
0
+ def explicitly_enabled?(plugin)
0
+ !explicit_plugin_loading_order? || explicitly_registered?(plugin)
0
- def application_library_path
0
- File.join(RAILS_ROOT, 'lib')
0
+ def explicitly_registered?(plugin)
0
+ explicit_plugin_loading_order? && registered_plugin_names.include?(plugin.name)
0
- # Allow plugins to reference the current configuration object
0
- initializer.configuration
0
+ def registered_plugins_names_plugin?(plugin)
0
+ registered_plugin_names.include?(plugin.name) || registered_plugin_names.include?('all')
0
- def register_plugin_as_loaded
0
- initializer.loaded_plugins << name
0
+ # The plugins that have been explicitly listed with config.plugins. If this list is nil
0
+ # then it means the client does not care which plugins or in what order they are loaded,
0
+ # so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is
0
+ # non empty, we load the named plugins in the order specified.
0
+ def registered_plugin_names
0
+ configuration.plugins ? configuration.plugins.map(&:to_s) : nil
0
- silence_warnings { eval(IO.read(init_path), binding, init_path) } if has_init_file?
0
+ def loaded?(plugin_name)
0
+ initializer.loaded_plugins.detect { |plugin| plugin.name == plugin_name.to_s }
0
- def <=>(other_plugin_loader)
0
+ def ensure_all_registered_plugins_are_loaded!
0
if explicit_plugin_loading_order?
0
- if non_existent_plugin = [self, other_plugin_loader].detect { |plugin| !registered_plugins_names_plugin?(plugin.name) }
0
- plugin_does_not_exist!(non_existent_plugin.name)
0
- if !explicitly_enabled? && !other_plugin_loader.explicitly_enabled?
0
- name.to_s <=> other_plugin_loader.name.to_s
0
- elsif registered_plugins.include?(:all) && (!explicitly_enabled? || !other_plugin_loader.explicitly_enabled?)
0
- effective_index = explicitly_enabled? ? registered_plugins.index(name) : registered_plugins.index(:all)
0
- other_effective_index = other_plugin_loader.explicitly_enabled? ?
0
- registered_plugins.index(other_plugin_loader.name) : registered_plugins.index(:all)
0
- effective_index <=> other_effective_index
0
- registered_plugins.index(name) <=> registered_plugins.index(other_plugin_loader.name)
0
+ if configuration.plugins.detect {|plugin| plugin != :all && !loaded?(plugin) }
0
+ missing_plugins = configuration.plugins - (plugins + [:all])
0
+ raise LoadError, "Could not locate the following plugins: #{missing_plugins.to_sentence}"
0
- name.to_s <=> other_plugin_loader.name.to_s
0
\ No newline at end of file