Permalink
Browse files

Revert back a bit to the old style (non-ActionView) but leverage frag…

…ment_name_with_digest if available
  • Loading branch information...
1 parent e1bb6fc commit c202f34022c656f0eab64e8720d2a4a559eb55c2 @rgarver rgarver committed Sep 27, 2012
Showing with 13 additions and 23 deletions.
  1. +8 −20 lib/jbuilder_template.rb
  2. +5 −3 test/jbuilder_template_test.rb
View
@@ -24,33 +24,21 @@ def partial!(options, locals = {})
# json.extract! @person, :name, :age
# end
def cache!(key=nil, options={}, &block)
- fragment = self.extract_output_buffer_change do
- @context.cache(key, options) do
- @context.safe_concat(
- ::MultiJson.encode(_scope { yield self }).html_safe
- )
- end
+ options[:force] = true unless @context.controller.perform_caching
+ value = ::Rails.cache.fetch(_cache_key(key), options) do
+ _scope { yield self }
end
- _merge(::MultiJson.load(fragment))
+ _merge(value)
end
protected
- def reset_safety_on_output_buffer
- if @context.output_buffer
- if @context.output_buffer.html_safe?
- @context.output_buffer = @context.output_buffer.class.new(@context.output_buffer)
- end
+ def _cache_key(key)
+ if @context.respond_to?(:fragment_name_with_digest)
+ @context.fragment_name_with_digest(key)
else
- @context.output_buffer = ::ActionView::OutputBuffer.new
+ ::ActiveSupport::Cache.expand_cache_key(key.is_a?(::Hash) ? url_for(key).split("://").last : key, :jbuilder)
end
end
-
- def extract_output_buffer_change
- self.reset_safety_on_output_buffer
- pos = @context.output_buffer.length
- yield
- @context.output_buffer.slice!(pos..-1)
- end
end
class JbuilderHandler
@@ -8,9 +8,11 @@
module Rails
class Cache
def initialize
- @cache = {}
+ clear
end
+ def clear; @cache = {}; end
+
def write(k, v, opt={})
@cache[k] = v
end
@@ -79,7 +81,7 @@ def render_jbuilder(source)
test "fragment caching a JSON object" do
self.controller.perform_caching = true
- self.controller.cache_store = :memory_store
+ Rails.cache.clear
render_jbuilder <<-JBUILDER
json.cache!("cachekey") do
json.name "Cache"
@@ -98,8 +100,8 @@ def render_jbuilder(source)
end
test "fragment caching deserializes an array" do
+ Rails.cache.clear
self.controller.perform_caching = true
- self.controller.cache_store = :memory_store
render_jbuilder <<-JBUILDER
json.cache!("cachekey") do
json.array! ['a', 'b', 'c']

0 comments on commit c202f34

Please sign in to comment.