Class: JbuilderTemplate

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

Constant Summary

Constants inherited from Jbuilder

Jbuilder::BLANK, Jbuilder::VERSION

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Jbuilder

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

Constructor Details

#initialize(context, *args) ⇒ JbuilderTemplate

Returns a new instance of JbuilderTemplate.



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

def initialize(context, *args)
  @context = context
  @cached_root = nil
  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.



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

def template_lookup_options
  @template_lookup_options
end

Instance Method Details

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



117
118
119
120
121
122
123
124
125
# File 'lib/jbuilder/jbuilder_template.rb', line 117

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


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

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, &block) ⇒ 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


109
110
111
# File 'lib/jbuilder/jbuilder_template.rb', line 109

def cache_if!(condition, *args, &block)
  condition ? cache!(*args, &block) : yield
end

#cache_root!(key = nil, options = {}) ⇒ Object

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'


90
91
92
93
94
95
96
97
98
# File 'lib/jbuilder/jbuilder_template.rb', line 90

def cache_root!(key=nil, options={})
  if @context.controller.perform_caching
    ::Kernel.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) ⇒ Object

Generates JSON using the template specified with the ‘:partial` option. For example, the code below will render the file `views/comments/_comments.json.jbuilder`, and set a local variable comments with all this message’s comments, which can be used inside the partial.

Example:

json.partial! 'comments/comments', comments: @message.comments

There are multiple ways to generate a collection of elements as JSON, as ilustrated below:

Example:

json.array! @posts, partial: 'posts/post', as: :post

# or:
json.partial! 'posts/post', collection: @posts, as: :post

# or:
json.partial! partial: 'posts/post', collection: @posts, as: :post

# or:
json.comments @post.comments, partial: 'comments/comment', as: :comment

Aside from that, the ‘:cached` options is available on Rails >= 6.0. This will cache the rendered results effectively using the multi fetch feature.

Example:

json.array! @posts, partial: "posts/post", as: :post, cached: true

json.comments @post.comments, partial: "comments/comment", as: :comment, cached: true


51
52
53
54
55
56
57
# File 'lib/jbuilder/jbuilder_template.rb', line 51

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



127
128
129
130
131
132
133
134
135
# File 'lib/jbuilder/jbuilder_template.rb', line 127

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!Object



113
114
115
# File 'lib/jbuilder/jbuilder_template.rb', line 113

def target!
  @cached_root || super
end