Ruby ruport practice-simple report system

Source: Internet
Author: User

Development Environment

OS: Windows XP

RUBY: ruby1.8.7

Rails: rails2.3.5

MySQL: mysql5.0.9

IDE: rubymine2.0.1

 

Preparations:

Install the following gem package

Gem install ruport

Gem install ruport-util

Gem install acts_as_reportable

 

In this example, the working principle of the Report system (which is purely personal understanding) is designed. The products table is used as an example:

 

 

The following example shows how to use the eval method to implement dynamic scripts, and store the code snippets of the runtime era through the database where changes may occur.

1. Data Tables required for report Creation

Automatic growth of primary keys

Create Table report_definitions </P> <p> (report_definition_id integer not null, </P> <p> report_code varchar (30 ), </P> <p> report_name varchar (240), </P> <p> report_ SQL varchar (4000), </P> <p> basic_model varchar (20 ), </P> <p> Primary Key ('report _ definition_id ')); </P> <p> Create Table report_templates </P> <p> (report_template_id integer not null, </P> <p> template_code varchar (30 ), </P> <p> template_name varchar (240), </P> <p> template_content varchar (4000), </P> <p> template_type varchar (10 ), </P> <p> Primary Key ('report _ template_id ')); </P> <p> Create Table report_executions </P> <p> (report_execute_id integer not null, </P> <p> execute_code varchar (30 ), </P> <p> execute_name varchar (240), </P> <p> report_definition_id int not null, </P> <p> report_template_id int not null </P> <p> Primary Key ('report _ execute_id ')); </P> <p> Create Table products </P> <p> (product_id int not null, </P> <p> title varchar (100 ), </P> <p> description varchar (100), </P> <p> price int, </P> <p> Primary Key ('product _ id'); </P> <p>

2. Use the scaffold method in rubymine to generate the files (including model, view, and controller) corresponding to the data table)

 

3. Modify envrioment. Rb

Add at the end

Require "rubygems"

Require "ruport"

 

4. modify products. Rb

Add

Acts_as_reportable

Use the method provided in acts_as_reportable

 

5. Create reportouptcontroller with the following code:

Class reportoutputcontroller <ruport: controller <br/> # code here <br/> stage: data_sheet <br/> def setup <br/> puts "basic_model =#{ options [: basicmodel]} "<br/> self. data = eval (options [: basicmodel]). report_table_by_ SQL (options [: SQL]) <br/> end <br/> class reporthtml <ruport: formatter: PDF <br/> renders: PDF,: For => reportoutputcontroller <br/> build: data_sheet DO <br/> eval (options [: outputcontent]) <br/> end </P> <p> class reportpdf <ruport: formatter: HTML <br/> renders: HTML ,: for => reportoutputcontroller <br/> build: data_sheet DO <br/> eval (options [: outputcontent]) <br/> end </P> <p> class reportcsv <ruport: formatter: CSV <br/> renders: CSV ,: for => reportoutputcontroller <br/> build: data_sheet DO <br/> eval (options [: outputcontent]) <br/> # output <data. to_csv <br/> end

 

6. Modify reportexecutionscontroller and add the output_report method and the simple template.

Ruport: formatter: Template. create (: simple) Do | format | <br/> eval ("format. page = {: Layout =>: Landscape} <br/> format. grouping = {: Style = >:separated} <br/> format. TEXT = {: font_size => 20,: Justification = >:center} <br/> format. table = {: font_size => 10,: heading_font_size => 10,: maximum_width => 720,: width => 720} <br/> format. column = {: Alignment = >:center} <br/> format. heading = {: Alignment =>: Center,: bold => true,: font_size => 10 }") <br/> end </P> <p> def output_report <br/> # puts "execute code = # {Params [: report_execute_code]} "<br/> report_execution = reportexecution. find_by_execute_code (Params [: report_execute_code]) <br/> report_definition = reportdefinition. find (report_execution.report_definition_id) <br/> report_template = reporttemplate. find (report_execution.report_template_id) <br/> puts "================ output parameters ============================= ==== "<br/> puts" execute_code = # {Params [: report_execute_code]} "<br/> puts" report_name = # {report_definition.report_name} "<br/> puts" template_name = # {report_template.template_name }, template_type =#{ report_template.template_type} "<br/> puts" ================== =======================" <br/> outputfile = reportoutputcontroller. render (eval (":" + report_template.template_type),: SQL => report_definition.report_ SQL, <br/>: outputcontent => report_template.template_content, <br/>: basicmodel => scheme, <br/>: template =>: simple) <br/> send_data outputfile,: TYPE => "application/" + report_template.template_type,: filename => "reportexecution. "+ report_template.template_type <br/> end

 

7. register the output_report method in routes. RB.

Map. Resources: report_executions,: Collection =>{: output_report =>: get}

 

8. Modify reoprt_executions/index.html. ERB and add the following code:

<H2> ouput Report </H2> <br/> <% form_tag ({: Action => 'output _ report'}) do %> <br/> report Execution Code: <% = text_field_tag: report_execute_code, Params [: report_execute_code],: size => 20%> <br/> <% = submit_tag "print" %> <br/> <% end %>

 

9. Prepare database data

Insert into report_definitions values ('1', 'product list', 'product _ list', 'select T. title, T. price, T. description type from products t', 'product'); </P> <p> insert into report_templates values ('1', 'productemplate', 'product _ template ', 'pad (10) {add_text/'products/'}/R/ndraw_table data', 'pdf '); </P> <p> insert into report_executions values ('2', 'product report', 'cuxprort ', '1', '1'); <br/>

 

Demo: http: // localhost: 3000/report_executions

 

 

 

 

 

(Note: The eval method can realize the dynamic script operation, please refer to the http://ruby-doc.org/core/classes/Kernel.html#M005922)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.