mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Use a NullFlash object when the session store is disabled
The module `EtagWithFlash` (introduced in #26250) uses flash. However, when the session store is not set (or disabled), it will raise an error because the flash middleware is included only if a session store is present. This patch includes the middleware even when the session store is disabled, although the flash module will return a null object to prevent breaks.
This commit is contained in:
parent
d89d14d241
commit
adc7cb2d36
3 changed files with 33 additions and 1 deletions
|
@ -45,6 +45,7 @@ module ActionDispatch
|
||||||
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
|
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
|
||||||
# to put a new one.
|
# to put a new one.
|
||||||
def flash
|
def flash
|
||||||
|
return Flash::NullFlash unless session.respond_to?(:loaded?)
|
||||||
flash = flash_hash
|
flash = flash_hash
|
||||||
return flash if flash
|
return flash if flash
|
||||||
self.flash = Flash::FlashHash.from_session_value(session["flash"])
|
self.flash = Flash::FlashHash.from_session_value(session["flash"])
|
||||||
|
@ -79,6 +80,20 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module NullFlash #:nodoc:
|
||||||
|
class << self
|
||||||
|
def []=(k, v); end
|
||||||
|
|
||||||
|
def [](k); end
|
||||||
|
|
||||||
|
def alert=(message); end
|
||||||
|
|
||||||
|
def notice=(message); end
|
||||||
|
|
||||||
|
def empty?; end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class FlashNow #:nodoc:
|
class FlashNow #:nodoc:
|
||||||
attr_accessor :flash
|
attr_accessor :flash
|
||||||
|
|
||||||
|
|
|
@ -67,10 +67,10 @@ module Rails
|
||||||
config.session_options[:secure] = true
|
config.session_options[:secure] = true
|
||||||
end
|
end
|
||||||
middleware.use config.session_store, config.session_options
|
middleware.use config.session_store, config.session_options
|
||||||
middleware.use ::ActionDispatch::Flash
|
|
||||||
end
|
end
|
||||||
|
|
||||||
unless config.api_only
|
unless config.api_only
|
||||||
|
middleware.use ::ActionDispatch::Flash
|
||||||
middleware.use ::ActionDispatch::ContentSecurityPolicy::Middleware
|
middleware.use ::ActionDispatch::ContentSecurityPolicy::Middleware
|
||||||
middleware.use ::ActionDispatch::PermissionsPolicy::Middleware
|
middleware.use ::ActionDispatch::PermissionsPolicy::Middleware
|
||||||
end
|
end
|
||||||
|
|
|
@ -582,6 +582,23 @@ module ApplicationTests
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "EtagWithFlash module doesn't break when the session store is disabled" do
|
||||||
|
make_basic_app do |application|
|
||||||
|
application.config.session_store :disabled
|
||||||
|
end
|
||||||
|
|
||||||
|
class ::OmgController < ActionController::Base
|
||||||
|
def index
|
||||||
|
stale?(weak_etag: "something")
|
||||||
|
render plain: "else"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
get "/"
|
||||||
|
|
||||||
|
assert last_response.ok?
|
||||||
|
end
|
||||||
|
|
||||||
test "Use key_generator when secret_key_base is set" do
|
test "Use key_generator when secret_key_base is set" do
|
||||||
make_basic_app do |application|
|
make_basic_app do |application|
|
||||||
application.secrets.secret_key_base = "b3c631c314c0bbca50c1b2843150fe33"
|
application.secrets.secret_key_base = "b3c631c314c0bbca50c1b2843150fe33"
|
||||||
|
|
Loading…
Reference in a new issue