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方法实现动态脚本,对可能变动的地方通过数据库保存运行时代码片断
一、创建报表运行时需要的数据表
主键自动增长
二、在RubyMine中利用Scaffold方法生成数据表对应的文件(包括Model,View,Controller等)
三、修改envrioment.rb
在末尾添加
require "rubygems"
require "ruport"
四、修改Products.rb
在其中添加
acts_as_reportable
使其能使用acts_as_reportable中提供的方法
五、创建ReportOuptController,代码如下:
六、修改ReportExecutionsController,添加output_report方法及:simple模板
七、在routes.rb中注册output_report方法
八、修改reoprt_executions/index.html.erb,添加如下代码
九、准备数据库数据
演示效果:http://localhost:3000/report_executions
(注:eval方法可实现动态脚本的运行,请参考http://ruby-doc.org/core/classes/Kernel.html#M005922)
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)
相关文章推荐
- 程序员的机器学习入门笔记(十一):简单人脸识别系统实践
- Ruby Ruport实践—报表参数实现
- 最简单的推荐系统实践
- Ruby Ruport实践—报表参数实现(二)
- 大型网站系统架构实践(一)从简单到复杂
- 信息安全系统设计基础课程实践:简单TUI游戏设计
- 提高第15课时,实践,简单银行系统的初步框架(加强版)v2.0
- Ruby Ruport实践—中文PDF报表之FPDF
- 实践作业3:白盒测试----简单介绍被测系统DAY4
- Ruby Ruport实践—中文PDF报表之PRAWN
- 个人重构机房收费系统报表使用之最简单的显示连接
- Android 复制文本内容到系统剪贴板的最简单实践
- 第九周 课后实践:阅读程序——(1)简单的学生信息管理系统
- 大型网站系统架构实践(一)从简单到复杂
- 【Netty4 简单项目实践】七、上线前准备--内存泄漏监控、系统监控
- 20135337——Linux实践三:ELF文件格式(64位系统,简单分析)
- Android 复制文本内容到系统剪贴板的最简单实践
- 大型网站系统架构实践(一)从简单到复杂
- c语言项目实践---设计简单银行登录、存取款、改密系统
- [置顶] [操作系统实践][简单的文件系统]