Undefined method colspan in Prawn - ruby-on-rails

I have correct code, but Prawn complains to it:
class MyClass < Prawn::Document
# ....
def def123
table main_table , width: bounds.width
end
def main_table
[[
"0","1", "2", "3", "4"
]] +
[
[{content: "data1", colspan: 4}, "111"],
[{content: "data2", colspan: 4}, "222"],
[{content:"data3", colspan: 4}, "333"]
]
end
end
by saying:
undefined method colspan= for Prawn::Table::Cell::Text:0x007fb86c3e7020
Note that I need to use width: bounds.width to be able to make the table fill a whole page.
prawn (0.12.0)

I'd recommend updating your Gem file to pull prawn directly from the git repository. Most undefined method errors are the result of using an out of date version:
gem 'prawn', :git => "https://github.com/prawnpdf/prawn.git"

Related

NoMethodError - undefined method `where_values' | Upgraded from rails 4.2 to 5.0.1

I have recently upgraded my rails app from 4.2.8 to 5.0.1 with ruby 2.4.5
Facing this error
NoMethodError - undefined method `where_values' for
Query:
PlacementActivityStep.where(query_values_hash[:query_string], query_values_hash[:values])
.where_values.select{|x| x.present?}
How do I achieve the same query in rails 5.0.1 using where_clause as where_values has been removed from the new rails version??
Reference: Ruby on Rails where_values method
Following is tested in Rails 5.2.2
First, where_values was not intended for public use, see https://bugs.ruby-lang.org/issues/12962
And to get the same behavior you must do some hacks.
I don't know what you will use the generated arel code for, I prefer to make the condition directly with arel if it's a complicated condition. And you will lose some automatic type detection.
Consider this (bad example but only to show the difference):
# model Foo have column 'bar' that is sql type string
Foo.where('bar = :one OR bar = :two', one: 55, two: nil ).to_sql
=> "SELECT `foos`.* FROM `foos` WHERE (firstname = 55 OR firstname = NULL)"
# bad, the last should be "firstname IS NULL"
# use Rails "magic"
Foo.where(bar: [ 55, nil ]).to_sql
=> "SELECT `foos`.* FROM `foos` WHERE (`foos`.`firstname` = '55' OR `foos`.`firstname` IS NULL)"
# "`firstname` IS NULL" as it should be (and '55' is a string)
# if it is the arel you want, you can use it directly
fo_a = Foo.arel_table
fo_a[:bar].eq_any([ 55, nil ]).to_sql
=> "(`foos`.`firstname` = '55' OR `foos`.`firstname` IS NULL)"
# that is the same as if you where using this in Rails 4:
Foo.where(bar: [ 55, nil ]).where_values.first.to_sql
# even if you use a hackish way you will not get the same result
Foo.where(bar: [ 55, nil ]).where_clause.send(:predicates).first.to_sql
=> "(`foos`.`firstname` = ? OR `foos`.`firstname` IS NULL)"
# '55' is gone (it's a "bind value")

Big Decimal Stored as 60.00, Returns as 0.0?

I am customizing a Spree 2.3 application in Rails 4. When I save a price.amount, it appears to save correctly in the database, but when I retrieve it, it is wrapped in the BigDecimal class and returns 0.
How can I store, or retrieve, the correct value?
# in the form
<%= number_field_tag :amount %>
# controller action
#variant.price = params[:amount]
# appears in PostgresQL database as type 'numeric'
id: 35, amount: 60.00
# retrieved in Rails console as BigDecimal class instance
# looks like the wrong amount
2.1.1 :001 > Spree::Price.find_by_id(35).amount
=> #<BigDecimal:105806d20,'0.0',9(27)>
# converts to 0.0
2.1.1 :002 > Spree::Price.find_by_id(35).amount.to_f
=> 0.0
# testing data retrieval in the console
2.1.1 :003 > ActiveRecord::Base.connection.execute("SELECT * FROM spree_prices WHERE id=35").first
=> {"id"=>"35", "variant_id"=>"35", "amount"=>"60.00", "currency"=>"USD", "deleted_at"=>nil}
2.1.1 :004 > Spree::Price.find(35)
=> #<Spree::Price id: 35, variant_id: 35, amount: #<BigDecimal:109ec4a28,'0.0',9(27)>, currency: "USD", deleted_at: nil>
The problem was in a typo in the price model decorator. The pure PostgresQL doesn't trigger the callback, but using the 'find' method does. This accounts for the seemingly impossible results above.
#original price_decorator.rb
after_initialize :set_defaults
def set_defaults
self.amount = 0.0
end
#corrected price_decorator.rb
after_initialize :set_defaults
def set_defaults
self.amount ||= 0.0
end

rails prawn undefined method rowspan

I get the following error for the code below: undefined method 'rowspan=' for #<Prawn::Table::Cell::Text:0x9477540>
table [
[{content: "<b>control</b>", rowspan: 2},
{content: "time", colspan: 2},
"order",
{content: "count", colspan: 6}]
], cell_style: {size: 10, inline_format: true}
I followed the prawn manual, and can not see what I did wrong. I am using prawn 0.12.0.
According to the Prawn google group, colspan and rowspan were not introduced until a later release. https://groups.google.com/forum/#!searchin/prawn-ruby/rowspan/prawn-ruby/G-QHFUZheMI/3a4pNnLur0EJ
Updating to the lastest master gem from github worked for me:
git clone https://github.com/prawnpdf/prawn.git
Create a directory to test the manual example.
Run bundle init to create a Gemfile in that directory and add this line
gem 'prawn', :path=>'/path/to/your/local/prawn/git/clone/dir'
Create the span_example.rb file from the manual, and set it up to use bundler Gemfile like this:
require 'rubygems'
require 'bundler/setup'
Bundler.require
pdf = Prawn::Document.generate('span_example.pdf') do
table([
["A", {content: "2x1", colspan: 2}, "B"],
[{content: "1x2", rowspan: 2}, "C", "D", "E"],
[{content: "2x2", colspan: 2, :rowspan => 2}, "F"],
["G", "H"]
])
end
Then run
bundle install
ruby span_example.rb
open span_example.pdf
Viola!

Ruby on Rails : Prawn: Positioning table is not working, getting undefined method 'position='

I am trying to position a table on right side but getting
undefined method 'position='
Here is my sample code:
table data , position: :right do
rows(0).font_style = :bold
cells.style :align => :right
cells.borders = []
end
but as per prawn document http://prawn.majesticseacreature.com/manual.pdf‎ above code should run. Where is the problem?
I just figured it out. The master branch of prawn gem has that fixed.
I added
gem 'prawn', :git => "https://github.com/prawnpdf/prawn.git", :ref => '8028ca0cd2'
instead of
gem 'prawn'
Now its working and the code matches with the manual they provided

How to center table in prawn?

I have created a table in prawn and wanted to pass the :position option by whih is also documented in the manual but it throws off an Method_missing error. It seems like this parameter doesnt exist anymore. How can I center a table in prawn?
I ran into the same error. Installing from master on Github fixed the issue.
# Gemfile
gem 'prawn', git: 'https://github.com/prawnpdf/prawn.git'
Issue thread on Github
Hope this helps
pdf.table tablename,
:border_width => 0,
:font_size => 11,
:position => :center
whether the PDF generation is from a declared class or within the controller
you need to add the following line to your Gemfile
gem 'prawn-table', '~> 0.2.1'
At the time of writing that is the gem version, this will give you access to the table methods such as position
I used it in this example and it worked
def table_items(chart_data, doc)
table = Prawn::Table.new(table_rows(chart_data), doc, {:header => true})
table.row(0).font_style = :bold
table.position = :center
table.draw
end

Resources