Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / lib / radiant / .svn / text-base / extension_loader.rb.svn-base
blob451704567af5c39e447d1d16f2fd35f95c6984b4
1 require 'radiant/extension'
2 require 'method_observer'
4 module Radiant
5   class ExtensionLoader
6     
7     class DependenciesObserver < MethodObserver
8       attr_accessor :config
9       
10       def initialize(rails_config)
11         @config = rails_config
12       end
13       
14       def before_clear(*args)
15         ExtensionLoader.deactivate_extensions
16       end
17       
18       def after_clear(*args)
19         ExtensionLoader.load_extensions
20         ExtensionLoader.activate_extensions
21       end
22     end
24     include Simpleton
25     
26     attr_accessor :initializer, :extensions
27     
28     def initialize
29       self.extensions = []
30     end
31     
32     def configuration
33       initializer.configuration
34     end
35     
36     def extension_load_paths
37       load_extension_roots.map { |extension| load_paths_for(extension) }.flatten.select { |d| File.directory?(d) }
38     end
40     def plugin_paths
41       load_extension_roots.map {|extension| "#{extension}/vendor/plugins" }.select {|d| File.directory?(d) }
42     end
43     
44     def add_extension_paths
45       extension_load_paths.reverse_each do |path|
46         configuration.load_paths.unshift path
47         $LOAD_PATH.unshift path
48       end
49     end
50     
51     def add_plugin_paths
52       configuration.plugin_paths.concat plugin_paths
53     end
55     def controller_paths
56       extensions.map { |extension| "#{extension.root}/app/controllers" }.select { |d| File.directory?(d) }
57     end
58     
59     def add_controller_paths
60       configuration.controller_paths.concat(controller_paths)
61     end
62     
63     def view_paths
64       extensions.map { |extension| "#{extension.root}/app/views" }.select { |d| File.directory?(d) }
65     end
66     
67     # Load the extensions
68     def load_extensions
69       @observer ||= DependenciesObserver.new(configuration).observe(::Dependencies)
70       self.extensions = load_extension_roots.map do |root|
71         begin
72           extension_file = "#{File.basename(root).sub(/^\d+_/,'')}_extension"
73           extension = extension_file.camelize.constantize
74           extension.unloadable
75           extension.root = root
76           extension
77         rescue LoadError, NameError => e
78           $stderr.puts "Could not load extension from file: #{extension_file}.\n#{e.inspect}"
79           nil
80         end
81       end.compact
82     end
83     
84     def deactivate_extensions
85       extensions.each &:deactivate
86     end
87     
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
93     end
94     alias :reactivate :activate_extensions
96     private
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|
101             path = "#{dir}/#{p}"
102             path if File.directory?(path)
103           end.compact << dir
104         else
105           []
106         end
107       end
108       
109       def load_extension_roots
110         @load_extension_roots ||= unless configuration.extensions.empty?
111           select_extension_roots
112         else
113           []
114         end
115       end
116       
117       def select_extension_roots
118         all_roots = all_extension_roots.dup
119         
120         roots = configuration.extensions.map do |ext_name|
121           if :all === ext_name
122             :all
123           else
124             ext_path = all_roots.detect do |maybe_path|
125               File.basename(maybe_path).sub(/^\d+_/, '') == ext_name.to_s
126             end
127             raise LoadError, "Cannot find the extension '#{ext_name}'!" if ext_path.nil?
128             all_roots.delete(ext_path)
129           end
130         end
132         if placeholder = roots.index(:all)
133           # replace the :all symbol with any remaining paths
134           roots[placeholder, 1] = all_roots
135         end
136         roots
137       end
138       
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
142         end.flatten
143       end
144   end