/
initializer.rb
246 lines (211 loc) · 7.29 KB
/
initializer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Add necessary Rails path
$LOAD_PATH.unshift "#{RADIANT_ROOT}/vendor/rails/railties/lib"
require 'initializer'
require 'radiant/admin_ui'
require 'radiant/extension_loader'
module Radiant
autoload :Cache, 'radiant/cache'
class Configuration < Rails::Configuration
attr_accessor :extension_paths
attr_writer :extensions
attr_accessor :view_paths
attr_accessor :extension_dependencies
def initialize
self.view_paths = []
self.extension_paths = default_extension_paths
self.extension_dependencies = []
super
end
def default_extension_paths
env = ENV["RAILS_ENV"] || RAILS_ENV
paths = [RAILS_ROOT + '/vendor/extensions', RADIANT_ROOT + '/vendor/extensions'].uniq
# There's no other way it will work, config/environments/test.rb loads too late
# TODO: Should figure out how to include this extension path only for the tests that need it
paths.unshift(RADIANT_ROOT + "/test/fixtures/extensions") if env == "test"
paths
end
def extensions
@extensions ||= all_available_extensions
end
def all_available_extensions
extension_paths.map do |path|
Dir["#{path}/*"].select {|f| File.directory?(f) }
end.flatten.map {|f| File.basename(f).sub(/^\d+_/, '') }.sort.map(&:to_sym)
end
def admin
AdminUI.instance
end
# Declare another extension as a dependency. Does not allow for the
# specification of versions.
# class MyExtension < Radiant::Extension
# extension_config do |config|
# config.extension 'multisite'
# end
# end
def extension(ext)
@extension_dependencies << ext unless @extension_dependencies.include?(ext)
end
def check_extension_dependencies
unloaded_extensions = []
@extension_dependencies.each do |ext|
extension = ext.camelcase + 'Extension'
begin
extension_class = extension.constantize
unloaded_extensions << extension unless defined?(extension_class) && (extension_class.active?)
rescue NameError
unloaded_extensions << extension
end
end
if unloaded_extensions.any?
abort <<-end_error
Missing these required extensions:
#{unloaded_extensions}
end_error
else
return true
end
end
private
def library_directories
libs = %W{
#{RADIANT_ROOT}/vendor/radius/lib
#{RADIANT_ROOT}/vendor/highline/lib
#{RADIANT_ROOT}/vendor/rack-cache/lib
}
begin
Object.send :gem, 'RedCloth', ">=4.0.0"
require 'redcloth'
rescue LoadError, Gem::LoadError
# If the gem is not available, use the packaged version
libs << "#{RADIANT_ROOT}/vendor/redcloth/lib"
after_initialize do
warn "RedCloth > 4.0 not found. Falling back to RedCloth 3.0.4 (2005-09-15). You should run `gem install RedCloth`."
require 'redcloth'
end
end
libs
end
def framework_root_path
RADIANT_ROOT + '/vendor/rails'
end
# Provide the load paths for the Radiant installation
def default_load_paths
paths = ["#{RADIANT_ROOT}/test/mocks/#{environment}"]
# Add the app's controller directory
paths.concat(Dir["#{RADIANT_ROOT}/app/controllers/"])
# Followed by the standard includes.
paths.concat %w(
app
app/metal
app/models
app/controllers
app/helpers
config
lib
vendor
).map { |dir| "#{RADIANT_ROOT}/#{dir}" }.select { |dir| File.directory?(dir) }
paths.concat builtin_directories
paths.concat library_directories
end
def default_plugin_paths
[
"#{RAILS_ROOT}/vendor/plugins",
"#{RADIANT_ROOT}/lib/plugins",
"#{RADIANT_ROOT}/vendor/plugins"
]
end
def default_view_path
File.join(RADIANT_ROOT, 'app', 'views')
end
def default_controller_paths
[File.join(RADIANT_ROOT, 'app', 'controllers')]
end
end
class Initializer < Rails::Initializer
def self.run(command = :process, configuration = Configuration.new)
Rails.configuration = configuration
super
end
def set_autoload_paths
extension_loader.add_extension_paths
super
end
# override Rails initializer to insert extension metals
def initialize_metal
Rails::Rack::Metal.requested_metals = configuration.metals
Rails::Rack::Metal.metal_paths = ["#{RADIANT_ROOT}/app/metal"] # reset Rails default to RADIANT_ROOT
Rails::Rack::Metal.metal_paths += plugin_loader.engine_metal_paths
Rails::Rack::Metal.metal_paths += extension_loader.metal_paths
configuration.middleware.insert_before(
:"ActionController::RewindableInput",
Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
end
def add_plugin_load_paths
# checks for plugins within extensions:
extension_loader.add_plugin_paths
super
end
def load_plugins
super
extension_loader.load_extensions
add_gem_load_paths
load_gems
check_gem_dependencies
end
def after_initialize
super
extension_loader.activate_extensions
configuration.check_extension_dependencies
end
def initialize_default_admin_tabs
admin.tabs.clear
admin.tabs.add "Pages", "/admin/pages"
admin.tabs.add "Snippets", "/admin/snippets"
admin.tabs.add "Layouts", "/admin/layouts", :visibility => [:admin, :developer]
end
def initialize_framework_views
view_paths = returning [] do |arr|
# Add the singular view path if it's not in the list
arr << configuration.view_path if !configuration.view_paths.include?(configuration.view_path)
# Add the default view paths
arr.concat configuration.view_paths
# Add the extension view paths
arr.concat extension_loader.view_paths
# Reverse the list so extensions come first
arr.reverse!
end
if configuration.frameworks.include?(:action_mailer) || defined?(ActionMailer::Base)
# This happens before the plugins are loaded so we must load it manually
unless ActionMailer::Base.respond_to? :view_paths
require "#{RADIANT_ROOT}/lib/plugins/extension_patches/lib/mailer_view_paths_extension"
end
ActionMailer::Base.view_paths = ActionView::Base.process_view_paths(view_paths)
end
if configuration.frameworks.include?(:action_controller) || defined?(ActionController::Base)
view_paths.each do |vp|
unless ActionController::Base.view_paths.include?(vp)
ActionController::Base.prepend_view_path vp
end
end
end
end
def initialize_routing
extension_loader.add_controller_paths
super
end
def extensions
@extensions ||= all_available_extensions
end
def all_available_extensions
extension_paths.map do |path|
Dir["#{path}/*"].select {|f| File.directory?(f) }
end.flatten.map {|f| File.basename(f).sub(/^\d+_/, '') }.sort.map(&:to_sym)
end
def admin
configuration.admin
end
def extension_loader
ExtensionLoader.instance {|l| l.initializer = self }
end
end
end