1 require 'radiant/extension'
2 require 'method_observer'
7 class DependenciesObserver < MethodObserver
10 def initialize(rails_config)
11 @config = rails_config
14 def before_clear(*args)
15 ExtensionLoader.deactivate_extensions
18 def after_clear(*args)
19 ExtensionLoader.load_extensions
20 ExtensionLoader.activate_extensions
26 attr_accessor :initializer, :extensions
33 initializer.configuration
36 def extension_load_paths
37 load_extension_roots.map { |extension| load_paths_for(extension) }.flatten.select { |d| File.directory?(d) }
41 load_extension_roots.map {|extension| "#{extension}/vendor/plugins" }.select {|d| File.directory?(d) }
44 def add_extension_paths
45 extension_load_paths.reverse_each do |path|
46 configuration.load_paths.unshift path
47 $LOAD_PATH.unshift path
52 configuration.plugin_paths.concat plugin_paths
56 extensions.map { |extension| "#{extension.root}/app/controllers" }.select { |d| File.directory?(d) }
59 def add_controller_paths
60 configuration.controller_paths.concat(controller_paths)
64 extensions.map { |extension| "#{extension.root}/app/views" }.select { |d| File.directory?(d) }
69 @observer ||= DependenciesObserver.new(configuration).observe(::Dependencies)
70 self.extensions = load_extension_roots.map do |root|
72 extension_file = "#{File.basename(root).sub(/^\d+_/,'')}_extension"
73 extension = extension_file.camelize.constantize
77 rescue LoadError, NameError => e
78 $stderr.puts "Could not load extension from file: #{extension_file}.\n#{e.inspect}"
84 def deactivate_extensions
85 extensions.each &:deactivate
88 def activate_extensions
89 initializer.initialize_default_admin_tabs
90 # Reset the view paths after
91 initializer.initialize_framework_views
92 extensions.each &:activate
94 alias :reactivate :activate_extensions
98 def load_paths_for(dir)
99 if File.directory?(dir)
100 %w(lib app/models app/controllers app/helpers test/helpers).collect do |p|
102 path if File.directory?(path)
109 def load_extension_roots
110 @load_extension_roots ||= unless configuration.extensions.empty?
111 select_extension_roots
117 def select_extension_roots
118 all_roots = all_extension_roots.dup
120 roots = configuration.extensions.map do |ext_name|
124 ext_path = all_roots.detect do |maybe_path|
125 File.basename(maybe_path).sub(/^\d+_/, '') == ext_name.to_s
127 raise LoadError, "Cannot find the extension '#{ext_name}'!" if ext_path.nil?
128 all_roots.delete(ext_path)
132 if placeholder = roots.index(:all)
133 # replace the :all symbol with any remaining paths
134 roots[placeholder, 1] = all_roots
139 def all_extension_roots
140 @all_extension_roots ||= configuration.extension_paths.map do |path|
141 Dir["#{path}/*"].map {|f| File.expand_path(f) if File.directory?(f) }.compact.sort