aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-11-23 10:17:34 -0800
committerJosé Valim <jose.valim@gmail.com>2011-11-23 10:17:34 -0800
commit2c568f1e45ef0704a87881750252731a3adff52c (patch)
tree74a9cffdadee137499e31468a885cd5039ad6ef9 /activesupport
parentf62f545f7ccb23c4616e26dbf5ea4ee17ed7b079 (diff)
parenta650dd05f82028e8d1310f1b68b7bc430ea47dcf (diff)
downloadrails-2c568f1e45ef0704a87881750252731a3adff52c.tar.gz
rails-2c568f1e45ef0704a87881750252731a3adff52c.tar.bz2
rails-2c568f1e45ef0704a87881750252731a3adff52c.zip
Merge pull request #3738 from exviva/issues/3737_AS_cache_expand_cache_key
Issues/3737 AS::Cache.expand_cache_key
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/cache.rb14
-rw-r--r--activesupport/test/caching_test.rb25
2 files changed, 26 insertions, 13 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 2bf24558a6..12eeff4f4b 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
+ else 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..8871b1b0e9 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
@@ -42,7 +42,7 @@ class CacheKeyTest < ActiveSupport::TestCase
end
end
- def test_respond_to_cache_key
+ def test_expand_cache_key_respond_to_cache_key
key = 'foo'
def key.cache_key
:foo_key
@@ -50,6 +50,25 @@ class CacheKeyTest < ActiveSupport::TestCase
assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key(key)
end
+ def test_expand_cache_key_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
+
+ def test_expand_cache_key_of_nil
+ assert_equal '', ActiveSupport::Cache.expand_cache_key(nil)
+ end
+
+ def test_expand_cache_key_of_false
+ assert_equal 'false', ActiveSupport::Cache.expand_cache_key(false)
+ end
+
+ def test_expand_cache_key_of_true
+ assert_equal 'true', ActiveSupport::Cache.expand_cache_key(true)
+ end
end
class CacheStoreSettingTest < ActiveSupport::TestCase