class ActionDispatch::Flash::FlashHash
def self.from_session_value(value) #:nodoc:
def self.from_session_value(value) #:nodoc: case value when FlashHash # Rails 3.1, 3.2 flashes = value.instance_variable_get(:@flashes) if discard = value.instance_variable_get(:@used) flashes.except!(*discard) end new(flashes, flashes.keys) when Hash # Rails 4.0 flashes = value["flashes"] if discard = value["discard"] flashes.except!(*discard) end new(flashes, flashes.keys) else new end end
def [](k)
def [](k) @flashes[k.to_s] end
def []=(k, v)
def []=(k, v) k = k.to_s @discard.delete k @flashes[k] = v end
def alert
def alert self[:alert] end
def alert=(message)
def alert=(message) self[:alert] = message end
def clear
def clear @discard.clear @flashes.clear end
def delete(key)
def delete(key) key = key.to_s @discard.delete key @flashes.delete key self end
def discard(k = nil)
flash.discard # discard the entire flash at the end of the current action
Marks the entire flash or a single flash entry to be discarded by the end of the current action:
def discard(k = nil) k = k.to_s if k @discard.merge Array(k || keys) k ? self[k] : self end
def each(&block)
def each(&block) @flashes.each(&block) end
def empty?
def empty? @flashes.empty? end
def initialize(flashes = {}, discard = []) #:nodoc:
def initialize(flashes = {}, discard = []) #:nodoc: @discard = Set.new(stringify_array(discard)) @flashes = flashes.stringify_keys @now = nil end
def initialize_copy(other)
def initialize_copy(other) if other.now_is_loaded? @now = other.now.dup @now.flash = self end super end
def keep(k = nil)
flash.keep # keeps the entire flash
Keeps either the entire current flash or a specific flash entry available for the next action:
def keep(k = nil) k = k.to_s if k @discard.subtract Array(k || keys) k ? self[k] : self end
def key?(name)
def key?(name) @flashes.key? name.to_s end
def keys
def keys @flashes.keys end
def notice
def notice self[:notice] end
def notice=(message)
def notice=(message) self[:notice] = message end
def now
flash.now.notice = "Good luck now!"
# Equivalent to flash.now[:alert] = "Beware now!"
flash.now.alert = "Beware now!"
Also, brings two convenience accessors:
Entries set via now are accessed the same way as standard entries: flash['my-key'].
vanish when the current action is done.
When you need to pass an object to the current action, you use now, and your object will
When you need to pass an object to the next action, you use the standard flash assign ([]=).
This method enables you to use the flash as a central messaging system in your app.
flash.now[:message] = "Hello current action"
Sets a flash that will not be available to the next action, only to the current.
def now @now ||= FlashNow.new(self) end
def now_is_loaded?
def now_is_loaded? @now end
def replace(h) #:nodoc:
def replace(h) #:nodoc: @discard.clear @flashes.replace h.stringify_keys self end
def stringify_array(array) # :doc:
def stringify_array(array) # :doc: array.map do |item| item.kind_of?(Symbol) ? item.to_s : item end end
def sweep #:nodoc:
This method is called automatically by filters, so you generally don't need to care about it.
Mark for removal entries that were kept, and delete unkept ones.
def sweep #:nodoc: @discard.each { |k| @flashes.delete k } @discard.replace @flashes.keys end
def to_hash
def to_hash @flashes.dup end
def to_session_value #:nodoc:
If there are none to keep, returns +nil+.
Builds a hash containing the flashes to keep for the next request.
def to_session_value #:nodoc: flashes_to_keep = @flashes.except(*@discard) return nil if flashes_to_keep.empty? { "discard" => [], "flashes" => flashes_to_keep } end
def update(h) #:nodoc:
def update(h) #:nodoc: @discard.subtract stringify_array(h.keys) @flashes.update h.stringify_keys self end