diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2016-02-24 00:22:04 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2016-02-24 00:22:04 -0300 |
commit | 22db455dbe9c26fe6d723cac0758705d9943ea4b (patch) | |
tree | f51b9bcbca80952a48501c2dbd5d72d38dd29d71 /actionpack/lib | |
parent | 40be61dfda1e04c3f306022a40370862e3a2ce39 (diff) | |
parent | 45a75a3fcc96b22954caf69be2df4e302b134d7a (diff) | |
download | rails-22db455dbe9c26fe6d723cac0758705d9943ea4b.tar.gz rails-22db455dbe9c26fe6d723cac0758705d9943ea4b.tar.bz2 rails-22db455dbe9c26fe6d723cac0758705d9943ea4b.zip |
Merge pull request #20851 from tomprats/indifferent-sessions
Give Sessions Indifferent Access
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_controller/test_case.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/request/session.rb | 26 |
2 files changed, 11 insertions, 17 deletions
diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index 700317614f..6548ce326b 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -176,7 +176,7 @@ module ActionController def initialize(session = {}) super(nil, nil) @id = SecureRandom.hex(16) - @data = stringify_keys(session) + @data = session.with_indifferent_access @loaded = true end diff --git a/actionpack/lib/action_dispatch/request/session.rb b/actionpack/lib/action_dispatch/request/session.rb index 42890225fa..38d0da3e67 100644 --- a/actionpack/lib/action_dispatch/request/session.rb +++ b/actionpack/lib/action_dispatch/request/session.rb @@ -9,7 +9,7 @@ module ActionDispatch # Singleton object used to determine if an optional param wasn't specified Unspecified = Object.new - + # Creates a session hash, merging the properties of the previous session if any def self.create(store, req, default_options) session_was = find req @@ -61,7 +61,7 @@ module ActionDispatch def initialize(by, req) @by = by @req = req - @delegate = {} + @delegate = {}.with_indifferent_access @loaded = false @exists = nil # we haven't checked yet end @@ -88,13 +88,13 @@ module ActionDispatch # nil if the given key is not found in the session. def [](key) load_for_read! - @delegate[key.to_s] + @delegate[key] end # Returns true if the session has the given key or false. def has_key?(key) load_for_read! - @delegate.key?(key.to_s) + @delegate.key?(key) end alias :key? :has_key? alias :include? :has_key? @@ -112,7 +112,7 @@ module ActionDispatch # Writes given value to given key of the session. def []=(key, value) load_for_write! - @delegate[key.to_s] = value + @delegate[key] = value end # Clears the session. @@ -139,13 +139,13 @@ module ActionDispatch # # => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2", "foo" => "bar"} def update(hash) load_for_write! - @delegate.update stringify_keys(hash) + @delegate.update hash end # Deletes given key from the session. def delete(key) load_for_write! - @delegate.delete key.to_s + @delegate.delete key end # Returns value of the given key from the session, or raises +KeyError+ @@ -165,9 +165,9 @@ module ActionDispatch def fetch(key, default=Unspecified, &block) load_for_read! if default == Unspecified - @delegate.fetch(key.to_s, &block) + @delegate.fetch(key, &block) else - @delegate.fetch(key.to_s, default, &block) + @delegate.fetch(key, default, &block) end end @@ -211,15 +211,9 @@ module ActionDispatch def load! id, session = @by.load_session @req options[:id] = id - @delegate.replace(stringify_keys(session)) + @delegate.replace(session) @loaded = true end - - def stringify_keys(other) - other.each_with_object({}) { |(key, value), hash| - hash[key.to_s] = value - } - end end end end |