class JbuilderTemplate

Attributes

template_lookup_options[RW]

Public Class Methods

new(context, *args) click to toggle source
Calls superclass method Jbuilder.new
# File lib/jbuilder/jbuilder_template.rb, line 12
def initialize(context, *args)
  @context = context
  @cached_root = nil
  super(*args)
end

Public Instance Methods

array!(collection = [], *args) click to toggle source
Calls superclass method Jbuilder#array!
# File lib/jbuilder/jbuilder_template.rb, line 84
def array!(collection = [], *args)
  options = args.first

  if args.one? && _partial_options?(options)
    partial! options.merge(collection: collection)
  else
    super
  end
end
cache!(key=nil, options={}) { |self| ... } click to toggle source

Caches the json constructed within the block passed. Has the same signature as the `cache` helper method in `ActionView::Helpers::CacheHelper` and so can be used in the same way.

Example:

json.cache! ['v1', @person], expires_in: 10.minutes do
  json.extract! @person, :name, :age
end
# File lib/jbuilder/jbuilder_template.rb, line 34
def cache!(key=nil, options={})
  if @context.controller.perform_caching
    value = _cache_fragment_for(key, options) do
      _scope { yield self }
    end

    merge! value
  else
    yield
  end
end
cache_if!(condition, *args) { || ... } click to toggle source

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:

json.cache_if! !admin?, @person, expires_in: 10.minutes do
  json.extract! @person, :name, :age
end
# File lib/jbuilder/jbuilder_template.rb, line 76
def cache_if!(condition, *args, &block)
  condition ? cache!(*args, &block) : yield
end
cache_root!(key=nil, options={}) { || ... } click to toggle source

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'
# File lib/jbuilder/jbuilder_template.rb, line 57
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
partial!(*args) click to toggle source
# File lib/jbuilder/jbuilder_template.rb, line 18
def partial!(*args)
  if args.one? && _is_active_model?(args.first)
    _render_active_model_partial args.first
  else
    _render_explicit_partial(*args)
  end
end
set!(name, object = BLANK, *args) click to toggle source
Calls superclass method Jbuilder#set!
# File lib/jbuilder/jbuilder_template.rb, line 94
def set!(name, object = BLANK, *args)
  options = args.first

  if args.one? && _partial_options?(options)
    _set_inline_partial name, object, options
  else
    super
  end
end
target!() click to toggle source
Calls superclass method Jbuilder#target!
# File lib/jbuilder/jbuilder_template.rb, line 80
def target!
  @cached_root || super
end

Private Instance Methods

_cache_fragment_for(key, options, &block) click to toggle source
# File lib/jbuilder/jbuilder_template.rb, line 131
def _cache_fragment_for(key, options, &block)
  key = _cache_key(key, options)
  _read_fragment_cache(key, options) || _write_fragment_cache(key, options, &block)
end
_read_fragment_cache(key, options = nil) click to toggle source
# File lib/jbuilder/jbuilder_template.rb, line 136
def _read_fragment_cache(key, options = nil)
  @context.controller.instrument_fragment_cache :read_fragment, key do
    ::Rails.cache.read(key, options)
  end
end
_render_partial(options) click to toggle source
# File lib/jbuilder/jbuilder_template.rb, line 126
def _render_partial(options)
  options[:locals].merge! json: self
  @context.render options
end
_render_partial_with_options(options) click to toggle source
# File lib/jbuilder/jbuilder_template.rb, line 106
def _render_partial_with_options(options)
  options.reverse_merge! locals: options.except(:partial, :as, :collection)
  options.reverse_merge! ::JbuilderTemplate.template_lookup_options
  as = options[:as]

  if as && options.key?(:collection)
    as = as.to_sym
    collection = options.delete(:collection)
    locals = options.delete(:locals)
    array! collection do |member|
      member_locals = locals.clone
      member_locals.merge! collection: collection
      member_locals.merge! as => member
      _render_partial options.merge(locals: member_locals)
    end
  else
    _render_partial options
  end
end
_write_fragment_cache(key, options = nil) { || ... } click to toggle source
# File lib/jbuilder/jbuilder_template.rb, line 142
def _write_fragment_cache(key, options = nil)
  @context.controller.instrument_fragment_cache :write_fragment, key do
    yield.tap do |value|
      ::Rails.cache.write(key, value, options)
    end
  end