1 # FIXME Rails dependencies / use gem
3 require 'radiant/extension'
4 require 'method_observer'
9 class DependenciesObserver < MethodObserver
12 def initialize(rails_config)
13 @config = rails_config
16 def before_clear(*args)
17 ExtensionLoader.deactivate_extensions
20 def after_clear(*args)
21 ExtensionLoader.load_extensions
22 ExtensionLoader.activate_extensions
28 attr_accessor :initializer, :extensions
35 initializer.configuration
38 def extension_load_paths
39 load_extension_roots.map { |extension| load_paths_for(extension) }.flatten.select { |d| File.directory?(d) }
43 load_extension_roots.map {|extension| "#{extension}/vendor/plugins" }.select {|d| File.directory?(d) }
46 def add_extension_paths
47 extension_load_paths.reverse_each do |path|
48 configuration.load_paths.unshift path
49 $LOAD_PATH.unshift path
54 configuration.plugin_paths.concat plugin_paths
58 extensions.map { |extension| "#{extension.root}/app/controllers" }.select { |d| File.directory?(d) }
61 def add_controller_paths
62 configuration.controller_paths.concat(controller_paths)
66 extensions.map { |extension| "#{extension.root}/app/views" }.select { |d| File.directory?(d) }
71 @observer ||= DependenciesObserver.new(configuration).observe(::Dependencies)
72 self.extensions = load_extension_roots.map do |root|
74 extension_file = "#{File.basename(root).sub(/^\d+_/,'')}_extension"
75 extension = extension_file.camelize.constantize
79 rescue LoadError, NameError => e
80 $stderr.puts "Could not load extension from file: #{extension_file}.\n#{e.inspect}"
86 def deactivate_extensions
87 extensions.each &:deactivate
90 def activate_extensions
91 initializer.initialize_default_admin_tabs
92 # Reset the view paths after
93 initializer.initialize_framework_views
94 extensions.each &:activate
96 alias :reactivate :activate_extensions
100 def load_paths_for(dir)
101 if File.directory?(dir)
102 %w(lib app/models app/controllers app/helpers test/helpers).collect do |p|
104 path if File.directory?(path)
111 def load_extension_roots
112 @load_extension_roots ||= unless configuration.extensions.empty?
113 select_extension_roots
119 def select_extension_roots
120 all_roots = all_extension_roots.dup
122 roots = configuration.extensions.map do |ext_name|
126 ext_path = all_roots.detect do |maybe_path|
127 File.basename(maybe_path).sub(/^\d+_/, '') == ext_name.to_s
129 raise LoadError, "Cannot find the extension '#{ext_name}'!" if ext_path.nil?
130 all_roots.delete(ext_path)
134 if placeholder = roots.index(:all)
135 # replace the :all symbol with any remaining paths
136 roots[placeholder, 1] = all_roots
141 def all_extension_roots
142 @all_extension_roots ||= configuration.extension_paths.map do |path|
143 Dir["#{path}/*"].map {|f| File.expand_path(f) if File.directory?(f) }.compact.sort