From 1e5fb868f685dca552d2fefe84863b273728bbae Mon Sep 17 00:00:00 2001 From: fatkodima Date: Thu, 31 Oct 2019 14:35:29 +0200 Subject: [PATCH] Auto include middleware for older railses --- README.md | 7 +------ lib/rack/attack/railtie.rb | 4 +--- spec/acceptance/rails_middleware_spec.rb | 21 +++------------------ 3 files changed, 5 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 86b92c4..738cb16 100644 --- a/README.md +++ b/README.md @@ -71,12 +71,7 @@ Or install it yourself as: Then tell your ruby web application to use rack-attack as a middleware. -a) For __rails__ applications with versions >= 5.1 it is used by default. For older rails versions you should enable it explicitly: -```ruby -# In config/application.rb - -config.middleware.use Rack::Attack -``` +a) For __rails__ applications it is used by default. You can disable it permanently (like for specific environment) or temporarily (can be useful for specific test cases) by writing: diff --git a/lib/rack/attack/railtie.rb b/lib/rack/attack/railtie.rb index 398ac6c..234e126 100644 --- a/lib/rack/attack/railtie.rb +++ b/lib/rack/attack/railtie.rb @@ -4,9 +4,7 @@ module Rack class Attack class Railtie < ::Rails::Railtie initializer "rack-attack.middleware" do |app| - if Gem::Version.new(::Rails::VERSION::STRING) >= Gem::Version.new("5.1") - app.middleware.use(Rack::Attack) - end + app.middleware.use(Rack::Attack) end end end diff --git a/spec/acceptance/rails_middleware_spec.rb b/spec/acceptance/rails_middleware_spec.rb index 8e7b014..0e14e89 100644 --- a/spec/acceptance/rails_middleware_spec.rb +++ b/spec/acceptance/rails_middleware_spec.rb @@ -12,24 +12,9 @@ if defined?(Rails) end end - if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new("5.1") - it "is used by default" do - @app.initialize! - assert_equal 1, @app.middleware.count(Rack::Attack) - end - - it "is not added when it was explicitly deleted" do - @app.config.middleware.delete(Rack::Attack) - @app.initialize! - refute @app.middleware.include?(Rack::Attack) - end - end - - if Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new("5.1") - it "is not used by default" do - @app.initialize! - assert_equal 0, @app.middleware.count(Rack::Attack) - end + it "is used by default" do + @app.initialize! + assert @app.middleware.include?(Rack::Attack) end end end