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

Discard from flash before persisting in session

This commit is contained in:
Samuel Cochran 2015-01-29 23:18:55 +11:00
parent 3a102d0528
commit f7adb34999
2 changed files with 25 additions and 23 deletions

View file

@ -80,24 +80,24 @@ module ActionDispatch
include Enumerable
def self.from_session_value(value) #:nodoc:
flash = case value
when FlashHash # Rails 3.1, 3.2
new(value.instance_variable_get(:@flashes), value.instance_variable_get(:@used))
when Hash # Rails 4.0
new(value['flashes'], value['discard'])
else
new
end
flash.tap(&:sweep)
case value
when FlashHash # Rails 3.1, 3.2
flashes = value.instance_variable_get(:@flashes)
new(flashes, flashes.keys)
when Hash # Rails 4.0
flashes = value['flashes']
new(flashes, flashes.keys)
else
new
end
end
# Builds a hash containing the discarded values and the hashes
# representing the flashes.
# If there are no values in @flashes, returns nil.
# Builds a hash containing the flashes to keep for the next request.
# If there are none to keep, returns nil.
def to_session_value #:nodoc:
return nil if empty?
{'discard' => @discard.to_a, 'flashes' => @flashes}
flashes_to_keep = @flashes.except(*@discard)
return nil if flashes_to_keep.empty?
{'flashes' => flashes_to_keep}
end
def initialize(flashes = {}, discard = []) #:nodoc:

View file

@ -57,13 +57,13 @@ module ActionDispatch
def test_to_session_value
@hash['foo'] = 'bar'
assert_equal({'flashes' => {'foo' => 'bar'}, 'discard' => []}, @hash.to_session_value)
@hash.discard('foo')
assert_equal({'flashes' => {'foo' => 'bar'}, 'discard' => %w[foo]}, @hash.to_session_value)
assert_equal({'flashes' => {'foo' => 'bar'}}, @hash.to_session_value)
@hash.now['qux'] = 1
assert_equal({'flashes' => {'foo' => 'bar', 'qux' => 1}, 'discard' => %w[foo qux]}, @hash.to_session_value)
assert_equal({'flashes' => {'foo' => 'bar'}}, @hash.to_session_value)
@hash.discard('foo')
assert_equal(nil, @hash.to_session_value)
@hash.sweep
assert_equal(nil, @hash.to_session_value)
@ -73,7 +73,8 @@ module ActionDispatch
rails_3_2_cookie = 'BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWY4ZTFiODE1MmJhNzYwOWMyOGJiYjE3ZWM5MjYzYmE3BjsAVEkiCmZsYXNoBjsARm86JUFjdGlvbkRpc3BhdGNoOjpGbGFzaDo6Rmxhc2hIYXNoCToKQHVzZWRvOghTZXQGOgpAaGFzaHsAOgxAY2xvc2VkRjoNQGZsYXNoZXN7BkkiDG1lc3NhZ2UGOwBGSSIKSGVsbG8GOwBGOglAbm93MA=='
session = Marshal.load(Base64.decode64(rails_3_2_cookie))
hash = Flash::FlashHash.from_session_value(session['flash'])
assert_equal({'flashes' => {'message' => 'Hello'}, 'discard' => %w[message]}, hash.to_session_value)
assert_equal({'message' => 'Hello'}, hash.to_hash)
assert_equal(nil, hash.to_session_value)
end
def test_from_session_value_on_json_serializer
@ -81,7 +82,8 @@ module ActionDispatch
session = ActionDispatch::Cookies::JsonSerializer.load(decrypted_data)
hash = Flash::FlashHash.from_session_value(session['flash'])
assert_equal({'discard' => %w[message], 'flashes' => { 'message' => 'hey you'}}, hash.to_session_value)
assert_equal({'message' => 'hey you'}, hash.to_hash)
assert_equal(nil, hash.to_session_value)
assert_equal "hey you", hash[:message]
assert_equal "hey you", hash["message"]
end