Posted: December 29th, 2009 | Author: gozali | Filed under: Programming, Rails, Ruby, Work | Tags: pdf, prawn, prawnto, ruby, ruby on rails | 1 Comment »
I was getting stuck how to generate dynamic file name for prawn generated pdf file, because there’s lack of example and document here http://cracklabs.com/prawnto/demos
What is Prawnto? Prawnto is is a rails plugin leveraging the new prawn library to produce compiled pdf views, as for me i’m using it for custom reporting tools.
OK, let’s getting back to the problem, what should you do is place prawnto definition in your method rather than in controller’s callbacks :
# /app/controllers/transactions_controller.rb
1
2
3
4
5
6
7
8
9
| def show
prawnto :prawn => { :page_size => 'A4',
:left_margin => 40,
:right_margin => 30,
:top_margin => 40,
:bottom_margin => 30},
:filename => "invoice_#{@transaction.id}_customer_#{@transaction.customer_id}.pdf"
render :layout => false
end |
# /app/views/transactions/show.pdf.prawn
1
| <%= link_to "Generate PDF", transaction_url(transaction.id, :format => 'pdf') %> |
Hope this is useful, cheers
Posted: December 15th, 2009 | Author: gozali | Filed under: Programming, Ruby | Tags: ruby, snippets, tips | 2 Comments »
Sebagai contoh kita ingin menggabungkan dua buah Array dengan elemen yang bervariasi, misalnya String dan Integer dan membuang elemen yang duplicated.
Ilustrasinya sebagai berikut :
1
2
| x = [1, 2, 3, 4]
y = ["1", "2", "3", 5] |
Array x memiliki elemen dengan tipe data Integer sedangkan Array y memiliki String dan Integer yang sebenarnya jika String tersebut diubah tipe datanya menjadi Integer akan memiliki elemen yang hampir sama dengan Array x. Lalu bagaimana cara agar ketika kedua Array tersebut digabungkan kita mendapatkan Array dengan data yang unik?
Caranya sangat sederhana dan cukup satu baris saja di Ruby :
Dari String ke Integer
1
2
3
4
| irb(main):003:0> z = (x+ y).flatten.collect { |i| i.to_i }
=> [1, 2, 3, 4, 1, 2, 3, 5]
irb(main):004:0> z.uniq
=> [1, 2, 3, 4, 5] |
Dari Integer ke String :
1
2
3
4
| irb(main):005:0> z = (x+ y).flatten.collect { |i| i.to_s }
=> ["1", "2", "3", "4", "1", "2", "3", "5"]
irb(main):006:0> z.uniq
=> ["1", "2", "3", "4", "5"] |
Atau bahkan Float
1
2
3
4
| irb(main):007:0> z = (x+ y).flatten.collect { |i| i.to_f }
=> [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 5.0]
irb(main):008:0> z.uniq
=> [1.0, 2.0, 3.0, 4.0, 5.0] |
Demikian, semoga bermanfaat
Posted: December 10th, 2009 | Author: gozali | Filed under: Programming, Ruby | Tags: jakarta.rb, meetup, ruby, ruby on rails | No Comments »
The last meeting Jakarta.rb in 2009 is going to be:
When:
Saturday, December 12nd at 10 AM
Where:
detikcom office
Gedung Aldevco Octagon Lt. 2,
Jl. Warung Jati Barat Raya No. 75
Jakarta Selatan
Google Map Link
Call me: 0818-0308-1790 if you get lost.
The building is across the street from the Pizza Hut Warung Buncit and Buncit Indah Busway Shelter. The entrance to the building is in back by the parking lot.
Topics:
We will have 2 presentations no longer then 30 minutes each:
BDD with Cucumber – by Kunto Aji (http://www.railsmine.net)
W2tags Live – by Widi Harsojo of Koprol (http://wharsojo.wordpress.com)
We are opening open submission, we welcome all your resources (codes, videos, documentations, slides, presentation) for our next meetup