From 7586fec462a44ba51781032252e72ab1ed5fddac Mon Sep 17 00:00:00 2001 From: Stanislav Katkov Date: Fri, 20 Sep 2024 18:06:05 +0200 Subject: [PATCH 1/2] Test for collection with variable --- test/pbbuilder_template_test.rb | 20 ++++++++++++++++++++ test/test_helper.rb | 1 + 2 files changed, 21 insertions(+) diff --git a/test/pbbuilder_template_test.rb b/test/pbbuilder_template_test.rb index 7e17d35..93af012 100644 --- a/test/pbbuilder_template_test.rb +++ b/test/pbbuilder_template_test.rb @@ -15,6 +15,11 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase pb.logo partial: "asset", asset: racer.logo if racer.logo.present? PBBUILDER + TEAM_PARTIAL = <<-PBBUILDER + pb.extract! racer, :name + pb.team_name team_name + PBBUILDER + ASSET_PARTIAL = <<-PBBUILDER pb.url asset.url pb.url_2x asset.url @@ -26,6 +31,7 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase "_person.pb.pbbuilder" => PERSON_PARTIAL, "racers/_racer.pb.pbbuilder" => RACER_PARTIAL, "_asset.pb.pbbuilder" => ASSET_PARTIAL, + "_team.pb.pbbuilder" => TEAM_PARTIAL, # Ensure we find only Pbbuilder partials from within Pbbuilder templates. "_person.html.erb" => "Hello world!" @@ -54,6 +60,20 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase assert_equal "https://google.com/test3.svg", result.friends.first.friends.first.friends.first.logo.url end + test "should be possible to pass variable with collection" 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: "team", collection: racers, as: :racer, locals: { team_name: "Red Bull Racing" } + PBBUILDER + + result = render(template) + + assert_equal 2, result.friends.count + result.friends.each do |member| + assert_equal "Red Bull Racing", member.team_name + end + 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", [])] diff --git a/test/test_helper.rb b/test/test_helper.rb index 1de8539..431b305 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -34,6 +34,7 @@ optional :last_name, :string, 8 optional :boolean_me, :bool, 9 optional :logo, :message, 10, "pbbuildertest.Asset" + optional :team_name, :string, 11 end add_message "pbbuildertest.Asset" do From 4143a00e7bddd48402e2805b938e9fb85b36677e Mon Sep 17 00:00:00 2001 From: "Stanislav (Stas) Katkov" Date: Sat, 21 Sep 2024 00:05:29 +0200 Subject: [PATCH 2/2] CollectionRenderer doesn't worj with locals --- test/pbbuilder_template_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/pbbuilder_template_test.rb b/test/pbbuilder_template_test.rb index 93af012..76e47da 100644 --- a/test/pbbuilder_template_test.rb +++ b/test/pbbuilder_template_test.rb @@ -74,6 +74,20 @@ class PbbuilderTemplateTest < ActiveSupport::TestCase end end + test "should be possible to pass variable with CollectionRenderer" 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 racers, partial: "team", as: :racer, locals: { team_name: "Red Bull Racing" } + PBBUILDER + + result = render(template) + + assert_equal 2, result.friends.count + result.friends.each do |member| + assert_equal "Red Bull Racing", member.team_name + end + 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", [])]