Class: TrustyCms::ExtensionLoader

Inherits:
Object
  • Object
show all
Includes:
Simpleton
Defined in:
lib/trusty_cms/extension_loader.rb

Defined Under Namespace

Classes: DependenciesObserver

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Simpleton

included

Constructor Details

#initializeExtensionLoader

:nodoc



34
35
36
# File 'lib/trusty_cms/extension_loader.rb', line 34

def initialize #:nodoc
  self.extensions = []
end

Instance Attribute Details

#extensionsObject

Returns the value of attribute extensions.



32
33
34
# File 'lib/trusty_cms/extension_loader.rb', line 32

def extensions
  @extensions
end

#initializerObject

Returns the value of attribute initializer.



32
33
34
# File 'lib/trusty_cms/extension_loader.rb', line 32

def initializer
  @initializer
end

Class Method Details

.record_path(path, name = nil) ⇒ Object

Builds an ExtensionPath object from the supplied path, working out the name of the extension on the way. The ExtensionPath object will later be used to scan and load the extension. An extension name can be supplied in addition to the path. It will be processed in the usual way to remove trusty- and -extension and any verion numbering.



125
126
127
# File 'lib/trusty_cms/extension_loader.rb', line 125

def record_path(path, name = nil)
  ExtensionPath.from_path(path, name)
end

Instance Method Details

#activate_extensionsObject Also known as: reactivate

Activates all enabled extensions and makes sure that any newly declared subclasses of Page are recognised. The admin UI and views have to be reinitialized each time to pick up changes and avoid duplicates.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/trusty_cms/extension_loader.rb', line 100

def activate_extensions
  initializer.initialize_views
  ordered_extensions = []
  configuration = TrustyCms::Application.config
  if configuration.extensions.first == :all
    ordered_extensions = extensions
  else
    configuration.extensions.each { |name| ordered_extensions << select_extension(name) }
  end
  ordered_extensions.flatten.each(&:activate)
  Page.load_subclasses
end

#deactivate_extensionsObject

Deactivates all enabled extensions.



93
94
95
# File 'lib/trusty_cms/extension_loader.rb', line 93

def deactivate_extensions
  extensions.each(&:deactivate)
end

#enabled_extension_pathsObject

Returns a list of paths to all the extensions that are enabled in the configuration of this application.



40
41
42
# File 'lib/trusty_cms/extension_loader.rb', line 40

def enabled_extension_paths
  ExtensionPath.enabled.map(&:to_s)
end

#load_extension(name) ⇒ Object

Loads the specified extension.



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/trusty_cms/extension_loader.rb', line 71

def load_extension(name)
  extension_path = ExtensionPath.find(name)
  begin
    constant = "#{name}_extension".camelize
    extension = constant.constantize
    extension.unloadable
    extension.path = extension_path
    extension
  rescue LoadError, NameError => e
    warn "Could not load extension: #{name}.\n#{e.inspect}"
    nil
  end
end

#load_extension_initalizersObject

Loads all the initializers defined in enabled extensions, in the configured order.



87
88
89
# File 'lib/trusty_cms/extension_loader.rb', line 87

def load_extension_initalizers
  extensions.each(&:load_initializers)
end

#load_extensionsObject

Loads but does not activate all the extensions that have been enabled, in the configured order (which defaults to alphabetically). If an extension fails to load an error will be logged but application startup will continue. If an extension doesn’t exist, a LoadError will be raised and startup will halt.



63
64
65
66
67
# File 'lib/trusty_cms/extension_loader.rb', line 63

def load_extensions
  configuration = TrustyCms::Application.config
  @observer ||= DependenciesObserver.new(configuration).observe(::ActiveSupport::Dependencies)
  self.extensions = configuration.enabled_extensions.map { |ext| load_extension(ext) }.compact
end

#paths(type) ⇒ Object

Returns a list of all the paths discovered within extension roots of the specified type. (by calling the corresponding class method on ExtensionPath).

extension_loader.paths(:metal)         #=> ['extension/app/metal', 'extension/app/metal']
extension_loader.paths(:controller)    #=> ['extension/app/controllers', 'extension/app/controllers']
extension_loader.paths(:eager_load)    #=> ['extension/app/controllers', 'extension/app/models', 'extension/app/helpers']

For compatibility with the old loader, there are corresponding type_paths methods. There are also (deprecated) add_type_paths methods.



54
55
56
# File 'lib/trusty_cms/extension_loader.rb', line 54

def paths(type)
  ExtensionPath.send("#{type}_paths".to_sym)
end

#select_extension(name) ⇒ Object



113
114
115
# File 'lib/trusty_cms/extension_loader.rb', line 113

def select_extension(name)
  extensions.select { |ext| ext.extension_name.symbolize == name }
end