Class: TurboStreamer::Template

Inherits:
TurboStreamer show all
Defined in:
lib/turbostreamer/template.rb

Constant Summary

Constants inherited from TurboStreamer

BLANK, DependencyTracker, ENCODERS, VERSION

Class Attribute Summary collapse

Instance Method Summary collapse

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_optionsObject

Returns the value of attribute template_lookup_options.



6
7
8
# File 'lib/turbostreamer/template.rb', line 6

def template_lookup_options
  @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)
  options = attributes.extract_options!

  if options.key?(:partial)
    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


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/turbostreamer/template.rb', line 57

def cache!(key=nil, options={})
  if @context.controller.perform_caching
    value = _cache_fragment_for(key, options) 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, options = {}, &block)
  if @context.controller.perform_caching
    keys_to_collection_map = _keys_to_collection_map(collection, options)
    results = _read_multi_fragment_cache(keys_to_collection_map.keys, options)
    
    array! do
      keys_to_collection_map.each_key do |key|
        if results[key]
          inject!(results[key])
        else
          value = _write_fragment_cache(key, options) do
            _capture { _scope { yield keys_to_collection_map[key] } }
          end
          inject!(value)
        end
      end
    end
  else
    array! collection, options, &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!(name_or_options, locals = {})
  if name_or_options.class.respond_to?(:model_name) && name_or_options.respond_to?(:to_partial_path)
    @context.render(name_or_options, json: self)
  else
    if name_or_options.is_a?(Hash)
      options = name_or_options
    else
      if locals.one? && (locals.keys.first == :locals)
        options = locals.merge(partial: name_or_options)
      else
        options = { partial: name_or_options, locals: locals }
      end
      # partial! 'name', foo: 'bar'
      as = locals.delete(:as)
      options[:as] = as if as.present?
      options[:collection] = locals[:collection] if locals.key?(:collection)
    end
    
    _render_partial_with_options options
  end
end