ddengler / prawn-grouping

A gem to add a more flexible grouping option to the prawn gem
https://github.com/ddengler/prawn-grouping
MIT License
14 stars 21 forks source link

undefined method errors in custom classes with Prawn::View mixed-in #15

Open giuseb opened 9 years ago

giuseb commented 9 years ago

I may have found a caveat in the gem. The currently recommended way to wrap Prawn in a custom class is to include Prawn::View, rather than inheriting from Prawn::Document, as it used to be the case.

But the following program will fail. Please let me know if it's too cryptic, I will provide more details (but see the Views page in Prawn's manual)

require 'prawn'
require 'prawn/grouping'

class MyView
  include Prawn::View

  def add_text
    lorem   'this would work'
    group do
      text  'this would work as well'
      lorem 'but this will raise undefined method'
    end
  end

  # this method is not recognized by code inside the group block
  def lorem(outcome)
    text outcome
  end
end

v = MyView.new
v.add_text
v.render_file 'out.pdf'

Ciao, Giuseppe

Fustrate commented 9 years ago

I'm having problems with this as well. The root of the problem, as far as I can tell, is that create_box_clone creates a new Prawn::Document and tries to run the block of code on it, instead of running it in the context of a MyView instance. When I tried to make it run in a copy of the view, it did run, but it wrote the text twice on the final pdf (so 5 lines instead of 3 in your example).