diff --git a/.rubocop.yml b/.rubocop.yml index 7da823e..196526e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,6 +16,9 @@ Metrics/BlockLength: Exclude: - 'spec/**/*.rb' +Metrics/LineLength: + Max: 100 + Security/MarshalLoad: Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ffba99a..6d4e08a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -9,4 +9,4 @@ # Offense count: 1 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 108 + Max: 110 diff --git a/README.md b/README.md index 84f40af..1bbbdcd 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,10 @@ My::Application.config.session_store :redis_session_store, { **Note** The session will *always* be destroyed when it cannot be loaded. +### Other notes + +It returns with_indifferent_access if ActiveSupport is defined + ## Rails 2 Compatibility This gem is currently only compatible with Rails 3+. If you need diff --git a/lib/redis-session-store.rb b/lib/redis-session-store.rb index 9a645de..b02eab6 100644 --- a/lib/redis-session-store.rb +++ b/lib/redis-session-store.rb @@ -13,6 +13,7 @@ class RedisSessionStore < ActionDispatch::Session::AbstractStore end end + USE_INDIFFERENT_ACCESS = defined?(ActiveSupport).freeze # ==== Options # * +:key+ - Same as with the other cookie stores, key name # * +:redis+ - A hash with redis-specific options @@ -86,16 +87,15 @@ def prefixed(sid) "#{default_options[:key_prefix]}#{sid}" end - def get_session(env, sid) - unless sid && (session = load_session_from_redis(sid)) - sid = generate_sid - session = {} - end + def session_default_values + [generate_sid, USE_INDIFFERENT_ACCESS ? {}.with_indifferent_access : {}] + end - [sid, session] + def get_session(env, sid) + sid && (session = load_session_from_redis(sid)) ? [sid, session] : session_default_values rescue Errno::ECONNREFUSED, Redis::CannotConnectError => e on_redis_down.call(e, env, sid) if on_redis_down - [generate_sid, {}] + session_default_values end alias find_session get_session @@ -111,7 +111,8 @@ def load_session_from_redis(sid) end def decode(data) - serializer.load(data) + session = serializer.load(data) + USE_INDIFFERENT_ACCESS ? session.with_indifferent_access : session end def set_session(env, sid, session_data, options = nil) @@ -121,10 +122,10 @@ def set_session(env, sid, session_data, options = nil) else redis.set(prefixed(sid), encode(session_data)) end - return sid + sid rescue Errno::ECONNREFUSED, Redis::CannotConnectError => e on_redis_down.call(e, env, sid) if on_redis_down - return false + false end alias write_session set_session