Skip to content

Commit 2c568f1

Browse files
committed
Merge pull request rails#3738 from exviva/issues/3737_AS_cache_expand_cache_key
Issues/3737 AS::Cache.expand_cache_key
2 parents f62f545 + a650dd0 commit 2c568f1

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

activesupport/lib/active_support/cache.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,10 @@ def self.expand_cache_key(key, namespace = nil)
8181
end
8282

8383
expanded_cache_key <<
84-
if key.respond_to?(:cache_key)
85-
key.cache_key
86-
elsif key.is_a?(Array)
87-
if key.size > 1
88-
key.collect { |element| expand_cache_key(element) }.to_param
89-
else
90-
key.first.to_param
91-
end
92-
elsif key
93-
key.to_param
84+
case
85+
when key.respond_to?(:cache_key) then key.cache_key
86+
when key.is_a?(Array) then key.map { |element| expand_cache_key(element) }.to_param
87+
else key.to_param
9488
end.to_s
9589

9690
expanded_cache_key

activesupport/test/caching_test.rb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def test_expand_cache_key_with_rails_cache_id
1212
begin
1313
ENV['RAILS_CACHE_ID'] = 'c99'
1414
assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key(:foo)
15-
assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key([:foo])
15+
assert_equal 'c99/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo])
1616
assert_equal 'c99/c99/foo/c99/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar])
1717
assert_equal 'nm/c99/foo', ActiveSupport::Cache.expand_cache_key(:foo, :nm)
18-
assert_equal 'nm/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo], :nm)
18+
assert_equal 'nm/c99/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo], :nm)
1919
assert_equal 'nm/c99/c99/foo/c99/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar], :nm)
2020
ensure
2121
ENV['RAILS_CACHE_ID'] = nil
@@ -42,14 +42,33 @@ def test_expand_cache_key_rails_cache_id_should_win_over_rails_app_version
4242
end
4343
end
4444

45-
def test_respond_to_cache_key
45+
def test_expand_cache_key_respond_to_cache_key
4646
key = 'foo'
4747
def key.cache_key
4848
:foo_key
4949
end
5050
assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key(key)
5151
end
5252

53+
def test_expand_cache_key_array_with_something_that_responds_to_cache_key
54+
key = 'foo'
55+
def key.cache_key
56+
:foo_key
57+
end
58+
assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key([key])
59+
end
60+
61+
def test_expand_cache_key_of_nil
62+
assert_equal '', ActiveSupport::Cache.expand_cache_key(nil)
63+
end
64+
65+
def test_expand_cache_key_of_false
66+
assert_equal 'false', ActiveSupport::Cache.expand_cache_key(false)
67+
end
68+
69+
def test_expand_cache_key_of_true
70+
assert_equal 'true', ActiveSupport::Cache.expand_cache_key(true)
71+
end
5372
end
5473

5574
class CacheStoreSettingTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)