Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable collection renderer for all collection-like objects #50

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file.

This format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.17
### Added
- Support "pb.friends "partial/path"" as single argument
- Support for fragment caching with "cached: true" parameter for collections
### Changed
- All collection-like objects are rendered with ActiveView::CollectionRenderer

## 0.16.1
### Changed
- Deal properly with recursive protobuf messages while using ActiveView::CollectionRenderer

## 0.16.0
### Added
- Added support for new collection rendering, that is backed by ActiveView::CollectionRenderer.
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pb.accounts @accounts, partial: "account", as: account
```

## Collections (or Arrays)
There are two different methods to render a collection. One that uses ActiveView::CollectionRenderer
There are two different methods to render a collection.
```ruby
pb.friends partial: "racers/racer", as: :racer, collection: @racers
```
Expand All @@ -91,7 +91,6 @@ pb.friends partial: "racers/racer", as: :racer, collection: @racers
pb.friends "racers/racer", as: :racer, collection: @racers
```

And there are other ways, that don't use Collection Renderer (not very effective probably)
```ruby
pb.partial! @racer, racer: Racer.new(123, "Chris Harris", friends)
```
Expand Down
84 changes: 49 additions & 35 deletions lib/pbbuilder/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,25 @@ def partial!(*args)
end
end

# Set value in a field in message.
#
# @example
# pb.friends @friends, partial: "friend", as: :friend
# pb.friends partial: "racers/racer", as: :racer, collection: [Racer.new(1, "Johnny Test", []), Racer.new(2, "Max Verstappen", [])]
# pb.best_friend partial: "person", person: @best_friend
# pb.friends "racers/racer", as: :racer, collection: [Racer.new(1, "Johnny Test", []), Racer.new(2, "Max Verstappen", [])]

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

_render_collection_with_options(field, args.flatten, kwargs)
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
options = kwargs.deep_dup

options.reverse_merge! locals: options.except(:partial, :as, :collection, :cached)
options.reverse_merge! ::PbbuilderTemplate.template_lookup_options

collection = options[:collection] || []
partial = options[:partial]

# The way recursive rendering works is that CollectionRenderer needs to be aware of node its currently rendering and parent node,
# these is no need to know entire "stack" of nodes. CollectionRenderer would traverse to bottom node render that first and then go up in stack.

# CollectionRenderer uses locals[:pb] to render the partial as a protobuf message,
# but also needs locals[:pb_parent] to apply rendered partial to top level protobuf message.

# This logic could be found in CollectionRenderer#build_rendered_collection method that we over wrote.
options[:locals].merge!(pb: ::PbbuilderTemplate.new(@context, new_message_for(field)))
options[:locals].merge!(pb_parent: self)
options[:locals].merge!(field: field)

if options.has_key?(:layout)
raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering."
end

if options.has_key?(:spacer_template)
raise ::NotImplementedError, "The `:spacer_template' option is not supported in collection rendering."
end

CollectionRenderer
.new(@context.lookup_context, options) { |&block| _scope(message[field.to_s],&block) }
.render_collection_with_partial(collection, partial, @context, nil)
_render_collection_with_options(field, kwargs[:collection], kwargs)
else
# pb.best_friend partial: "person", person: @best_friend
# Call set! as a submessage, passing in the kwargs as partial options
Expand All @@ -80,7 +57,12 @@ def set!(field, *args, **kwargs, &block)
end
end
else
super
if args.one? && kwargs.has_key?(:collection) && kwargs.has_key?(:as)
# pb.friends "racers/racer", as: :racer, collection: [Racer.new(1, "Johnny Test", []), Racer.new(2, "Max Verstappen", [])]
_render_collection_with_options(field, kwargs[:collection], kwargs.merge(partial: args.first))
else
super
end
end
end

Expand Down Expand Up @@ -119,6 +101,38 @@ def cache_if!(condition, *args, &block)

private

# Uses ActionView::CollectionRenderer to render collection effectively (and users fragment caching)
#
# The way recursive rendering works is that CollectionRenderer needs to be aware of node its currently rendering and parent node,
# these is no need to know entire "stack" of nodes. CollectionRenderer would traverse to bottom node render that first and then go up in stack.

# CollectionRenderer uses locals[:pb] to render the partial as a protobuf message,
# but also needs locals[:pb_parent] to apply rendered partial to top level protobuf message.

# This logic could be found in CollectionRenderer#build_rendered_collection method that we over wrote.
def _render_collection_with_options(field, collection, options)
partial = options[:partial]

options.reverse_merge! locals: options.except(:partial, :as, :collection, :cached)
options.reverse_merge! ::PbbuilderTemplate.template_lookup_options

options[:locals].merge!(pb: ::PbbuilderTemplate.new(@context, new_message_for(field)))
options[:locals].merge!(pb_parent: self)
options[:locals].merge!(field: field)

if options.has_key?(:layout)
raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering."
end

if options.has_key?(:spacer_template)
raise ::NotImplementedError, "The `:spacer_template' option is not supported in collection rendering."
end

CollectionRenderer
.new(@context.lookup_context, options) { |&block| _scope(message[field.to_s],&block) }
.render_collection_with_partial(collection, partial, @context, nil)
end

# Writes to cache, if cache with keys is missing.
#
# @return fragment value
Expand Down
2 changes: 1 addition & 1 deletion pbbuilder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |spec|
spec.name = "pbbuilder"
spec.version = "0.16.1"
spec.version = "0.17.0"
spec.authors = ["Bouke van der Bijl"]
spec.email = ["[email protected]"]
spec.homepage = "https://github.com/cheddar-me/pbbuilder"
Expand Down
20 changes: 16 additions & 4 deletions test/pbbuilder_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,19 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase
assert_equal "The `:spacer_template' option is not supported in collection rendering.", error.message
end

test "collection partial with fragment caching enabled" do
template = <<-PBBUILDER
racers = [Racer.new(1, "Johnny Test", [], nil, API::Asset.new(url: "https://google.com/test1.svg")), Racer.new(2, "Max Verstappen", [])]
pb.friends partial: "racers/racer", as: :racer, collection: racers, cached: true
PBBUILDER
result = render(template)

assert_equal 2, result.friends.count
assert_nil result.logo
assert_equal "https://google.com/test1.svg", result.friends.first.logo.url
end

test "render collections with partial as arg" do
skip("This will be addressed in future version of a gem")
result = render('pb.friends "racers/racer", as: :racer, collection: [Racer.new(1, "Johnny Test", []), Racer.new(2, "Max Verstappen", [])]')

assert_equal 2, result.friends.count
Expand Down Expand Up @@ -356,9 +367,10 @@ def build_view(options = {})

view = ActionView::Base.with_empty_template_cache.new(lookup_context, assigns, controller)

def view.view_cache_dependencies
[]
end
def view.view_cache_dependencies; [] end
def view.combined_fragment_cache_key(key) [ key ] end
def view.cache_fragment_name(key, *) key end
def view.fragment_name_with_digest(key) key end

view
end
Expand Down
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def cache
class Racer < Struct.new(:id, :name, :friends, :best_friend, :logo)
extend ActiveModel::Naming
include ActiveModel::Conversion

# For compatibility with ActiveModel::API.
def persisted?
false
end
end

Mime::Type.register "application/vnd.google.protobuf", :pb, [], %w(pb)
Expand Down