Class: JbuilderTemplate
- Defined in:
- lib/jbuilder/jbuilder_template.rb
Constant Summary
Constants inherited from Jbuilder
Jbuilder::BLANK, Jbuilder::VERSION
Class Attribute Summary collapse
-
.template_lookup_options ⇒ Object
Returns the value of attribute template_lookup_options.
Instance Method Summary collapse
- #array!(collection = [], *args) ⇒ Object
-
#cache!(key = nil, options = {}) ⇒ Object
Caches the json constructed within the block passed.
-
#cache_if!(condition, *args, &block) ⇒ Object
Conditionally caches the json depending in the condition given as first parameter.
-
#cache_root!(key = nil, options = {}) ⇒ Object
Caches the json structure at the root using a string rather than the hash structure.
-
#initialize(context, options = nil) ⇒ JbuilderTemplate
constructor
A new instance of JbuilderTemplate.
-
#partial!(*args) ⇒ Object
Generates JSON using the template specified with the ‘:partial` option.
- #set!(name, object = BLANK, *args) ⇒ Object (also: #method_missing)
- #target! ⇒ Object
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, = nil) @context = context @cached_root = nil .nil? ? super() : super(**) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object (private)
146 147 148 149 150 151 152 153 154 |
# File 'lib/jbuilder/jbuilder_template.rb', line 146 def set!(name, object = BLANK, *args) = args.first if args.one? && () _set_inline_partial name, object, .dup else super end end |
Class Attribute Details
.template_lookup_options ⇒ Object
Returns the value of attribute template_lookup_options.
10 11 12 |
# File 'lib/jbuilder/jbuilder_template.rb', line 10 def @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) = args.first if args.one? && () = .dup [: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
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/jbuilder/jbuilder_template.rb', line 72 def cache!(key=nil, ={}) if @context.controller.perform_caching value = _cache_fragment_for(key, ) 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, ={}) 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 ], ) { 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 = args..dup [:partial] = args.first if args.present? 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) = args.first if args.one? && () _set_inline_partial name, object, .dup else super end end |
#target! ⇒ Object
118 119 120 |
# File 'lib/jbuilder/jbuilder_template.rb', line 118 def target! @cached_root || super end |