###環境
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin17]
Rails 4.2.6
prawn (2.2.2)
prawn-table (0.2.2)
###問題点
データをcreated_atの月ごとでグループ化して、表を複数個表示させたいと思っています。
以下のように、distributionsレコードをcreated_atの月ごとにまとめて取得し、イテレータの中で
table arr
を記述することで、複数のテーブルを表示させようとしましたが、
一番新しい月のレコードしか取得できませんでした。
ruby
1class FreePhotographerSpecificationPDF < Prawn::Document 2 3 def initialize(photographer) 4 super( 5 page_size: 'A4', 6 top_margin: 40, 7 bottom_margin: 40, 8 left_margin: 40, 9 right_margin: 40 10 ) 11 12 # フォントを設定(明朝体) 13 font "vendor/fonts/ipaexm.ttf" 14 # インスタンス変数に代入 15 @dists = Distribution.where(created_at: Time.now.last_month.all_month) 16 17 # 下記で作成したコンポーネントを表示順に 18 render_table 19 end 20 21 private 22 def render_table 23 date_arr = Array.new 24 date = "2019-01-01" 25 head = date.to_date.beginning_of_month 26 tail = Time.current.to_date 27 while head <= tail 28 date_arr.push(head) 29 head = head.next_month 30 end 31 32 date_arr.each do |date| 33 dists = @dists.where(created_at: date.all_month) 34 if dists.present? 35 text date.strftime('%Y年%-m月') 36 move_down 10 37 arr = [[make_cell(content: '名前', width: 270, background_color: "E0E0E0" ), 38 { content: '番号', width: 80, background_color: "E0E0E0" }, 39 { content: '作成日', width: 80, background_color: "E0E0E0" }, 40 { content: '', width: 80, background_color: "E0E0E0" } 41 ]] 42 dists.each do |dist| 43 arr.push( 44 [ 45 dist.request.name, dist.request.no, dist.selected_at.tomorrow.strftime('%-m月%-d日'), '¥ ' + dist.payment_fee.to_s 46 ] 47 ) 48 end 49 table arr, header: true, cell_style: { size: 14, align: :center, valign: :center, padding_bottom: 7 }, position: :center 50 move_down 50 51 end 52 end 53 54 move_down 60 55 56 end
###知りたいこと
どのようにすれば、prawn_tableのtableメソッドをイテレータの中に組み込むことができるのかについて教えていただきたいです。
その他の方法でも、表をイテレータで回して複数個表示させる方法について知りたいです。
なにかご教示いただけることがあれば、何卒よろしくお願いいたします。
あなたの回答
tips
プレビュー