Class: PbbuilderTemplate
- 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
-
.template_lookup_options ⇒ Object
Returns the value of attribute template_lookup_options.
Instance Method Summary collapse
-
#cache!(key = nil, options = {}) ⇒ Object
Caches fragment of message.
-
#cache_if!(condition, *args, &block) ⇒ Object
Conditionally caches the protobuf message depending on the condition given as first parameter.
-
#initialize(context, message) ⇒ PbbuilderTemplate
constructor
A new instance of PbbuilderTemplate.
-
#partial!(*args) ⇒ Object
Render a partial.
- #set!(field, *args, **kwargs, &block) ⇒ Object
Methods inherited from Pbbuilder
#attributes!, #extract!, #merge!, #method_missing, #respond_to_missing?, #target!
Constructor Details
#initialize(context, message) ⇒ PbbuilderTemplate
Returns a new instance of PbbuilderTemplate.
14 15 16 17 |
# File 'lib/pbbuilder/template.rb', line 14 def initialize(context, ) @context = context super() end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Pbbuilder
Class Attribute Details
.template_lookup_options ⇒ Object
Returns the value of attribute template_lookup_options.
9 10 11 |
# File 'lib/pbbuilder/template.rb', line 9 def 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’
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/pbbuilder/template.rb', line 86 def cache!(key=nil, ={}) if @context.controller.perform_caching value = _cache_fragment_for(key, ) 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
107 108 109 |
# File 'lib/pbbuilder/template.rb', line 107 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
25 26 27 28 29 30 31 |
# File 'lib/pbbuilder/template.rb', line 25 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/pbbuilder/template.rb', line 33 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 elsif kwargs.has_key?(:collection) && kwargs.has_key?(:as) # pb.friends partial: "racers/racer", as: :racer, collection: [Racer.new(1, "Johnny Test", []), Racer.new(2, "Max Verstappen", [])] # collection renderer = kwargs.deep_dup .reverse_merge! locals: .except(:partial, :as, :collection, :cached) .reverse_merge! ::PbbuilderTemplate. collection = [:collection] || [] partial = [:partial] [:locals].merge!(pb: self) [:locals].merge!(field: field) if .has_key?(:layout) raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering." end if .has_key?(:spacer_template) raise ::NotImplementedError, "The `:spacer_template' option is not supported in collection rendering." end CollectionRenderer .new(@context.lookup_context, ) { |&block| _scope([field.to_s],&block) } .render_collection_with_partial(collection, partial, @context, nil) 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 (kwargs) end end else super end end |