From 48ace490fec6f00d6e25bdce25576ff9181dcd18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Gra=CC=88fe?= Date: Sun, 13 Jul 2025 17:49:10 +0200 Subject: [PATCH] Make the rendering of Prawn templates idempotent --- lib/tilt/prawn.rb | 3 +-- test/tilt_prawntemplate.prawn | 1 + test/tilt_prawntemplate_test.rb | 7 ++++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/tilt/prawn.rb b/lib/tilt/prawn.rb index 35ad8a3..63cd7e5 100644 --- a/lib/tilt/prawn.rb +++ b/lib/tilt/prawn.rb @@ -10,11 +10,10 @@ class PrawnTemplate < Template def prepare @options[:page_size] = 'A4' unless @options.has_key?(:page_size) @options[:page_layout] = :portrait unless @options.has_key?(:page_layout) - @engine = ::Prawn::Document.new(@options) end def evaluate(scope, locals, &block) - pdf = @engine + pdf = ::Prawn::Document.new(@options) locals = locals.dup locals[:pdf] = pdf super diff --git a/test/tilt_prawntemplate.prawn b/test/tilt_prawntemplate.prawn index 91603c3..4b7d0b2 100644 --- a/test/tilt_prawntemplate.prawn +++ b/test/tilt_prawntemplate.prawn @@ -1 +1,2 @@ pdf.text "Hello Template!" +pdf.start_new_page diff --git a/test/tilt_prawntemplate_test.rb b/test/tilt_prawntemplate_test.rb index abd0840..c908def 100644 --- a/test/tilt_prawntemplate_test.rb +++ b/test/tilt_prawntemplate_test.rb @@ -9,6 +9,10 @@ def initialize(pdf_raw) def text @reader.pages.map(&:text).join end + + def page_count + @reader.page_count + end def page_attributes(page_num=1) @reader.page(page_num).attributes @@ -39,7 +43,8 @@ def page_attributes(page_num=1) template = Tilt::PrawnTemplate.new("test/tilt_prawntemplate.prawn") 3.times do output = _PdfOutput.new(template.render) - assert_includes output.text, "Hello Template!" + assert_equal 2, output.page_count + assert_equal output.text, "Hello Template!" end end