Module: PluginAWeek::LoadedPlugins::Extensions::Initializer

Defined in:
lib/loaded_plugins/extensions/initializer.rb

Overview

Keeps track of all the plugins that Rails loaded during initialization

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(initializer) ⇒ Object

:nodoc:



13
14
15
16
17
18
# File 'lib/loaded_plugins/extensions/initializer.rb', line 13

def self.extended(initializer) #:nodoc:
  # Get all of the paths we missed before this plugin was loaded.  This
  # is only necessary for plugins that need +Rails.plugins+ during
  # initialization.
  initializer.add_loaded_plugins
end

.included(base) ⇒ Object

:nodoc:



6
7
8
9
10
11
# File 'lib/loaded_plugins/extensions/initializer.rb', line 6

def self.included(base) #:nodoc:
  base.class_eval do
    alias_method_chain :load_plugin, :loaded_plugins
    alias_method_chain :after_initialize, :loaded_plugins
  end
end

Instance Method Details

#add_loaded_pluginsObject

Adds all of the loaded plugins that were missed to Rails.plugins



61
62
63
64
65
66
67
# File 'lib/loaded_plugins/extensions/initializer.rb', line 61

def add_loaded_plugins
  loaded_plugins.reverse.each do |name|
    if !Rails.plugins[name] && path = find_plugin_path(name)
      Rails.plugins.insert(0, Plugin.new(path))
    end
  end
end

#after_initialize_with_loaded_pluginsObject

Freezes the list of loaded plugins and invokes the after_initialize callback for each plugin



52
53
54
55
56
57
58
# File 'lib/loaded_plugins/extensions/initializer.rb', line 52

def after_initialize_with_loaded_plugins #:nodoc:
  add_loaded_plugins
  Rails.plugins.freeze
  Rails.plugins.each {|plugin| plugin.after_initialize}
  
  after_initialize_without_loaded_plugins
end

#load_plugin_with_loaded_plugins(directory) ⇒ Object

Loads the plugin from the specified directory, tracking all successfully loaded plugins in Rails.plugins



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/loaded_plugins/extensions/initializer.rb', line 22

def load_plugin_with_loaded_plugins(directory)
  plugin = Plugin.new(directory)
  return false if loaded_plugins.include?(plugin.name)
  
  # Be careful to place the plugin in the correct order in case a plugin
  # has required another plugin
  if @loaded_plugin_index
    Rails.plugins.insert(@loaded_plugin_index, plugin)
  else
    Rails.plugins << plugin
    @loaded_plugin_index = Rails.plugins.size - 1
  end
  
  # Plugin is about to be loaded
  plugin.before_load
  
  load_plugin_without_loaded_plugins(directory)
  
  # Plugin has been loaded
  plugin.after_load
  
  # We no longer need to track the current index in LOADED_PLUGINS if
  # we're the last one
  @loaded_plugin_index = nil if Rails.plugins.last == plugin
  
  true
end