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



37
38
39
# File 'lib/trusty_cms/extension_loader.rb', line 37

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

Instance Attribute Details

#extensionsObject

Returns the value of attribute extensions.



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

def extensions
  @extensions
end

#initializerObject

Returns the value of attribute initializer.



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

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.



130
131
132
# File 'lib/trusty_cms/extension_loader.rb', line 130

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.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/trusty_cms/extension_loader.rb', line 105

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.



98
99
100
# File 'lib/trusty_cms/extension_loader.rb', line 98

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.



43
44
45
# File 'lib/trusty_cms/extension_loader.rb', line 43

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

#load_extension(name) ⇒ Object

Loads the specified extension.



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

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

#load_extension_initalizersObject Also known as: load_extension_initializers

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



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

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.



66
67
68
69
# File 'lib/trusty_cms/extension_loader.rb', line 66

def load_extensions
  configuration = TrustyCms::Application.config
  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.



57
58
59
# File 'lib/trusty_cms/extension_loader.rb', line 57

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

#select_extension(name) ⇒ Object



118
119
120
# File 'lib/trusty_cms/extension_loader.rb', line 118

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