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)
Immediately deletes the single flash entry. Use this method when you want
def delete(key) key = key.to_s @discard.delete key @flashes.delete key self end
def discard(k = nil)
Use this method when you want to display the message in the current action but
flash.discard(:warning) # discard only the "warning" entry at the end of the current action
flash.discard # discard the entire flash at the end of the current action
the current action:
Marks the entire flash or a single flash entry to be discarded by the end of
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
the next action:
Keeps either the entire current flash or a specific flash entry available for
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:
`flash['my-key']`.
Entries set via `now` are accessed the same way as standard entries:
you use `now`, and your object will vanish when the current action is done.
flash assign (`[]=`). When you need to pass an object to the current action,
app. When you need to pass an object to the next action, you use the standard
This method enables you to use the flash as a central messaging system in your
flash.now[:message] = "Hello current action"
current.
Sets a flash that will not be available to the next action, only to the
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:
care about it.
This method is called automatically by filters, so you generally don't need to
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:
are none to keep, returns `nil`.
Builds a hash containing the flashes to keep for the next request. If there
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