aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlek Janiszewski <olek.janiszewski@gmail.com>2011-11-23 18:06:15 +0100
committerOlek Janiszewski <olek.janiszewski@gmail.com>2011-11-23 18:06:16 +0100
commitd8e6dc9cf12096908a7a53dec07a397ba23b1088 (patch)
treef7c8682da6c26d8e888f4ac029fce8b461044260
parenta93ee92da2b9ba83e3a3fe0b8d4dd5cac2790f15 (diff)
downloadrails-d8e6dc9cf12096908a7a53dec07a397ba23b1088.tar.gz
rails-d8e6dc9cf12096908a7a53dec07a397ba23b1088.tar.bz2
rails-d8e6dc9cf12096908a7a53dec07a397ba23b1088.zip
Fix #3737 AS::expand_cache_key generates wrong key in certain situations
`cache_key` method is never called when the argument is a 1-element array with something that responds to `cache_key`
-rw-r--r--activesupport/lib/active_support/cache.rb14
-rw-r--r--activesupport/test/caching_test.rb12
2 files changed, 14 insertions, 12 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 2bf24558a6..cfd1a6dbe3 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -81,16 +81,10 @@ module ActiveSupport
end
expanded_cache_key <<
- if key.respond_to?(:cache_key)
- key.cache_key
- elsif key.is_a?(Array)
- if key.size > 1
- key.collect { |element| expand_cache_key(element) }.to_param
- else
- key.first.to_param
- end
- elsif key
- key.to_param
+ case
+ when key.respond_to?(:cache_key) then key.cache_key
+ when key.is_a?(Array) then key.map { |element| expand_cache_key(element) }.to_param
+ when key then key.to_param
end.to_s
expanded_cache_key
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index b1b6de0613..5c844b8122 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -12,10 +12,10 @@ class CacheKeyTest < ActiveSupport::TestCase
begin
ENV['RAILS_CACHE_ID'] = 'c99'
assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key(:foo)
- assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key([:foo])
+ assert_equal 'c99/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo])
assert_equal 'c99/c99/foo/c99/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar])
assert_equal 'nm/c99/foo', ActiveSupport::Cache.expand_cache_key(:foo, :nm)
- assert_equal 'nm/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo], :nm)
+ assert_equal 'nm/c99/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo], :nm)
assert_equal 'nm/c99/c99/foo/c99/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar], :nm)
ensure
ENV['RAILS_CACHE_ID'] = nil
@@ -50,6 +50,14 @@ class CacheKeyTest < ActiveSupport::TestCase
assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key(key)
end
+ def test_array_with_something_that_responds_to_cache_key
+ key = 'foo'
+ def key.cache_key
+ :foo_key
+ end
+ assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key([key])
+ end
+
end
class CacheStoreSettingTest < ActiveSupport::TestCase