Class: PbbuilderTemplate

Inherits:
Pbbuilder show all
Defined in:
lib/pbbuilder/template.rb

Overview

PbbuilderTemplate is an extension of Pbbuilder to be used as a Rails template It adds support for partials.

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Pbbuilder

#extract!, #merge!, #method_missing, #respond_to_missing?, #target!

Constructor Details

#initialize(context, message) ⇒ PbbuilderTemplate



10
11
12
13
# File 'lib/pbbuilder/template.rb', line 10

def initialize(context, message)
  @context = context
  super(message)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Pbbuilder

Class Attribute Details

.template_lookup_optionsObject

Returns the value of attribute template_lookup_options.



5
6
7
# File 'lib/pbbuilder/template.rb', line 5

def template_lookup_options
  @template_lookup_options
end

Instance Method Details

#cache!(key = nil, options = {}) ⇒ Object

Caches fragment of message. Can be called like the following: ‘pb.cache! “cache-key” do; end’ ‘pb.cache! “cache-key”, expire_in: 1.min do; end’



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

def cache!(key=nil, options={})
  if @context.controller.perform_caching
    value = _cache_fragment_for(key, options) do
      _scope(target!) { yield self }.to_h.compact_blank
    end

    merge! value
  else
    yield
  end
end

#cache_if!(condition, *args, &block) ⇒ Object

Conditionally caches the protobuf message depending on 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:

pb.cache_if! !admin?, @person, expires_in: 10.minutes do
  pb.extract! @person, :name, :age
end


79
80
81
# File 'lib/pbbuilder/template.rb', line 79

def cache_if!(condition, *args, &block)
  condition ? cache!(*args, &block) : yield
end

#partial!(*args) ⇒ Object

Render a partial. Can be called as: pb.partial! “name/of_partial”, argument: 123 pb.partial! “name/of_partial”, locals: 123 pb.partial! partial: “name/of_partial”, argument: 123 pb.partial! partial: “name/of_partial”, locals: 123 pb.partial! @model # @model is an ActiveModel value, it will use the name to look up a partial



21
22
23
24
25
26
27
# File 'lib/pbbuilder/template.rb', line 21

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!(field, *args, **kwargs, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pbbuilder/template.rb', line 29

def set!(field, *args, **kwargs, &block)
  # If partial options are being passed, we render a submessage with a partial
  if kwargs.has_key?(:partial)
    if args.one? && kwargs.has_key?(:as)
      # pb.friends @friends, partial: "friend", as: :friend
      # Call set! on the super class, passing in a block that renders a partial for every element
      super(field, *args) do |element|
        _set_inline_partial(element, kwargs)
      end
    else
      # pb.best_friend partial: "person", person: @best_friend
      # Call set! as a submessage, passing in the kwargs as partial options
      super(field, *args) do
        _render_partial_with_options(kwargs)
      end
    end
  else
    super
  end
end