pbbuilder 0.16.0 → 0.16.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24755dfd52ac20a000c27ed88c368ef1b09977cacffc1d185f59733474247043
4
- data.tar.gz: 0dcac7ff05a509dad422976926176b921fcddcc443de24617c4218d9e5be1ada
3
+ metadata.gz: f4c9ff9f8ec71c8e3fee410430e9c29da1907e6e110ec6869531b52b8ec54ce5
4
+ data.tar.gz: 0f75e4c2fbdde5b6c33acc05dd62173b2b399d03c1030c7fd132c460d229b12b
5
5
  SHA512:
6
- metadata.gz: f6aa9b3c55c5b89a85fc081a62125b7c675c22ec638fe7e29f073dcf291b87d6f0c50af69eb87371fe9c4718131b2243a230f54913f9b325e524fdbd2203107d
7
- data.tar.gz: ecb4b6a1de74be30dc041e44f0859b8b6fab5a9e3b932503de02d53dc61b10c5e990ca0e8a631b25be2f7e92b80d6fbaa376f45dfe09e963d561a9c63a5e7a09
6
+ metadata.gz: '0191dcf320aa97316c483230dda497cc1d83c20f1aac6caa5e4ce4a8883188a949a8217344f04445ab4375b1a4bf952c49eef115af357c87c8127f1c85db3ac7'
7
+ data.tar.gz: 287458b4fc6db49f90a0d76e4da273861b58bfd84d49edd86f966198a16cb694319cd47fca69200d5cad5767307fbbe83b2fa8c866519056313b3d435c5e917e
@@ -16,13 +16,17 @@ class Pbbuilder
16
16
  end
17
17
 
18
18
  def build_rendered_collection(templates, _spacer)
19
- pb.set!(field, templates.map(&:body))
19
+ pb_parent.set!(field, templates.map(&:body))
20
20
  end
21
21
 
22
22
  def pb
23
23
  @options[:locals].fetch(:pb)
24
24
  end
25
25
 
26
+ def pb_parent
27
+ @options[:locals].fetch(:pb_parent)
28
+ end
29
+
26
30
  def field
27
31
  @options[:locals].fetch(:field).to_s
28
32
  end
@@ -49,13 +49,22 @@ class PbbuilderTemplate < Pbbuilder
49
49
 
50
50
  collection = options[:collection] || []
51
51
  partial = options[:partial]
52
- options[:locals].merge!(pb: self)
52
+
53
+ # The way recursive rendering works is that CollectionRenderer needs to be aware of node its currently rendering and parent node,
54
+ # 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.
55
+
56
+ # CollectionRenderer uses locals[:pb] to render the partial as a protobuf message,
57
+ # but also needs locals[:pb_parent] to apply rendered partial to top level protobuf message.
58
+
59
+ # This logic could be found in CollectionRenderer#build_rendered_collection method that we over wrote.
60
+ options[:locals].merge!(pb: ::PbbuilderTemplate.new(@context, new_message_for(field)))
61
+ options[:locals].merge!(pb_parent: self)
53
62
  options[:locals].merge!(field: field)
54
63
 
55
64
  if options.has_key?(:layout)
56
65
  raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering."
57
66
  end
58
-
67
+
59
68
  if options.has_key?(:spacer_template)
60
69
  raise ::NotImplementedError, "The `:spacer_template' option is not supported in collection rendering."
61
70
  end
data/lib/pbbuilder.rb CHANGED
@@ -162,6 +162,13 @@ class Pbbuilder
162
162
  @message
163
163
  end
164
164
 
165
+ def new_message_for(field)
166
+ descriptor = _descriptor_for_field(field)
167
+ ::Kernel.raise ::ArgumentError, "Unknown field #{field}" if descriptor.nil?
168
+
169
+ _new_message_from_descriptor(descriptor)
170
+ end
171
+
165
172
  private
166
173
 
167
174
  def _descriptor_for_field(field)
data/pbbuilder.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "pbbuilder"
5
- spec.version = "0.16.0"
5
+ spec.version = "0.16.1"
6
6
  spec.authors = ["Bouke van der Bijl"]
7
7
  spec.email = ["bouke@cheddar.me"]
8
8
  spec.homepage = "https://github.com/cheddar-me/pbbuilder"
@@ -12,12 +12,20 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase
12
12
  pb.extract! racer, :name
13
13
  pb.friends racer.friends, partial: "racers/racer", as: :racer
14
14
  pb.best_friend partial: "racers/racer", racer: racer.best_friend if racer.best_friend.present?
15
+ pb.logo partial: "asset", asset: racer.logo if racer.logo.present?
16
+ PBBUILDER
17
+
18
+ ASSET_PARTIAL = <<-PBBUILDER
19
+ pb.url asset.url
20
+ pb.url_2x asset.url
21
+ pb.url_3x asset.url
15
22
  PBBUILDER
16
23
 
17
24
  PARTIALS = {
18
25
  "_partial.pb.pbbuilder" => "pb.name name",
19
26
  "_person.pb.pbbuilder" => PERSON_PARTIAL,
20
27
  "racers/_racer.pb.pbbuilder" => RACER_PARTIAL,
28
+ "_asset.pb.pbbuilder" => ASSET_PARTIAL,
21
29
 
22
30
  # Ensure we find only Pbbuilder partials from within Pbbuilder templates.
23
31
  "_person.html.erb" => "Hello world!"
@@ -31,9 +39,19 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase
31
39
  end
32
40
 
33
41
  test "render collections with partial as kwarg" do
34
- result = render('pb.friends partial: "racers/racer", as: :racer, collection: [Racer.new(1, "Johnny Test", []), Racer.new(2, "Max Verstappen", [])]')
42
+ template = <<-PBBUILDER
43
+ more_friends = [Racer.new(4, "Johnny Brave", [], nil, API::Asset.new(url: "https://google.com/test3.svg"))]
44
+ friends_of_racer = [Racer.new(3, "Chris Harris", more_friends, nil, API::Asset.new(url: "https://google.com/test2.svg"))]
45
+ racers = [Racer.new(1, "Johnny Test", friends_of_racer, nil, API::Asset.new(url: "https://google.com/test1.svg")), Racer.new(2, "Max Verstappen", [])]
46
+ pb.friends partial: "racers/racer", as: :racer, collection: racers
47
+ PBBUILDER
48
+ result = render(template)
35
49
 
36
50
  assert_equal 2, result.friends.count
51
+ assert_nil result.logo
52
+ assert_equal "https://google.com/test1.svg", result.friends.first.logo.url
53
+ assert_equal "https://google.com/test2.svg", result.friends.first.friends.first.logo.url
54
+ assert_equal "https://google.com/test3.svg", result.friends.first.friends.first.friends.first.logo.url
37
55
  end
38
56
 
39
57
  test "CollectionRenderer: raises an error on a render with :layout option" do
@@ -52,14 +70,13 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase
52
70
  assert_equal "The `:spacer_template' option is not supported in collection rendering.", error.message
53
71
  end
54
72
 
55
- test " render collections with partial as arg" do
73
+ test "render collections with partial as arg" do
56
74
  skip("This will be addressed in future version of a gem")
57
75
  result = render('pb.friends "racers/racer", as: :racer, collection: [Racer.new(1, "Johnny Test", []), Racer.new(2, "Max Verstappen", [])]')
58
76
 
59
77
  assert_equal 2, result.friends.count
60
78
  end
61
79
 
62
-
63
80
  test "partial by name with top-level locals" do
64
81
  result = render('pb.partial! "partial", name: "hello"')
65
82
  assert_equal "hello", result.name
data/test/test_helper.rb CHANGED
@@ -46,6 +46,7 @@ end
46
46
 
47
47
  module API
48
48
  Person = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("pbbuildertest.Person").msgclass
49
+ Asset = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("pbbuildertest.Asset").msgclass
49
50
  end
50
51
 
51
52
  class << Rails
@@ -54,7 +55,7 @@ class << Rails
54
55
  end
55
56
  end
56
57
 
57
- class Racer < Struct.new(:id, :name, :friends, :best_friend)
58
+ class Racer < Struct.new(:id, :name, :friends, :best_friend, :logo)
58
59
  extend ActiveModel::Naming
59
60
  include ActiveModel::Conversion
60
61
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pbbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.16.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bouke van der Bijl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-23 00:00:00.000000000 Z
11
+ date: 2023-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-protobuf