Instruction file imported from Hermes-SRV/hermes (
.github/instructions/plugins-initializers-setup-hooks.instructions.md). Copyright stays with the author.
Plugin Initialization System
Purpose
Load and initialize plugins using Rails Railtie pattern with single entry point per plugin.
Plugin Initializer
File: plugins/{plugin-name}/initializer.rb (REQUIRED)
Template
# frozen_string_literal: true
# Phase 1: Autoload
ActiveSupport::Dependencies.autoload_paths << File.expand_path('app/models', __dir__)
ActiveSupport::Dependencies.autoload_paths << File.expand_path('app/controllers', __dir__)
# Phase 2: Routes
routes_file = File.expand_path('routes.rb', __dir__)
Rails.application.routes_reloader.paths << routes_file if File.exist?(routes_file)
# Phase 3: Initialization
Rails.application.config.to_prepare do
require_relative 'lib/my_library'
require_relative 'config/initializers/my_feature'
plugin_views_path = File.expand_path('app/views', __dir__)
if File.exist?(plugin_views_path)
ActionController::Base.prepend_view_path(plugin_views_path)
Rails.logger.info "[MY-PLUGIN] ✅ Initialized"
end
end
Rules
✅ DO
- Use
Rails.application.config.to_preparefor loading code - Use
require_relativefor plugin files - Check file existence before loading
❌ DON'T
- Modify core files
- Use nested
to_prepareblocks - Use
requirewithout full path