Module: TrustyCms::Initializer

Defined in:
lib/trusty_cms/initializer.rb

Instance Method Summary collapse

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)



69
70
71
72
73
# File 'lib/trusty_cms/initializer.rb', line 69

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 TrustyCms::AdminUI singleton so that the initializer can set up the admin interface.



151
152
153
# File 'lib/trusty_cms/initializer.rb', line 151

def admin
  TrustyCms::Application.config.admin
end

#after_initializeObject

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

  • hook up trusty 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 trusty extensions

  • add extension controller paths

  • mark extension app paths for eager loading



119
120
121
122
123
124
# File 'lib/trusty_cms/initializer.rb', line 119

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

#deployed_as_app?Boolean

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

Returns:

  • (Boolean)


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

def deployed_as_app?
  TRUSTY_CMS_ROOT == Rails.root
end

#extension_loaderObject

Returns the ExtensionLoader singleton that will eventually load extensions.



157
158
159
# File 'lib/trusty_cms/initializer.rb', line 157

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.



136
137
138
# File 'lib/trusty_cms/initializer.rb', line 136

def initialize_default_admin_tabs
  admin.initialize_nav
end

#initialize_i18nObject

Extends the Rails initializer to add locale paths from TRUSTY_CMS_ROOT and from trusty extensions.



60
61
62
63
64
# File 'lib/trusty_cms/initializer.rb', line 60

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

#initialize_metalObject

Overrides the Rails initializer to load metal from TRUSTY_CMS_ROOT and from trusty extensions.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/trusty_cms/initializer.rb', line 45

def initialize_metal
  Rails::Rack::Metal.requested_metals = configuration.metals
  Rails::Rack::Metal.metal_paths = ["#{TRUSTY_CMS_ROOT}/app/metal"] # reset Rails default to TRUSTY_CMS_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.



143
144
145
146
147
# File 'lib/trusty_cms/initializer.rb', line 143

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.



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

def initialize_views
  initialize_default_admin_tabs
  admin.load_default_regions
end

#load_application_initializersObject

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

  1. TRUSTY_CMS_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 trusty is deployed as an ordinary rails application, step 1 is skipped because it is equivalent to step 2.



97
98
99
100
101
# File 'lib/trusty_cms/initializer.rb', line 97

def load_application_initializers
  load_trusty_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



78
79
80
# File 'lib/trusty_cms/initializer.rb', line 78

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

#load_pluginsObject

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



84
85
86
87
# File 'lib/trusty_cms/initializer.rb', line 84

def load_plugins
  super
  extension_loader.load_extensions
end

#load_trusty_initializersObject

Loads initializers found in TRUSTY_CMS_ROOT/config/initializers.



105
106
107
108
109
# File 'lib/trusty_cms/initializer.rb', line 105

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

#set_autoload_patfObject

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



39
40
41
# File 'lib/trusty_cms/initializer.rb', line 39

def set_autoload_patf
  super
end