aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorSamuel Cochran <sj26@sj26.com>2015-01-29 23:18:55 +1100
committerSamuel Cochran <sj26@sj26.com>2015-01-29 23:18:55 +1100
commitf7adb34999eb4df7916102b60f5aa3f0ddc8210c (patch)
tree94019b9fcc8c227e41efa19226c8f8632c0f51f7 /actionpack
parent3a102d052803f7f066e648454297301310d69906 (diff)
downloadrails-f7adb34999eb4df7916102b60f5aa3f0ddc8210c.tar.gz
rails-f7adb34999eb4df7916102b60f5aa3f0ddc8210c.tar.bz2
rails-f7adb34999eb4df7916102b60f5aa3f0ddc8210c.zip
Discard from flash before persisting in session
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/middleware/flash.rb34
-rw-r--r--actionpack/test/controller/flash_hash_test.rb16
2 files changed, 26 insertions, 24 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb
index a7f95150a4..b1d402cd30 100644
--- a/actionpack/lib/action_dispatch/middleware/flash.rb
+++ b/actionpack/lib/action_dispatch/middleware/flash.rb
@@ -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)
- end
-
- # Builds a hash containing the discarded values and the hashes
- # representing the flashes.
- # If there are no values in @flashes, returns nil.
+ 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 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:
diff --git a/actionpack/test/controller/flash_hash_test.rb b/actionpack/test/controller/flash_hash_test.rb
index d979b561f2..93ed5a037e 100644
--- a/actionpack/test/controller/flash_hash_test.rb
+++ b/actionpack/test/controller/flash_hash_test.rb
@@ -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