Class: JbuilderTemplate

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

Constant Summary

Constants inherited from Jbuilder

Jbuilder::BLANK, Jbuilder::DependencyTracker, Jbuilder::NON_ENUMERABLES

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Jbuilder

#attributes!, #call, #child!, encode, #extract!, ignore_nil, #ignore_nil!, key_format, #key_format!, #merge!, #method_missing, #nil!, #target!

Constructor Details

#initialize(context, *args) ⇒ JbuilderTemplate

Returns a new instance of JbuilderTemplate.



12
13
14
15
# File 'lib/jbuilder/jbuilder_template.rb', line 12

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

Class Attribute Details

.template_lookup_optionsObject

Returns the value of attribute template_lookup_options.



7
8
9
# File 'lib/jbuilder/jbuilder_template.rb', line 7

def template_lookup_options
  @template_lookup_options
end

Instance Method Details

#array!(collection = [], *args) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/jbuilder/jbuilder_template.rb', line 58

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 = {}) ⇒ 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


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jbuilder/jbuilder_template.rb', line 33

def cache!(key=nil, options={})
  if @context.controller.perform_caching
    value = ::Rails.cache.fetch(_cache_key(key, options), options) do
      _scope { yield self }
    end

    merge! value
  else
    yield
  end
end

#cache_if!(condition, *args) ⇒ Object

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


54
55
56
# File 'lib/jbuilder/jbuilder_template.rb', line 54

def cache_if!(condition, *args)
  condition ? cache!(*args, &::Proc.new) : yield
end

#partial!(*args) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/jbuilder/jbuilder_template.rb', line 17

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) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/jbuilder/jbuilder_template.rb', line 68

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