I am trying to find out how a gem in the Rails 3 gemfile can automatically add middleware to the Rack stack. I am looking for the hook in that gem. For example ... when I add the devise gem to my Rails 3 gemfile, then devise somehow adds warden as middleware on the Rack stack. This seems to work automatically. There is no further configuration needed in the Rails 3 app. I guess there is automatically called a special class/method from boot.rb. Any hints how this process really works?
You should use a Railtie. In fact, this is the very example given in the Rails::Railtie documentation.
class MyRailtie < Rails::Railtie
initializer "my_railtie.configure_rails_initialization" do |app|
app.middleware.use MyRailtie::Middleware
end
end
To insert middleware in a gem, you should add it to the gem's engine.
in lib/gem_name/engine.rb
require 'rails'
module GemName
class Engine < Rails::Engine
config.app_middleware.insert_before(Warden::Manager, Rack::OpenID)
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With