Ruby Ruport实践—简单报表系统
开发环境
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
相关文档:
使用 will_paginate 进行分页和简单查询
在命令行下使用 gem install will_paginate 命令,出现下面结果安装成功
打开 books_controller.rb (你自己的控制器)
注释掉查找全部的方法,使用下面的方法,已经集成根据title进行查询
Ruby代码
#@books = Book.all
@books = Book.pagina ......
Ruby 类的继承
关键字: Ruby 类的继承
一、普通方式的继承
Ruby只支持单继承
ruby 代码
class
Child < Father
......
end
Object是所有类的始祖,并且Object的实例方法 ......
%{String} 用于创建一个使用双引号括起来的字符串
%Q{String} 用于创建一个使用双引号括起来的字符串
%Q!Some String of “Characters”! <==> ” Some String of \”Characters\” “
%q{String} 用于创建一个使用单引号括起来的字符串
%q!Som ......
Bignum
+ 加
- 减
* 乘
/ 除
** 指数操作2**2 意思是2的平方
<=> 大于, ......