Formatting Tables in Prawn (this seems to be a common problem) - ruby-on-rails

I"m having no luck formatting a table in a PDF served from a Rails action using Prawn.
Here's the code, but it doesn't apply any of the formatting:
p = Prawn::Document.new(:page_size => "A4")
data = []
data << ["alpha", "brava", "charlie"]
data << ["delta", "echo", "foxtrot"]
p.table(data) do
row(0).style(:background_color => 'dddddd', :size => 9, :align => :center, :font_style => :bold)
cells[0,0].background_color = '999999'
end
p.render
Any clues?

Which version of prawn are you using? I had all sorts of issues with things not working as expected, then I upgraded to prawn 0.11.1.pre and it sorted it all.
See Prawn Tables: Block is not executing which is pretty much the same question.

Related

How to make a Chartkick Multipleseries

Hi I'm practically new on ruby on rails and having a hard time on making multipleseries on chartkick.
Based on :
http://chartkick.com/
Making Chartkick multipleseries is easy as :
<%= line_chart #goals.map{|goal|
{:name => goal.name, :data => goal.feats.group_by_week(:created_at).count }
} %>
but it seems to me is not working as it should since group_by deprecated in rails 4.
is there a way to produce the same effect like the code above?
thank you

Ruby PDF Label Printer New Line Not Working

Please be patient with me, this is the first time that I have ever messed with Ruby. Basically here is my issue. I have a huge site that is just needing a single thing modified however I'm running into issues.
I am trying to get a PDF output for a lapel printer to wrap down to the next line. Here is my code.
def print_labels
quantity = params[:quantity].to_i
hashed_label = ActiveSupport::JSON.decode(params[:label])
p = InventoryItem.labels_pdf do |pdf|
(1..quantity).each do |i|
pdf.text("#{hashed_label['inventory_item_code']} #{hashed_label['label_description']} #{hashed_label['inventory_item_size']}", :style => :bold)
pdf.text("#{hashed_label['ingredients']}", :size => 9, :font => :serif)
#pdf.text(params[:label]);
pdf.start_new_page if i < quantity
end
end
send_data(p, :type => "application/x-pdf", :filename => "labels.pdf")
end
Here is the output in the PDF.
inventory_item_code label_description inventory_item_size
I am trying to get them to break between each item.
Is this using Prawn? You should be able to put newlines in all you want:
pdf.text("Here is some\ntext with\nnewlines")
So, a newline after the ingredients would just be:
pdf.text("#{hashed_label['ingredients']}\n", :size => 9, :font => :serif)

Merging cells with Ruby Gem Spreadsheet

How do I merge cells using the Ruby Spreadsheet gem. I would like to merge the first 6 cells on the first row of a worksheet. When I try the following it does not work:
merge_format = Spreadsheet::Format.new :align => :merge
6.times do |j|
sheet.row(0).set_format(j,merge_format)
end
What am I doing wrong?
You can simply do
sheet.merge_cells(start_row, start_col, end_row, end_col)
If you want to go with set_format, I'd advise trying :vertical_align => :merge, although I didn't use it since merge_cells always worked for me.

Can you recommend good data grid class/gem for Ruby on Rails?

Can you recommend good data grid class/gem for Ruby on Rails? Like http://code.google.com/p/zend-framework-datagrid/ for ZF
You can also try datagrid gem. That is focused not only grids with columns but also filters.
class SimpleReport
include Datagrid
scope do
User.includes(:group)
end
filter(:category, :enum, :select => ["first", "second"])
filter(:disabled, :eboolean)
filter(:confirmed, :boolean)
filter(:group_id, :integer, :multiple => true)
integer_range_filter(:logins_count, :integer)
filter(:group_name, :string, :header => "Group") do |value|
self.joins(:group).where(:groups => {:name => value})
end
column(:name)
column(:group, :order => "groups.name") do |user|
user.name
end
column(:active, :header => "Activated") do |user|
!user.disabled
end
end
Not sure if this is what you are looking for but checkout https://github.com/wice/wice_grid
If you are looking for a powerful client-side grid, supporting pagination, sorting, grouping, editing, export to Excel, PDF, etc, you can check Shield UI's Grid component.
Here's a tutorial on how to integrate it in Rails.
If you are looking for things like pagination, ordering, sorting etc, then rails does all this automatically.
So, for example if you wanted to sort all row by a particular column, then the title of that column could simply be a link that sorted the results by that column and then re rendered the grid.
So if you want to build a data grid that isn't AJAXy then this is pretty simple. If you are looking for a way to do it with XHR requests then you can use jQuery to make requests in the background.
As fas as a gem that does all this automatically, I couldn't find one, but I can't see why you couldn't do it yourself easily with the foundations that rails provides.

Output text size in ruby on rails

How do I get only first (say) 200 characters from my object Article (which is in a table articles with aattributes content and title)?
Thank you
The rails' TextHelper has a truncate method.
So, in your views, you just have to do :
<%= truncate #article.content, :length => 50 %>
Where 50 is the number of characters you want to display.
irb(main):004:0> '0123456789'[3,7]
=> "3456789"
irb(main):005:0> '0123456789'[3..7]
=> "34567"
irb(main):006:0> '0123456789'[3...7]
=> "3456"
Above code and outputs are self explanatory.
now there is a gem; Auto_excerpt, at github.
https://github.com/RipTheJacker/auto_excerpt.

Resources