Running with Ruby

Tag: Sinatra (page 1 of 5)

Removing a particular middleware from Sinatra based Ruby application

From time to time, you may want to remove a particular middleware from your Sinatra application. Normally I would not recommend this, but when you test stuff and play around there might be no other way. It is also useful when a middleware contains bugs that make it unusable in your particular case. Removing it is really easy. Only thing you need to know is the name of the middleware you want to remove:

Sidekiq::Web.instance_variable_get(:@middleware).delete_if do |middleware|
  middleware.first == Rack::Protection # Or any other middleware name
end

Sinatra and Kaminari without any views and without padrino-helpers gem

If you have an app that doesn’t have any views (for example it only responds with JSON) you still may want to use excellent Kaminari gem for paginating big data sets. Unfortunately even when you work only with JSON and you don’t need any views helpers, you will still get this warning:

[!]You should install `padrino-helpers' gem if you want
to use kaminari's pagination helpers with Sinatra.
[!]Kaminari::Helpers::SinatraHelper does nothing now...

so to get rid of this, you have to add:

gem 'padrino-helpers'

to your Gemfile (even when you don’t need it).

Luckily, there’s a better way to do this. Add Kaminari to your Gemfile that way:

gem 'kaminari', require: %w( kaminari kaminari/hooks )

and then, either create a config/initializers/kaminari.rb or directly in your app.rb put this:

# Initialize the Kaminari gem
::Kaminari::Hooks.init

This will initialize Kaminari without requesting padrino-helpers.

Olderposts

Copyright © 2017 Running with Ruby

Theme by Anders NorenUp ↑