Skip to content

Easy8 initialisation order

The order in which Easy8 initialises. Use it to choose the right place for your initialisation code.

  1. Engine Initializers #1
  2. code in lib/EngineName/engine.rb, e.g. lib/easy_b2b/engine.rb
  3. code that is not in any later blocks

  4. Rails application initializers

  5. config/initializers/*
  6. code that is not in any of the later blocks

  7. Engines Initializers #2

  8. initializer code blocks from lib/EngineName/engine.rb

    class Engine < ::Rails::Engine
      initializer 'My initializer' do
        # Custom initializer
      end
    end
    
  9. Engines Initializers #3

  10. config/initializers/*

  11. Rails.application.config.to_prepare blocks

  12. These are run on every code reload in development mode
  13. This block runs before patches are applied !!

  14. Railtie.config.to_prepare blocks

  15. These are run on every code reload in development mode
  16. 30-redmine.rb registers a block that runs Redmine::Preparation.prepare and the after_plugins_loaded hooks. 33-easy_patches.rb applies the Easy patches from an after_plugins_loaded hook.
  17. EasyB2b::Engine.config.to_prepare runs after the plugin application if it is defined in easy_b2b/config/initializers, and before patches if it is defined in easy_b2b/lib/easy_b2b/engine.rb

  18. Rails.application.config.after_initialize blocks

Easy plugins (including your modification_<name> plugin) are loaded by EasyPluginLoader, which easy_engines/easy_extensions/config/initializers/x_easy_plugin_loader.rb calls from the initializers of the easy_extensions engine. It requires lib/<plugin_name>.rb and then every file in the plugin's config/initializers/, so plugin initializers run after the initializers of easy_extensions and before those of the other engines.

Example

1.

# my_engine/lib/my_engine/engine.rb
class Engine < ::Rails::Engine
  puts "This runs right away"
end

2.

# Rails.root/config/initializers/30-redmine.rb
puts "This is where plugins/PLUGIN_NAME/app/* + lib is added to autoload_paths"

3.

# my_engine/lib/my_engine/engine.rb
class Engine < ::Rails::Engine
  initializer do
    puts "Enqueued but runs very early"
  end
end

4.

# my_engine/config/initializers/03_hooks.rb
puts "Enqueued but runs very early"

5.

# Rails.root/config/initializers/00-core_plugins.rb
Rails.application.config.to_prepare do
  puts "This is before easy patch application"
end

6.

# my_engine/config/initializers/03_hooks.rb
Rails.application.config.to_prepare do
  puts "This is before easy patch application"
end

7.

# my_engine/lib/my_engine/engine.rb
class Engine < ::Rails::Engine
  config.to_prepare do
    puts "This is still before easy patch application"
  end
end

8.

# Rails.root/config/initializers/30-redmine.rb
Rails.application.config.to_prepare do
  puts "In this block the after_plugins_loaded hooks run"
end
# Rails.root/config/initializers/33-easy_patches.rb
Rails.application.config.to_prepare do
  puts "In this block we apply easy_patches"
end

9.

# my_engine/config/initializers/03_hooks.rb
MyEngine::Engine.config.to_prepare do
  puts "Runs as #1 out of these 3"
end
Rails.application.config.to_prepare do
  puts "Runs as #2 out of these 3"
end
MyEngine::Engine.config.to_prepare do
  puts "Runs as #3 out of these 3"
end

10.

# my_engine/lib/my_engine/engine.rb
class Engine < ::Rails::Engine
  config.after_initialize do
    puts "This is the first after_initialize block"
  end
end

11.

# Rails.root/config/initializers/0-easy.rb
Rails.application.config.after_initialize do
  puts "After initialize block from core app"
end

12.

# my_engine/config/initializers/03_hooks.rb
Rails.application.config.after_initialize do
  puts "After initialize block from engine initializer files"
end