您的位置:首页 > 编程语言 > Ruby

Ruby Ruport实践—简单报表系统

2010-05-18 15:55 309 查看
开发环境

OS:WindowsXP

Ruby:Ruby1.8.7

Rails:Rails2.3.5

Mysql:Mysql5.0.9

IDE:Rubymine2.0.1



准备工作:

安装以下gem包

gem install ruport

gem install ruport-util

gem install acts_as_reportable



本例设计的报表系统工作原理如图(纯属个人理解),以Products表为例:







接下来实现的例子将利用eval方法实现动态脚本,对可能变动的地方通过数据库保存运行时代码片断

一、创建报表运行时需要的数据表

主键自动增长

create table report_definitions

(report_definition_id integer not null,

report_code varchar(30),

report_name varchar(240),

report_sql varchar(4000),

basic_model  varchar(20),

primary key ('report_definition_id'));

 

create table report_templates

(report_template_id integer not null,

template_code varchar(30),

template_name varchar(240),

template_content varchar(4000),

template_type  varchar(10),

primary key ('report_template_id'));

 

create table report_executions

(report_execute_id integer not null,

execute_code varchar(30),

execute_name varchar(240),

report_definition_id int not null,

report_template_id int not null

primary key ('report_execute_id'));

 

create table products

(product_id int not null,

 title varchar(100),

 description varchar(100),

 price int,

 primary key('product_id'));


二、在RubyMine中利用Scaffold方法生成数据表对应的文件(包括Model,View,Controller等)



三、修改envrioment.rb

在末尾添加

require "rubygems"

require "ruport"



四、修改Products.rb

在其中添加

acts_as_reportable

使其能使用acts_as_reportable中提供的方法



五、创建ReportOuptController,代码如下:

class ReportOutputController< Ruport::Controller
  #Code here
  stage :data_sheet
  def setup
    puts "basic_model= #{options[:basicModel]}"
    self.data = eval(options[:basicModel]).report_table_by_sql(options[:sql])
  end
end
class ReportHtml < Ruport::Formatter::PDF
    renders :pdf, :for => ReportOutputController
    build :data_sheet  do
      eval(options[:outputContent])
    end
end

 class ReportPdf < Ruport::Formatter::HTML
    renders :html, :for => ReportOutputController
    build :data_sheet do
      eval(options[:outputContent])
    end
  end

  class ReportCsv < Ruport::Formatter::CSV
    renders :csv, :for => ReportOutputController
    build :data_sheet do
      eval(options[:outputContent])
      #output << data.to_csv
    end
  end




六、修改ReportExecutionsController,添加output_report方法及:simple模板

Ruport::Formatter::Template.create(:simple) do |format|
        eval("format.page = {:layout => :landscape}
         format.grouping = {:style => :separated}
         format.text = {:font_size => 20,:justification => :center}
         format.table = {:font_size => 10,:heading_font_size => 10,:maximum_width => 720,:width => 720}
         format.column = {:alignment => :center}
         format.heading = {:alignment => :center,:bold => true,:font_size=>10}")
  end
  
  def output_report
    #puts "Execute Code = #{params[:report_execute_code]}"
    report_execution = ReportExecution.find_by_execute_code(params[:report_execute_code])
    report_definition = ReportDefinition.find(report_execution.report_definition_id)
    report_template = ReportTemplate.find(report_execution.report_template_id)
    puts "=============Output Parameters========================"
    puts "execute_code = #{params[:report_execute_code]}"
    puts "report_name = #{report_definition.report_name}"
    puts "template_name = #{report_template.template_name}, template_type= #{report_template.template_type}"
    puts "======================================================"
    outputFile = ReportOutputController.render(eval(":"+report_template.template_type),:sql=>report_definition.report_sql,
                                      :outputContent=>report_template.template_content,
                                      :basicModel=>report_definition.basic_model,
                                      :template=>:simple)
     send_data outputFile,:type => "application/"+report_template.template_type,:filename => "reportExecution."+report_template.template_type
  end




七、在routes.rb中注册output_report方法

map.resources :report_executions,:collection => { :output_report => :get }




八、修改reoprt_executions/index.html.erb,添加如下代码

<h2>Ouput Report</h2>
<%form_tag({:action=>'output_report' }) do%>
  报表执行编码:<%= text_field_tag :report_execute_code,params[:report_execute_code],:size=>20 %>
  <%= submit_tag "Print"%>
<%end%>




九、准备数据库数据

INSERT INTO report_definitions VALUES ('1', 'Product List', 'PRODUCT_LIST', 'SELECT t.title,t.price,t.description type FROM products t', 'Product');

INSERT INTO report_templates VALUES ('1', 'Product Template', 'PRODUCT_TEMPLATE', 'pad(10) { add_text /'Products/' }/r/ndraw_table data  ', 'pdf');

INSERT INTO report_executions VALUES ('2', 'Product Report', 'CUXPRORT', '1', '1');




演示效果:http://localhost:3000/report_executions

















(注:eval方法可实现动态脚本的运行,请参考http://ruby-doc.org/core/classes/Kernel.html#M005922
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: