lib/jbuilder/jbuilder_template.rb in jbuilder-2.6.0 vs lib/jbuilder/jbuilder_template.rb in jbuilder-2.6.1

- old
+ new

@@ -40,10 +40,31 @@ else yield end end + # Caches the json structure at the root using a string rather than the hash structure. This is considerably + # faster, but the drawback is that it only works, as the name hints, at the root. So you cannot + # use this approach to cache deeper inside the hierarchy, like in partials or such. Continue to use #cache! there. + # + # Example: + # + # json.cache_root! @person do + # json.extract! @person, :name, :age + # end + # + # # json.extra 'This will not work either, the root must be exclusive' + def cache_root!(key=nil, options={}) + if @context.controller.perform_caching + raise "cache_root! can't be used after JSON structures have been defined" if @attributes.present? + + @cached_root = _cache_fragment_for([ :root, key ], options) { yield; target! } + else + yield + end + end + # Conditionally caches the json depending in the condition given as first parameter. Has the same # signature as the `cache` helper method in `ActionView::Helpers::CacheHelper` and so can be used in # the same way. # # Example: @@ -51,9 +72,13 @@ # json.cache_if! !admin?, @person, expires_in: 10.minutes do # json.extract! @person, :name, :age # end def cache_if!(condition, *args) condition ? cache!(*args, &::Proc.new) : yield + end + + def target! + @cached_root || super end def array!(collection = [], *args) options = args.first