Ruby ruport practice-ruport: controller and template Application

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

 

1. Create productaggragator to implement data extraction logic. In this example, some parameters of product. report_table are dynamically transmitted.

Class productaggregator <br/> # code here <br/> def initialize (options ={}) <br/> @ totalfield = options [: totalfield] <br/> @ showfield = options [: showfield] <br/> @ userfield = options [: userfield] <br/> @ renamefield = options [: renamefield] <br/> @ conditions = options [: conditions] <br/> end <br/> def generatetable <br/> @ table = product. report_table (: All,: Only =>@ showfield, <br/>: Include =>{: user =>{: only => @ Userfield }}, <br/>: conditions => @ conditions) <br/> unless @ renamefield. Empty? <Br/> replacecolumnname () <br/> end </P> <p> unless @ totalfield. Empty? <Br/> showtotal () <br/> end <br/> def replacecolumnname <br/> unless @ renamefield. Empty? <Br/> @ renamefield. each do | key, value | <br/> @ table. rename_column (Key, value) <br/> transfershowfield (Key, value) <br/> end <br/> def transfershowfield (key, value) <br/> for I in 0..@showField.length-1 <br/> If (@ showfield [I] = key) <br/> @ showfield [I] = value <br/> end <br/> def showtotal <br/> total = {} <br/> for I in 0..@showField.length-1 <br/> if (I = 0) <br/> total. store (@ showfield [I], "Total") <br/> elsif (@ showfield [I] ==@ totalfield) <br/> total. store (@ showfield [I], @ table. SIGMA ("price") <br/> else <br/> total. store (@ showfield [I], "") <br/> end <br/> @ table <total <br/> end

 

Ii. Create a basecontroller to implement the ruport: Controller Application

Class basecontroller <ruport: controller <br/> stage: product_sheet <br/> def setup <br/> self. data = productaggregator. new (: showfield => options [: showfields], <br/>: userfield => options [: userfields], <br/>: totalfield => options [: totalfield], <br/>: renamefield => options [: renamefield], <br/>: conditions => options [: conditions]). generatetable <br/> end <br/> # equivalent to class HTML <ruport: formatter: HTML <br/> formatter: html do <br/> build: product_sheet DO <br/> output <textile ("H3. products List") <br/> output <data. to_html <br/> end <br/> # equivalent to class PDF <ruport: formatter: PDF <br/> formatter: PDF do <br/> build: product_sheet DO <br/> pad (10) {add_text "products list"} <br/> draw_table data <br/> end </P> <p> formatter: csv do <br/> build: product_sheet DO <br/> output <data. to_csv <br/> end </P> <p> end <br/>

 

3. Report output application in productscontroller

Different report printing templates can be dynamically passed through the specified template.

Def save_as_report <br/> ruport: formatter: Template. create (: simple) Do | format | <br/> format. page = {: Layout =>: Landscape} <br/> format. grouping = {: Style = >:separated} <br/> format. TEXT = {: font_size => 16,: Justification = >:center} <br/> format. table = {: font_size => 10,: heading_font_size => 15,: maximum_width => 720,: width => 720} <br/> format. column = {: Alignment = >:center} <br/> format. head Ing = {: Alignment =>: Center,: bold => true} <br/> end <br/> # interface parameters <br/> pdf = basecontroller. render_pdf (: showfields => ["Description", "title", "price"], <br/>: userfields => ["username"], <br/>: totalfield => "price", <br/>: renamefield => {"Description" => "type", "title" => "name", "user. username "=>" user "},< br/>: conditions => [" title like? "," % A % "], <br/>: template =>: simple) <br/> send_data PDF,: TYPE =>" application/pdf ", <br/>: filename => "products.pdf" <br/> end

 

4. Call report printing in product/index.html. ERB

<% = Link_to 'Save As report',: controller => "Products",: Action => "save_as_report" %>

 

5. Specify in routes. RB that the save_as_report method is called as get.

# Add,: Collection =>{: save_as_report =>: execution of the query condition <br/> map. Resources: Products,: Collection =>{: save_as_report =>: get}

 

Report printing result:

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.