diff --git a/lib/rails_admin/engine.rb b/lib/rails_admin/engine.rb index 1c162d0891..bbe01b2673 100644 --- a/lib/rails_admin/engine.rb +++ b/lib/rails_admin/engine.rb @@ -24,11 +24,6 @@ class Engine < Rails::Engine end initializer 'RailsAdmin setup middlewares' do |app| - app.config.session_store :cookie_store - app.config.middleware.use ActionDispatch::Cookies - app.config.middleware.use ActionDispatch::Flash - app.config.middleware.use ActionDispatch::Session::CookieStore, app.config.session_options - app.config.middleware.use Rack::MethodOverride app.config.middleware.use Rack::Pjax end @@ -47,5 +42,24 @@ class Engine < Rails::Engine rake_tasks do Dir[File.join(File.dirname(__FILE__), '../tasks/*.rake')].each { |f| load f } end + + initializer 'RailsAdmin check for required middlewares', after: :build_middleware_stack do |app| + has_session_store = app.config.middleware.to_a.any? { |m| m.klass.try(:<=, ActionDispatch::Session::AbstractStore) } || ::Rails.version < '5.0' + loaded = app.config.middleware.to_a.map(&:name) + required = %w(ActionDispatch::Cookies ActionDispatch::Flash Rack::MethodOverride) + missing = required - loaded + unless missing.empty? && has_session_store + configs = missing.map { |m| "config.middleware.use #{m}" } + configs << "config.middleware.use #{app.config.session_store.try(:name) || 'ActionDispatch::Session::CookieStore'}, #{app.config.session_options}" unless has_session_store + raise <<-EOM +Required middlewares for RailsAdmin are not added +To fix tihs, add + + #{configs.join("\n ")} + +to config/application.rb. + EOM + end + end end end