Sha256: 578ee6e3b5fe247612d71f2bcb51e83038d7bb6ccb0135506da7ea75dbd08b29

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

# -*- coding: utf-8 -*-
require "rexml/document"

class JasperSourceBuilder
  def initialize(data_source, model, record)
    @out_doc = REXML::Document.new()
    @out_doc.add(REXML::XMLDecl.new(version="1.0", encoding="UTF-8"))
    elem = REXML::Element.new(model)
    elem.add_attribute("type","array")

    data_source.each do |data|
      row = REXML::Element.new(record)
      data.each_pair do |k,v|
        cell = REXML::Element.new(k.to_s)
        cell.add_text(v.to_s)
        row.add_element(cell)
      end
      elem.add_element(row)
    end
    @out_doc.add(elem)
  end

  def to_xml(dummy)
    @out_doc.to_s
  end
end

class ActionController::Base
  private
  def jasper_pdf(arg)
    model_name = arg[:model] || "jasper"
    record_name = arg[:record] || "record"
    unless arg[:template].nil?
      jasper_file = File.join("app/views",arg[:template])
      jasper_file += ".jasper" unless jasper_file =~ /.jasper$/
    else
      jasper_file = File.join("app/views",params[:controller],params[:action]) + ".jasper"
    end
    if arg[:resource].size > 0 && arg[:resource][0].kind_of?(Hash)
      resource = JasperSourceBuilder.new(arg[:resource], model_name, record_name)
    else
      resource = arg[:resource]
    end
	  jasper_params = arg[:params] || params
    options = arg[:options] || {}
    send_data JasperRails::Jasper::Rails.render_pdf(jasper_file, resource, jasper_params, options), :type => Mime::PDF
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hashed-jasper-rails-0.3.0 lib/hashed-jasper-rails/jasper_source_builder.rb