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!, #nil!

Constructor Details

#initialize(context, options = nil) ⇒ JbuilderTemplate

Returns a new instance of JbuilderTemplate.



15
16
17
18
19
20
# File 'lib/jbuilder/jbuilder_template.rb', line 15

def initialize(context, options = nil)
  @context = context
  @cached_root = nil

  options.nil? ? super() : super(**options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject (private)



146
147
148
149
150
151
152
153
154
# File 'lib/jbuilder/jbuilder_template.rb', line 146

def set!(name, object = BLANK, *args)
  options = args.first

  if args.one? && _partial_options?(options)
    _set_inline_partial name, object, options.dup
  else
    super
  end
end

Class Attribute Details

.template_lookup_optionsObject

Returns the value of attribute template_lookup_options.



10
11
12
# File 'lib/jbuilder/jbuilder_template.rb', line 10

def template_lookup_options
  @template_lookup_options
end

Instance Method Details

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



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jbuilder/jbuilder_template.rb', line 122

def array!(collection = [], *args)
  options = args.first

  if args.one? && _partial_options?(options)
    options = options.dup
    options[:collection] = collection
    _render_partial_with_options options
  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


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jbuilder/jbuilder_template.rb', line 72

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


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

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'


95
96
97
98
99
100
101
102
103
# File 'lib/jbuilder/jbuilder_template.rb', line 95

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


54
55
56
57
58
59
60
61
62
# File 'lib/jbuilder/jbuilder_template.rb', line 54

def partial!(*args)
  if args.one? && _is_active_model?(args.first)
    _render_active_model_partial args.first
  else
    options = args.extract_options!.dup
    options[:partial] = args.first if args.present?
    _render_partial_with_options options
  end
end

#set!(name, object = BLANK, *args) ⇒ Object Also known as: method_missing



134
135
136
137
138
139
140
141
142
# File 'lib/jbuilder/jbuilder_template.rb', line 134

def set!(name, object = BLANK, *args)
  options = args.first

  if args.one? && _partial_options?(options)
    _set_inline_partial name, object, options.dup
  else
    super
  end
end

#target!Object



118
119
120
# File 'lib/jbuilder/jbuilder_template.rb', line 118

def target!
  @cached_root || super
end