Class: Rails::Application::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/configuration_extensions/configuration_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#extension_pathsObject

The TrustyCms::Configuration class extends Rails::Configuration with three purposes:

  • to reset some rails defaults so that files are found in TRUSTY_CMS_ROOT instead of Rails.root

  • to notice that some gems and plugins are in fact trusty extensions

  • to notice that some trusty extensions add load paths (for plugins, controllers, metal, etc)



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

def extension_paths
  @extension_paths
end

#ignored_extensionsObject

The TrustyCms::Configuration class extends Rails::Configuration with three purposes:

  • to reset some rails defaults so that files are found in TRUSTY_CMS_ROOT instead of Rails.root

  • to notice that some gems and plugins are in fact trusty extensions

  • to notice that some trusty extensions add load paths (for plugins, controllers, metal, etc)



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

def ignored_extensions
  @ignored_extensions
end

Instance Method Details

#adminObject

Returns the AdminUI singleton, giving get-and-set access to the tabs and partial-sets it defines. More commonly accessed in the initializer via its call to configuration.admin.



238
239
240
# File 'lib/configuration_extensions/configuration_extensions.rb', line 238

def admin
  TrustyCms::AdminUI.instance
end

#available_extensionsObject

Returns an alphabetical list of every extension found among all the load paths and bundled gems. Any plugin or gem whose path ends in the form trusty-something-extension is considered to be an extension.

TrustyCms.configuration.available_extensions  # => [:name, :name, :name, :name]

This method is always called during initialization, either as a default or to check that specified extensions are available. One of its side effects is to populate the ExtensionLoader’s list of extension root locations, later used when activating those extensions that have been enabled.



189
190
191
# File 'lib/configuration_extensions/configuration_extensions.rb', line 189

def available_extensions
  @available_extensions ||= (vendored_extensions + gem_extensions).uniq.sort.map(&:to_sym)
end

#default_extension_pathsObject

Sets the locations in which we look for vendored extensions. Normally:

Rails.root/vendor/extensions
TrustyCms.root/vendor/extensions

There are no vendor/* directories in TRUSTY_CMS_ROOT any more but the possibility remains for compatibility reasons. In test mode we also add a fixtures path for testing the extension loader.



101
102
103
104
105
106
107
# File 'lib/configuration_extensions/configuration_extensions.rb', line 101

def default_extension_paths
  env = ENV["RAILS_ENV"] || Rails.env
  paths = [Rails.root + 'vendor/extensions']
  paths.unshift(TRUSTY_CMS_ROOT + "/vendor/extensions") unless Rails.root == TRUSTY_CMS_ROOT
  paths.unshift(TRUSTY_CMS_ROOT + "test/fixtures/extensions") if env =~ /test/
  paths
end

#enabled_extensionsObject

The list of extensions, expanded and in load order, that results from combining all the extension configuration directives. These are the extensions that will actually be loaded or migrated, and for most purposes this is the list you want to refer to.

TrustyCms.configuration.enabled_extensions  # => [:name, :name, :name, :name]

Note that an extension enabled is not the same as an extension activated or even loaded: it just means that the application is configured to load that extension.



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

def enabled_extensions
  @enabled_extensions ||= expanded_extension_list - ignored_extensions
end

#expand_and_check(extension_list) ⇒ Object

Raises:

  • (LoadError)


135
136
137
138
139
140
141
142
143
# File 'lib/configuration_extensions/configuration_extensions.rb', line 135

def expand_and_check(extension_list)
  #:nodoc
  missing_extensions = extension_list - [:all] - available_extensions
  raise LoadError, "These configured extensions have not been found: #{missing_extensions.to_sentence}" if missing_extensions.any?
  if m = extension_list.index(:all)
    extension_list[m] = available_extensions - extension_list
  end
  extension_list.flatten
end

#expanded_extension_listObject

The expanded and ordered list of extensions, including any that may later be ignored. This can be configured (it is here that the :all entry is expanded to mean ‘everything else’), or will default to an alphabetical list of every extension found among gems and vendor/extensions directories.

TrustyCms.configuration.expanded_extension_list  # => [:name, :name, :name, :name]

If an extension in the configurted list is not found, a LoadError will be thrown from here.



130
131
132
133
# File 'lib/configuration_extensions/configuration_extensions.rb', line 130

def expanded_extension_list
  # NB. it should remain possible to say config.extensions = []
  @extension_list ||= extensions ? expand_and_check(extensions) : available_extensions
end

#extension(ext) ⇒ Object

Old extension-dependency mechanism now deprecated



224
225
226
# File 'lib/configuration_extensions/configuration_extensions.rb', line 224

def extension(ext)
  ::ActiveSupport::Deprecation.warn("Extension dependencies have been deprecated and are no longer supported in trusty 1.0. Extensions with dependencies should be packaged as gems and use the .gemspec to declare them.", caller)
end

#extensionsObject

Without such a call, we default to the alphabetical list of all well-formed vendor and gem extensions returned by available_extensions.

TrustyCms.configuration.extensions  # => [:name, :all, :name]


153
154
155
# File 'lib/configuration_extensions/configuration_extensions.rb', line 153

def extensions
  @requested_extensions ||= available_extensions
end

#extensions=(extensions) ⇒ Object

Sets the list of extensions that will be loaded and the order in which to load them. It can include an :all marker to mean ‘everything else’ and is typically set in environment.rb:

config.extensions = [:layouts, :taggable, :all]
config.extensions = [:dashboard, :blog, :all]
config.extensions = [:dashboard, :blog, :all, :comments]

A LoadError is raised if any of the specified extensions can’t be found.



165
166
167
# File 'lib/configuration_extensions/configuration_extensions.rb', line 165

def extensions=(extensions)
  @requested_extensions = extensions
end

#gem(name, options = {}) ⇒ Object

Old gem-invogation method now deprecated



230
231
232
233
# File 'lib/configuration_extensions/configuration_extensions.rb', line 230

def gem(name, options = {})
  ::ActiveSupport::Deprecation.warn("Please declare gem dependencies in your Gemfile (or for an extension, in the .gemspec file).", caller)
  super
end

#gem_extensionsObject

Scans the bundled gems for any whose name match the trusty-something-extension format and returns a list of their names as symbols.

TrustyCms.configuration.gem_extensions  # => [:name, :name]


213
214
215
216
217
218
219
220
# File 'lib/configuration_extensions/configuration_extensions.rb', line 213

def gem_extensions
  Gem.loaded_specs.each_with_object([]) do |(gemname, gemspec), found|
    if gemname =~ /trusty-.*-extension$/
      ep = TrustyCms::ExtensionLoader.record_path(gemspec.full_gem_path, gemname)
      found << ep.name
    end
  end
end

#ignore_extensions(array) ⇒ Object

This is a configurable list of extension that should not be loaded.

config.ignore_extensions = [:experimental, :broken]

You can also retrieve the list with ignored_extensions:

TrustyCms.configuration.ignored_extensions  # => [:experimental, :broken]

These exclusions are applied regardless of dependencies and extension locations. A configuration that bundles required extensions then ignores them will not boot and is likely to fail with errors about unitialized constants.



176
177
178
# File 'lib/configuration_extensions/configuration_extensions.rb', line 176

def ignore_extensions(array)
  self.ignored_extensions |= array
end

#initialize_extension_pathsObject



90
91
92
93
# File 'lib/configuration_extensions/configuration_extensions.rb', line 90

def initialize_extension_paths
  self.extension_paths = default_extension_paths
  self.ignored_extensions = []
end

#vendored_extensionsObject

Searches the defined extension_paths for subdirectories and returns a list of names as symbols.

TrustyCms.configuration.vendored_extensions  # => [:name, :name]


197
198
199
200
201
202
203
204
205
206
# File 'lib/configuration_extensions/configuration_extensions.rb', line 197

def vendored_extensions
  extension_paths.each_with_object([]) do |load_path, found|
    Dir["#{load_path}/*"].each do |path|
      if File.directory?(path)
        ep = TrustyCms::ExtensionLoader.record_path(path)
        found << ep.name
      end
    end
  end
end