Automatically add gemstone rack middleware

I have a gem that provides some middleware for the rack, the only way to make it work is to put this in my .rb application

config.middleware.use "TBBC::Editor::Middleware" 

How can I make it so that this middleware is automatically used when my gem is used in the gemfile app?

+6
source share
1 answer

If you intend to use your gem with Rails 3, you can provide Railtie . Then you can automatically download it if you use Rails.

Assuming the name of your gem is tbbc , put this in lib/tbbc/railtie.rb :

 module TBBC class Railtie < Rails::Railtie initializer "tbbc.insert_middleware" do |app| app.config.middleware.use "TBBC::Editor::Middleware" end end end 

In lib/tbbc.rb :

 require "tbbc/railtie" if defined? Rails 

You cannot automatically add middleware to common Rack applications. For applications other than Rails, this will be what the user should do.

+12
source

Source: https://habr.com/ru/post/886308/


All Articles