Class: Findbug::Railtie
- Inherits:
-
Rails::Railtie
- Object
- Rails::Railtie
- Findbug::Railtie
- Defined in:
- lib/findbug/railtie.rb
Overview
Railtie hooks Findbug into the Rails boot process.
WHAT IS A RAILTIE?
When Rails boots, it looks for classes that inherit from Rails::Railtie and calls their initializers in order. This is how gems integrate with Rails.
Common things Railties do:
-
Insert middleware into the stack
-
Subscribe to ActiveSupport::Notifications
-
Add rake tasks
-
Configure the Rails app
WHY USE A RAILTIE?
Instead of making users add Findbug to 5 different places:
# application.rb
config.middleware.use Findbug::Capture::Middleware
# initializer
ActiveSupport::Notifications.subscribe(...)
# routes
mount Findbug::Web::Engine => "/findbug"
We do it all automatically in the Railtie. User just adds the gem and creates a config file. Zero setup!
THE INITIALIZATION ORDER
Rails runs initializers in stages:
-
before_configuration - Before config is read
-
before_initialize - Before Rails.initialize!
-
to_prepare - Before each request (dev) or once (prod)
-
after_initialize - After Rails is fully loaded
We use after_initialize because we need:
-
Rails.env to be set
-
Database connections to exist
-
All models to be loaded