Class: JbuilderTemplate

Inherits:
Jbuilder
  • Object
show all
Defined in:
lib/jbuilder_template.rb

Instance Method Summary collapse

Methods inherited from Jbuilder

#array!, #attributes!, #call, #child!, encode, #extract!, key_format, #key_format!, #set!, #target!

Constructor Details

#initialize(context, *args) ⇒ JbuilderTemplate

Returns a new instance of JbuilderTemplate.



2
3
4
5
# File 'lib/jbuilder_template.rb', line 2

def initialize(context, *args)
  @context = context
  super(*args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Jbuilder

Instance Method Details

#cache!(key = nil, options = {}, &block) ⇒ Object

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


26
27
28
29
30
31
32
33
# File 'lib/jbuilder_template.rb', line 26

def cache!(key=nil, options={}, &block)
  options[:force] = true unless @context.controller.perform_caching
  value = ::Rails.cache.fetch(_cache_key(key), options) do
    _scope { yield self }
  end

  _merge(value)
end

#partial!(options, locals = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/jbuilder_template.rb', line 7

def partial!(options, locals = {})
  case options
  when ::Hash
    options[:locals] ||= {}
    options[:locals].merge!(:json => self)
    @context.render(options.reverse_merge(:formats => [:json]))
  else # String
    @context.render(:partial => options, :locals => locals.merge(:json => self), :formats => [:json])
  end
end