Class: TurboStreamer::Template
- Inherits:
-
TurboStreamer
- Object
- TurboStreamer
- TurboStreamer::Template
- Defined in:
- lib/turbostreamer/template.rb
Constant Summary
Constants inherited from TurboStreamer
BLANK, DependencyTracker, ENCODERS, VERSION
Class Attribute Summary collapse
-
.template_lookup_options ⇒ Object
Returns the value of attribute template_lookup_options.
Instance Method Summary collapse
- #array!(collection = BLANK, *attributes, &block) ⇒ Object
-
#cache!(key = nil, options = {}) ⇒ Object
Caches the json constructed within the block passed.
-
#cache_collection!(collection, options = {}, &block) ⇒ Object
Caches a collection of objects using fetch_multi, if supported.
-
#cache_if!(condition, *args) ⇒ Object
Conditionally catches the json depending in the condition given as first parameter.
-
#initialize(context, *args, &block) ⇒ Template
constructor
A new instance of Template.
- #partial!(name_or_options, locals = {}) ⇒ Object
Methods inherited from TurboStreamer
#_extract_collection, #child!, default_encoder_for, encode, #extract!, get_encoder, #inject!, #key!, key_format, #key_format!, key_formatter=, #merge!, #object!, #pluck!, #set!, set_default_encoder, set_default_encoder_options, #target!, #value!
Constructor Details
#initialize(context, *args, &block) ⇒ Template
Returns a new instance of Template.
11 12 13 14 |
# File 'lib/turbostreamer/template.rb', line 11 def initialize(context, *args, &block) @context = context super(*args, &block) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class TurboStreamer
Class Attribute Details
.template_lookup_options ⇒ Object
Returns the value of attribute template_lookup_options.
6 7 8 |
# File 'lib/turbostreamer/template.rb', line 6 def @template_lookup_options end |
Instance Method Details
#array!(collection = BLANK, *attributes, &block) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/turbostreamer/template.rb', line 38 def array!(collection = BLANK, *attributes, &block) = attributes. if .key?(:partial) 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
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/turbostreamer/template.rb', line 57 def cache!(key=nil, ={}) if @context.controller.perform_caching value = _cache_fragment_for(key, ) do _capture { _scope { yield self }; } end inject!(value) else yield end end |
#cache_collection!(collection, options = {}, &block) ⇒ Object
Caches a collection of objects using fetch_multi, if supported. Requires a block for each item in the array. Accepts optional ‘key’ attribute in options (e.g. key: ‘v1’).
Example:
json.cache_collection! @people, expires_in: 10.minutes do |person|
json.partial! 'person', :person => person
end
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/turbostreamer/template.rb', line 78 def cache_collection!(collection, = {}, &block) if @context.controller.perform_caching keys_to_collection_map = _keys_to_collection_map(collection, ) results = _read_multi_fragment_cache(keys_to_collection_map.keys, ) array! do keys_to_collection_map.each_key do |key| if results[key] inject!(results[key]) else value = _write_fragment_cache(key, ) do _capture { _scope { yield keys_to_collection_map[key] } } end inject!(value) end end end else array! collection, , &block end end |
#cache_if!(condition, *args) ⇒ Object
Conditionally catches 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/turbostreamer/template.rb', line 109 def cache_if!(condition, *args) condition ? cache!(*args, &::Proc.new) : yield end |
#partial!(name_or_options, locals = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/turbostreamer/template.rb', line 16 def partial!(, locals = {}) if .class.respond_to?(:model_name) && .respond_to?(:to_partial_path) @context.render(, json: self) else if .is_a?(Hash) = else if locals.one? && (locals.keys.first == :locals) = locals.merge(partial: ) else = { partial: , locals: locals } end # partial! 'name', foo: 'bar' as = locals.delete(:as) [:as] = as if as.present? [:collection] = locals[:collection] if locals.key?(:collection) end end end |