Skip to content

Commit

Permalink
fix cache key generation for complex object
Browse files Browse the repository at this point in the history
  • Loading branch information
kaichen committed Jun 6, 2014
1 parent 3905da6 commit ca9622c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
11 changes: 8 additions & 3 deletions lib/jbuilder/jbuilder_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ def _render_partial(options)
end

def _cache_key(key, options)
key = fragment_name_with_digest(key, options)
::ActiveSupport::Cache.expand_cache_key(key.is_a?(::Hash) ? url_for(key).split('://').last : key, :jbuilder)
end

private

def fragment_name_with_digest(key, options)
if @context.respond_to?(:cache_fragment_name)
# Current compatibility, fragment_name_with_digest is private again and cache_fragment_name
# should be used instead.
Expand All @@ -106,12 +113,10 @@ def _cache_key(key, options)
# Backwards compatibility for period of time when fragment_name_with_digest was made public.
@context.fragment_name_with_digest(key)
else
::ActiveSupport::Cache.expand_cache_key(key.is_a?(::Hash) ? url_for(key).split('://').last : key, :jbuilder)
key
end
end

private

def _mapable_arguments?(value, *args)
return true if super
options = args.last
Expand Down
14 changes: 2 additions & 12 deletions test/jbuilder_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def assert_collection_rendered(json, context = nil)
undef_context_methods :fragment_name_with_digest

@context.expects :cache_fragment_name
ActiveSupport::Cache.expects :expand_cache_key

render_jbuilder <<-JBUILDER
json.cache! 'cachekey' do
Expand All @@ -258,6 +259,7 @@ def assert_collection_rendered(json, context = nil)
undef_context_methods :fragment_name_with_digest

@context.expects(:cache_fragment_name).with('cachekey', skip_digest: true)
ActiveSupport::Cache.expects :expand_cache_key

render_jbuilder <<-JBUILDER
json.cache! 'cachekey', skip_digest: true do
Expand All @@ -277,16 +279,4 @@ def assert_collection_rendered(json, context = nil)
assert_equal Rails.cache.inspect[/entries=(\d+)/, 1], '0'
end

test 'fragment caching falls back on ActiveSupport::Cache.expand_cache_key' do
undef_context_methods :fragment_name_with_digest, :cache_fragment_name

ActiveSupport::Cache.expects :expand_cache_key

render_jbuilder <<-JBUILDER
json.cache! 'cachekey' do
json.name 'Cache'
end
JBUILDER
end

end

0 comments on commit ca9622c

Please sign in to comment.