ruby-on-railspdfmodelprawnprawnto

Rails prawnto gem generate pdf from multiple-db


I have such models:

class WorkOrder < ActiveRecord::Base
  self.table_name = "work_orders"
  set_primary_key "id"
  has_many :work_order_details
  belongs_to :type
  belongs_to :client
def get_action_name(id)
    n = Action.find(id)
    logger.warn("!!!!!!!!!!!!!!! AAAA !!!!!!!!!!!!!!!!!!!")
    logger.warn(n.name)
    n.name
  end

  def get_price_name(id)
    n = ActionDetail.find_by_action_id(id)
    logger.warn("!!!!!!!!!!!!!!! AAAA !!!!!!!!!!!!!!!!!!!")
    n.price
  end

  def get_master_name(id)
    n = Master.find(id)
    logger.warn("!!!!!!!!!!!!!!! AAAA !!!!!!!!!!!!!!!!!!!")
    a = n.lastname + " " + n.firstname
    a
  end
end



class WorkOrderDetail < ActiveRecord::Base
  self.table_name = "work_order_details"
  set_primary_key "id"
  belongs_to :master
  belongs_to :action
  belongs_to :work_order
end



class Action < ActiveRecord::Base
  self.table_name = "actions"
  set_primary_key "id"
  belongs_to :specialization
  has_many :action_details
  has_many :work_order_details
end



class Master < ActiveRecord::Base
  self.table_name = "masters"
  set_primary_key "id"
  has_many :master_specializations
  has_many :work_order_details
end

I must to generate such view-like pdf

%table.zebra
  %tr
    %th id
    %th Услуга
    %th Мастер
    %th Стоимость
    %th Бонус
    %th
    %th
    %th
  -if @work_order.work_order_details.kind_of?(Array)
    - @work_order.work_order_details.each do |work_order_detail|
      %tr{:class => cycle("zebra-stripe zebra-grey zebra1", "zebra-stripe zebra-grey zebra2")}
        %td= work_order_detail.work_order_id
        %td= work_order_detail.get_action_name(work_order_detail.action_id)
        %td= work_order_detail.get_master_name(work_order_detail.master_id)
        %td= work_order_detail.get_price_name(work_order_detail.action_id)
        - count = count + work_order_detail.get_price_name(work_order_detail.action_id)
        %td= work_order_detail.bonus
    Общая сумма:
    = count
  -else
    %tr{:class => cycle("zebra-stripe zebra-grey zebra1", "zebra-stripe zebra-grey zebra2")}
      %td= @work_order.work_order_details.work_order_id
      %td=# @work_order.work_order_details.get_action_name(@work_order.work_order_details.action_id)
      %td= @work_order.work_order_details.master_id
      %td= @work_order.work_order_details.bonus
  = link_to 'Добавить услугу', new_work_order_detail_path   

it's work's fine, but when i try to do such pdf:

pdf.font_families.update(
      "Verdana" => {
        :bold => "C:/verdanab.ttf",
        :italic => "C:/verdanai.ttf",
        :normal  => "C:/verdana.ttf" })
    pdf.font "Verdana", :size => 10
pdf.text "Заказ-наряд ##{@work_order.id}", :size => 20, :style => :bold
pdf.text "##{@work_order.date_of_work_start}", :size => 8, :style => :bold

pdf.move_down(30)




 items = @work_order.work_order_details.map do |item|
  [   
    item.work_order_id
    item.get_action_name(item.action_id)
    item.get_master_name(item.master_id)
    item.get_price_name(item.action_id)
    item.bonus
  ]
end



pdf.table items, :border_style => :grid,
  :row_colors => ["FFFFFF","DDDDDD"],
  :headers => ["id", "Услуга", "Мастер", "Стоимость"]


pdf.move_down(10)

It gives me error's...

Controller's method:

def show
    @work_order = WorkOrder.find(params[:id])
    @work_order_details = WorkOrderDetail.find_all_by_work_order_id(@work_order.id)
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @work_order }
      format.pdf { render :layout => false }
    end
  end

error's:

C:/_Projects/CarService/app/views/work_orders/show.pdf.prawn:18: syntax error, unexpected tIDENTIFIER, expecting ']'
    item.get_action_name(item.action_id)
        ^
C:/_Projects/CarService/app/views/work_orders/show.pdf.prawn:22: syntax error, unexpected ']', expecting keyword_end
C:/_Projects/CarService/app/views/work_orders/show.pdf.prawn:34: syntax error, unexpected keyword_ensure, expecting keyword_end
C:/_Projects/CarService/app/views/work_orders/show.pdf.prawn:36: syntax error, unexpected $end, expecting keyword_end

So how to do pdf with fertching data from another table?

Also when i write

@work_order.work_order_details.each do |item|  
  pdf.text item.get_action_name(item.action_id)
end  

all is ok


Solution

  • Heh) Just see

    items = @work_order.work_order_details.map do |item|
      [   
        item.work_order_id
        item.get_action_name(item.action_id)
        item.get_master_name(item.master_id)
        item.get_price_name(item.action_id)
        item.bonus
      ]
    end
    

    I forget comma's. With:

     items = @work_order.work_order_details.map do |item|
      [   
        item.work_order_id,
        item.get_action_name(item.action_id),
        item.get_master_name(item.master_id),
        item.get_price_name(item.action_id),
        item.bonus
      ]
    end
    

    All is ok)