Class: Radiant::Initializer

Inherits:
Rails::Initializer
  • Object
show all
Defined in:
lib/radiant/initializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(command = :process, configuration = Configuration.new) ⇒ Object

Rails::Initializer is essentially a list of startup steps and we extend it here by:

  • overriding or extending some of those steps so that they use radiant and extension paths as well as (or instead of) the rails defaults.

  • appending some extra steps to set up the admin UI and activate extensions



264
265
266
267
# File 'lib/radiant/initializer.rb', line 264

def self.run(command = :process, configuration = Configuration.new) #:nodoc
  Rails.configuration = configuration
  super
end

Instance Method Details

#add_plugin_load_pathsObject

Extends the Rails initializer to add plugin paths in extensions and makes extension load paths reloadable (eg in development mode)



313
314
315
316
317
# File 'lib/radiant/initializer.rb', line 313

def add_plugin_load_paths
  configuration.add_plugin_paths(extension_loader.paths(:plugin))
  super
  ActiveSupport::Dependencies.autoload_once_paths -= extension_loader.paths(:load)
end

#adminObject

Returns the Radiant::AdminUI singleton so that the initializer can set up the admin interface.



410
411
412
# File 'lib/radiant/initializer.rb', line 410

def admin
  configuration.admin
end

#after_initializeObject

Extends the Rails initializer with some extra steps at the end of initialization:

  • hook up radiant view paths in controllers and notifiers

  • initialize the navigation tabs in the admin interface

  • initialize the extendable partial sets that make up the admin interface

  • call activate on all radiant extensions

  • add extension controller paths

  • mark extension app paths for eager loading



363
364
365
366
367
368
# File 'lib/radiant/initializer.rb', line 363

def after_initialize
  super
  extension_loader.activate_extensions  # also calls initialize_views
  configuration.add_controller_paths(extension_loader.paths(:controller))
  configuration.add_eager_load_paths(extension_loader.paths(:eager_load))
end

#deployed_as_app?Boolean

Returns true in the very unusual case where radiant has been deployed as a rails app itself, rather than loaded as a gem or from vendor/. This is only likely in situations where radiant is customised so heavily that extensions are not sufficient.

Returns:

  • (Boolean)


273
274
275
# File 'lib/radiant/initializer.rb', line 273

def deployed_as_app?
  RADIANT_ROOT == RAILS_ROOT
end

#extension_loaderObject

Returns the ExtensionLoader singleton that will eventually load extensions.



416
417
418
# File 'lib/radiant/initializer.rb', line 416

def extension_loader
  ExtensionLoader.instance {|l| l.initializer = self }
end

#initialize_default_admin_tabsObject

Initializes the core admin tabs. Separate so that it can be invoked by itself in tests.



381
382
383
# File 'lib/radiant/initializer.rb', line 381

def initialize_default_admin_tabs
  admin.initialize_nav
end

#initialize_framework_viewsObject

This adds extension view paths to the standard Rails::Initializer method. In environments that don’t cache templates it reloads the path set on each request, so that new extension paths are noticed without a restart.



389
390
391
392
393
394
395
396
397
# File 'lib/radiant/initializer.rb', line 389

def initialize_framework_views
  view_paths = extension_loader.paths(:view).push(configuration.view_path)
  if ActionController::Base.view_paths.blank? || !ActionView::Base.cache_template_loading?
    ActionController::Base.view_paths = ActionView::Base.process_view_paths(view_paths)
  end
  if configuration.frameworks.include?(:action_mailer) && ActionMailer::Base.view_paths.blank? || !ActionView::Base.cache_template_loading?
    ActionMailer::Base.view_paths = ActionView::Base.process_view_paths(view_paths) if configuration.frameworks.include?(:action_mailer)
  end
end

#initialize_i18nObject

Extends the Rails initializer to add locale paths from RADIANT_ROOT and from radiant extensions.



304
305
306
307
308
# File 'lib/radiant/initializer.rb', line 304

def initialize_i18n
  radiant_locale_paths = Dir[File.join(RADIANT_ROOT, 'config', 'locales', '*.{rb,yml}')]
  configuration.i18n.load_path = radiant_locale_paths + extension_loader.paths(:locale)
  super
end

#initialize_metalObject

Overrides the Rails initializer to load metal from RADIANT_ROOT and from radiant extensions.



290
291
292
293
294
295
296
297
298
299
300
# File 'lib/radiant/initializer.rb', line 290

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.paths(:metal)
  Rails::Rack::Metal.metal_paths.uniq!

  configuration.middleware.insert_before(
    :"ActionController::ParamsParser",
    Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
end

#initialize_routingObject

Extends the Rails initializer to make sure that extension controller paths are available when routes are initialized.



402
403
404
405
406
# File 'lib/radiant/initializer.rb', line 402

def initialize_routing
  configuration.add_controller_paths(extension_loader.paths(:controller))
  configuration.add_eager_load_paths(extension_loader.paths(:eager_load))
  super
end

#initialize_viewsObject

Initializes all the admin interface elements and views. Separate here so that it can be called to reset the interface before extension (re)activation.



373
374
375
376
377
# File 'lib/radiant/initializer.rb', line 373

def initialize_views
  initialize_default_admin_tabs
  initialize_framework_views
  admin.load_default_regions
end

#load_application_initializersObject

Extends the Rails initializer to run initializers from radiant and from extensions. The load order will be:

  1. RADIANT_ROOT/config/intializers/*.rb

  2. RAILS_ROOT/config/intializers/*.rb

  3. config/initializers/*.rb found in extensions, in extension load order.

In the now rare case where radiant is deployed as an ordinary rails application, step 1 is skipped because it is equivalent to step 2.



341
342
343
344
345
# File 'lib/radiant/initializer.rb', line 341

def load_application_initializers
  load_radiant_initializers unless deployed_as_app?
  super
  extension_loader.load_extension_initalizers
end

#load_gemsObject

Overrides the standard gem-loader to use Bundler instead of config.gem. This is the method normally monkey-patched into Rails::Initializer from boot.rb if you follow the instructions at gembundler.com/rails23.html



322
323
324
# File 'lib/radiant/initializer.rb', line 322

def load_gems
  @bundler_loaded ||= Bundler.require :default, Rails.env
end

#load_pluginsObject

Extends the Rails initializer also to load radiant extensions (which have been excluded from the list of plugins).



328
329
330
331
# File 'lib/radiant/initializer.rb', line 328

def load_plugins
  super
  extension_loader.load_extensions
end

#load_radiant_initializersObject

Loads initializers found in RADIANT_ROOT/config/initializers.



349
350
351
352
353
# File 'lib/radiant/initializer.rb', line 349

def load_radiant_initializers
  Dir["#{RADIANT_ROOT}/config/initializers/**/*.rb"].sort.each do |initializer|
    load(initializer)
  end
end

#set_autoload_pathsObject

Extends the Rails::Initializer default to add extension paths to the autoload list. Note that default_autoload_paths is also overridden to point to RADIANT_ROOT.



280
281
282
283
284
285
286
# File 'lib/radiant/initializer.rb', line 280

def set_autoload_paths
  extension_loader.paths(:load).reverse_each do |path|
    configuration.autoload_paths.unshift path
    $LOAD_PATH.unshift path
  end
  super
end