1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Remove ActionDispatch::Flash::NullFlash

After #42167, all apps (except api only ones) have access to the flash
module. If the session store is disabled, then an empty flash object
is used.

This patch also prevents the flash from being committed to the session
if this is not enabled.
This commit is contained in:
Ricardo Díaz 2021-05-18 21:26:24 -05:00
parent a34d64d82e
commit ca7c820c69
4 changed files with 11 additions and 20 deletions

View file

@ -199,6 +199,10 @@ module ActionController #:nodoc:
def exists? def exists?
true true
end end
def enabled?
false
end
end end
class NullCookieJar < ActionDispatch::Cookies::CookieJar #:nodoc: class NullCookieJar < ActionDispatch::Cookies::CookieJar #:nodoc:

View file

@ -211,6 +211,10 @@ module ActionController
@data.fetch(key.to_s, *args, &block) @data.fetch(key.to_s, *args, &block)
end end
def enabled?
true
end
private private
def load! def load!
@id @id

View file

@ -45,7 +45,6 @@ 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"])
@ -60,16 +59,14 @@ module ActionDispatch
end end
def commit_flash # :nodoc: def commit_flash # :nodoc:
session = self.session || {} return unless session.enabled?
flash_hash = self.flash_hash
if flash_hash && (flash_hash.present? || session.key?("flash")) if flash_hash && (flash_hash.present? || session.key?("flash"))
session["flash"] = flash_hash.to_session_value session["flash"] = flash_hash.to_session_value
self.flash = flash_hash.dup self.flash = flash_hash.dup
end end
if (!session.respond_to?(:loaded?) || session.loaded?) && # reset_session uses {}, which doesn't implement #loaded? if session.loaded? && session.key?("flash") && session["flash"].nil?
session.key?("flash") && session["flash"].nil?
session.delete("flash") session.delete("flash")
end end
end end
@ -80,20 +77,6 @@ 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