Replaced Rails environment variables with Merb equivalents.
[merb_radiant.git] / lib / radiant / initializer.rb
blob5c60176e7edcae8f5382c17df4c7f7fd614abef5
1 # FIXME Rails dependencies
3 # Add necessary Rails path
4 $LOAD_PATH.unshift "#{Merb.root}/vendor/rails/railties/lib"
6 require 'initializer'
7 require 'radiant/admin_ui'
8 #require 'radiant/extension_loader'
10 module Radiant
12   class Configuration < Rails::Configuration
13     attr_accessor :extension_paths
14     attr_accessor :extensions
15     attr_accessor :view_paths
17     def initialize
18       self.view_paths = []
19       self.extension_paths = default_extension_paths
20       self.extensions = [ :all ]
21       super
22     end
24     def default_extension_paths
25       env = ENV["MERB_ENV"] || Merb.environment
26       paths = [RADIANT_ROOT + '/vendor/extensions', Merb.root + '/vendor/extensions'].uniq
27       # There's no other way it will work, config/environments/test.rb loads too late
28       # TODO: Should figure out how to include this extension path only for the tests that need it
29       paths.unshift(RADIANT_ROOT + "/test/fixtures/extensions") if env == "test"
30       paths
31     end
33     def admin
34       AdminUI.instance
35     end
37     private
39       def library_directories
40         Dir["#{RADIANT_ROOT}/vendor/*/lib"]
41       end
43       def framework_root_path
44         RADIANT_ROOT + '/vendor/rails'
45       end
47       # Provide the load paths for the Radiant installation
48       def default_load_paths
49         paths = ["#{RADIANT_ROOT}/test/mocks/#{environment}"]
51         # Add the app's controller directory
52         paths.concat(Dir["#{RADIANT_ROOT}/app/controllers/"])
54         # Then components subdirectories.
55         paths.concat(Dir["#{RADIANT_ROOT}/components/[_a-z]*"])
57         # Followed by the standard includes.
58         paths.concat %w(
59           app
60           app/models
61           app/controllers
62           app/helpers
63           config
64           lib
65           vendor
66         ).map { |dir| "#{RADIANT_ROOT}/#{dir}" }.select { |dir| File.directory?(dir) }
68         paths.concat builtin_directories
69         paths.concat library_directories
70       end
72       def default_plugin_paths
73         [
74           "#{Merb.root}/vendor/plugins",
75           "#{RADIANT_ROOT}/lib/plugins",
76           "#{RADIANT_ROOT}/vendor/plugins"
77         ]
78       end
80       def default_view_path
81         File.join(RADIANT_ROOT, 'app', 'views')
82       end
84       def default_controller_paths
85         [File.join(RADIANT_ROOT, 'app', 'controllers')]
86       end
87   end
89   class Initializer < Rails::Initializer
90     def self.run(command = :process, configuration = Configuration.new)
91       super
92     end
94     def set_autoload_paths
95       extension_loader.add_extension_paths
96       super
97     end
99     def add_plugin_load_paths
100       # checks for plugins within extensions:
101       extension_loader.add_plugin_paths
102       super
103     end
105     def load_plugins
106       super
107       extension_loader.load_extensions
108     end
110     def after_initialize
111       extension_loader.activate_extensions
112       super
113     end
115     def initialize_default_admin_tabs
116       admin.tabs.clear
117       admin.tabs.add "Pages",    "/admin/pages"
118       admin.tabs.add "Snippets", "/admin/snippets"
119       admin.tabs.add "Layouts",  "/admin/layouts", :visibility => [:admin, :developer]
120     end
122     def initialize_framework_views
123       view_paths = returning [] do |arr|
124         # Add the singular view path if it's not in the list
125         arr << configuration.view_path if !configuration.view_paths.include?(configuration.view_path)
126         # Add the default view paths
127         arr.concat configuration.view_paths
128         # Add the extension view paths
129         arr.concat extension_loader.view_paths
130         # Reverse the list so extensions come first
131         arr.reverse!
132       end
133       ActionMailer::Base.view_paths = view_paths if configuration.frameworks.include?(:action_mailer) || defined?(ActionMailer::Base)
134       ActionController::Base.view_paths = view_paths if configuration.frameworks.include?(:action_controller)
135     end
137     def initialize_routing
138       extension_loader.add_controller_paths
139       super
140     end
142     def admin
143       configuration.admin
144     end
146     def extension_loader
147       ExtensionLoader.instance {|l| l.initializer = self }
148     end
149   end