Class: JbuilderTemplate
- Defined in:
- lib/jbuilder/jbuilder_template.rb
Constant Summary
Constants inherited from Jbuilder
Jbuilder::BLANK, Jbuilder::DependencyTracker, Jbuilder::NON_ENUMERABLES
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) ⇒ 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, *args) ⇒ JbuilderTemplate
constructor
A new instance of JbuilderTemplate.
- #partial!(*args) ⇒ Object
- #set!(name, object = BLANK, *args) ⇒ Object
- #target! ⇒ Object
Methods inherited from Jbuilder
#attributes!, #call, #child!, 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.
12 13 14 15 16 |
# File 'lib/jbuilder/jbuilder_template.rb', line 12 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_options ⇒ Object
Returns the value of attribute template_lookup_options.
7 8 9 |
# File 'lib/jbuilder/jbuilder_template.rb', line 7 def @template_lookup_options end |
Instance Method Details
#array!(collection = [], *args) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/jbuilder/jbuilder_template.rb', line 84 def array!(collection = [], *args) = args.first if args.one? && () partial! .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
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jbuilder/jbuilder_template.rb', line 34 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) ⇒ 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
76 77 78 |
# File 'lib/jbuilder/jbuilder_template.rb', line 76 def cache_if!(condition, *args) condition ? cache!(*args, &::Proc.new) : 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'
57 58 59 60 61 62 63 64 65 |
# File 'lib/jbuilder/jbuilder_template.rb', line 57 def cache_root!(key=nil, ={}) if @context.controller.perform_caching 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
18 19 20 21 22 23 24 |
# File 'lib/jbuilder/jbuilder_template.rb', line 18 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
94 95 96 97 98 99 100 101 102 |
# File 'lib/jbuilder/jbuilder_template.rb', line 94 def set!(name, object = BLANK, *args) = args.first if args.one? && () _set_inline_partial name, object, else super end end |
#target! ⇒ Object
80 81 82 |
# File 'lib/jbuilder/jbuilder_template.rb', line 80 def target! @cached_root || super end |