Ruby ruport practice-Report parameter implementation

Source: Internet
Author: User

This example is based on Ruby ruport practice-simple report system and Ruby ruport practice-Chinese PDF report prawn, and adds the design and implementation of report parameters.

 

1. Create a data table report_parameters

Create Table report_parameters <br/> (report_parameter_id integer not null auto_increment, <br/> report_execute_id integer not null, <br/> parameter_name varchar (240 ), <br/> parameter_value varchar (240), <br/> Primary Key "report_parameter_id") <br/> engine = InnoDB auto_increment = 4 default charset = utf8

 

Ii. Modify controllers/reportexecutionscontroller. Rb

Add the basic operation of report_parameters to the report_executions interface.

Class reportexecutionscontroller <applicationcontroller <br/> # Get/report_executions <br/> # Get/report_executions.xml <br/> 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_s Ize => 10,: maximum_width => 720,: width => 720} <br/> format. column = {: Alignment = >:center} <br/> format. heading = {: Alignment =>: Center,: bold => true,: font_size => 10 }") <br/> end <br/> def show_report_details <br/> report_execution = reportexecution. find_by_execute_code (Params [: report_execute_code]) <br/> @ report_execute_code = Params [: report_execute_code] <br/> puts "========================== show report Parameters Number =======================" <br/> @ report_params = reportexecution. find_by_ SQL ("select * From report_parameters where report_execute_id =#{ report_execution.report_execute_id}") <br/> @ report_params.each do | T | <br/> puts "# {T. parameter_name }:# {T. parameter_value} "<br/> end <br/> puts" ==================== =============================" <br/> render: action => "execute" <br/> end </P> <p> def out Put_report <br/> puts "-------------------------- output report code: # {Params [: execution_code]}" <br/> report_execution = reportexecution. find_by_execute_code (Params [: execution_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 [: execution_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_t Ype. downcase),: SQL => report_definition.report_ SQL, <br/>: parameters => Params [: parameters], <br/>: outputcontent => report_template.template_content, <br/>: template =>: simple) <br/> send_data outputfile,: TYPE => "application/" + report_template.template_type.downcase,: filename => "reportexecution. "+ report_template.template_type.downcase </P> <p> end <br/> def index <br/> @ report_executions = reportexecuti On. all <br/> respond_to do | format | <br/> format.html # index.html. ERB <br/> format. XML {render: XML =>@ report_executions} <br/> end </P> <p> # Get/report_executions/1 <br/> # Get/report_executions/1.xml< br /> def show <br/> @ report_execution = reportexecution. find (Params [: Id]) <br/> @ report_parameters = reportparameter. find_by_ SQL ("select * From report_parameters where report_execute _ Id = # {Params [: Id]} ") </P> <p> respond_to do | format | <br/> format.html # show.html. ERB <br/> format. XML {render: XML =>@ report_execution} <br/> end </P> <p> # Get/report_executions/New <br/> # Get/report_executions/New. XML <br/> def new <br/> @ report_execution = reportexecution. new <br/> respond_to do | format | <br/> format.html # new.html. ERB <br/> format. XML {render: xml => @ Report _ Execution} <br/> end </P> <p> # Get/report_executions/1/edit <br/> def edit <br/> @ report_execution = reportexecution. find (Params [: Id]) <br/> report_definition = reportdefinition. find (@ report_execution.report_definition_id) <br/> report_template = reporttemplate. find (@ report_execution.report_template_id) <br/> @ latest_report_name = report_definition.report_name <br/> @ latest_template_nam E = report_template.template_name <br/> @ report_parameters = reportparameter. find_by_ SQL ("select * From report_parameters where report_execute_id =#{ Params [: Id]}") <br/> If (@ report_parameters) <br/> puts "has parameters # {@ report_parameters.length }===================" <br/> @ report_parameters.each do | f | <br/> puts "# {f. parameter_name }:# {f. parameter_value} "<br/> end <br/> puts" has Param Eters =====================" <br/> end </P> <p> def create_report_parameters <br/> flag = true <br/> If (Params [: parameter_name]) <br/> for I in 0 .. params [: parameter_name]. length-1 <br/> If (Params [: parameter_name] [I]! = "") <Br/> report_parameter = reportparameter. new <br/> report_parameter.write_attribute ("report_execute_id", @ report_execution.report_execute_id) <br/> report_parameter.write_attribute ("parameter_name", Params [: parameter_name] [I]) <br/> report_parameter.write_attribute ("parameter_value", Params [: parameter_value] [I]) </P> <p> If! Report_parameter.save <br/> flag = false <br/> end <br/> return flag <br/> end <br/> # post/report_executions <br/> # post/report_executions.xml <br/> def create <br/> puts "----------------- create" <br/> @ report_execution = reportexecution. new (Params [: report_execution]) <br/> # @ report_parameter = reportparameter. new (Params [: report_parameter]) <br/> puts "Params [: parameter_name] :#{ Params [: parameter_name]}" <br/> puts "Params [: parameter_value]: # {Params [: parameter_value]} "<br/> respond_to do | format | <br/> If @ report_execution.save </P> <p> If create_report_parameters <br/> FLASH [: notice] = 'reportexecution was successfully created. '<br/> end <br/> format.html {redirect_to (@ report_execution)} <br/> format. XML {render: XML =>@ report_execution,: Status =>: created,: Location =>@ report_execution }< br/> else <br/> format.html {render: action => "new"} <br/> format. XML {render: xml => @ report_execution.errors,: Status =>: unprocessable_entity} <br/> end </P> <p> # Put/report_executions/1 <br/> # Put/report_executions/1.xml< br/> def update <br/> @ report_execution = reportexecution. find (Params [: Id]) <br/> puts "=======================" <br/> puts" length: # {Params [: parameter_id]} "<br/> respond_to do | format | <br/> If @ report_execution.update_attributes (Params [: report_execution]) <br/> FLASH [: notice] = 'reportexecution was successfully updated. '</P> <p> If (Params [: parameter_id]) <br/> for I in 0 .. params [: parameter_id]. length-1 <br/> report_parameter = reportparameter. find (Params [: parameter_id] [I]) <br/> report_parameter.update_attribute ("parameter_name", Params [: parameter_name] [I]) <br/> report_parameter.update_attribute ("parameter_value ", params [: parameter_value] [I]) <br/> end </P> <p> format.html {redirect_to (@ report_execution)} <br/> format. XML {head: OK} <br/> else <br/> format.html {render: Action => "edit"} <br/> format. XML {render: xml => @ report_execution.errors,: Status =>: unprocessable_entity} <br/> end </P> <p> # delete/report_executions/1 <br/> # delete/report_executions/1.xml< br/> def destroy <br/> @ report_execution = reportexecution. find (Params [: Id]) <br/> @ report_execution.destroy </P> <p> respond_to do | format | <br/> format.html {redirect_to (report_executions_url )} <br/> format. XML {head: OK} <br/> end <br/>

 

3. Modify helpers/reportexecutionshelper. Rb

Add an application from the drop-down list

Module reportexecutionshelper <br/> def report_definitions_control <br/> report_definitions = reportdefinition. find_by_ SQL ("select * From report_definitions") <br/> Index = get_selected_definition (report_definitions, @ latest_report_name) <br/> puts "latest report name: # {@ latest_report_name }, index = # {index} "<br/> return content_tag ('select', options_from_collection_for_select (report_definitions, 'Report _ definition_id ', 'Report _ name ',: selected => index),: Id => 'report _ definition ',: Name => 'report _ execution [report_definition_id]') <br/> end <br/> def report_templates_control <br/> report_templates = reporttemplate. find_by_ SQL ("select * From report_templates") <br/> Index = get_selected_template (report_templates, @ latest_template_name) <br/> puts "latest Template Name: # {@ latest_template_name }, index = # {index} "<br/> return content_tag ('select', options_from_collection_for_select (report_templates, 'Report _ template_id ', 'template _ name ',: selected => index),: Id => 'report _ template',: Name => 'report _ execution [report_template_id] ') <br/> end <br/> def report_executions_control <br/> reportexecutions = reportexecution. find_by_ SQL ("select * From report_executions") <br/> return content_tag ('select', options_from_collection_for_select (reportexecutions, 'execute _ Code', 'execute _ name '),: id => 'report _ execution ',: Name => 'report _ execute_code') <br/> end <br/> def get_selected_definition (report_definitions, latest_report_name) <br/> Index = 0 <br/> for I in 0 .. report_definitions.length-1 <br/> If (report_definitions [I]. report_name = latest_report_name) <br/> Index = I + 1 <br/> Break <br/> end <br/> return index <br/> end <br/> def get_selected_template (report_templates, latest_template_name) <br/> Index = 0 <br/> for I in 0 .. report_templates.length-1 <br/> If (report_templates [I]. template_name = latest_template_name) <br/> Index = I + 1 <br/> Break <br/> end <br/> return index <br/> end <br/>

 

4. Modify reportoutputcontroller. Rb

Convert the number-taking logic SQL statement

Class reportoutputcontroller <ruport: controller <br/> # code here <br/> stage: data_sheet <br/> def setup <br/> # self. data = reportexecution. report_table_by_ SQL (options [: SQL]) <br/> If (options [: parameters]) <br/> puts "parameter list: # {options [: parameters]} "<br/> options [: SQL] = transfersql (options [: SQL], options [: parameters]) <br/> end <br/> puts "run SQL: # {options [: SQL]}" <br/> self. data = product. report_ta Ble_by_ SQL (options [: SQL]) <br/> end <br/> def transfersql (STR, parameters) <br/> If (parameters [0] = "") <br/> parameters [0] = "null" <br/> end <br/> puts "Conversion: # {STR }, replace parameter # {parameters [0]} "<br/> If (Str. include? "? ") <Br/> puts "============ start replacement ========================" <br/> STR = Str. sub (//? /, Parameters [0]) <br/> puts "STR (# {parameters [0]}) ##{ STR} "<br/> puts" ========================== ======== "<br/> parameters. delete_at (0) <br/> If (Str. include? "? ") <Br/> puts" Replaceable Parameters "<br/> transfersql (STR, parameters) <br/> else <br/> return STR <br/> end <br/> end <br/> class ruport:: formatter: prawn <ruport: formatter: PDF <br/> def author _writer <br/> unless @ author _writer <br/> @ author _writer =: prawn: document. new <br/> @ pai_writer.font "# {:: prawn: basedir}/data/fonts/simkai. TTF "<br/> end <br/> return @ pai_writer <br/> end <br/> def render_pdf <br/> output <pai_writer.render <br/> end <br /> end </P> <p> class PDF <ruport:: formatter: prawn <br/> renders: PDF,: For => reportoutputcontroller <br/> build: data_sheet DO <br/> eval (options [: outputcontent]) <br/> end </P> <p> class reporthtml <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/> end

 

5. Modify views/report_executions/index.html. ERB.

Add the following code at the end to implement the application of the drop-down list.

<H2> ouput Report </H2> <br/> <% form_tag ({: Action => 'show _ report_details '}) do %> <br/> report Execution Code: <% = report_executions_control %> <br/> <% = submit_tag "detail" %>

 

6. Modify views/report_executions/new.html. ERB.

<H1> New report_execution </p> </P> <p> <% form_for (@ report_execution) Do | f |%> <br/> <% = f. error_messages %> </P> <p> <br/> <% = f. label: execute_code % >:< br/> <% = f. text_field: execute_code %> <br/> </P> <br/> <p> <br/> <% = f. label: execute_name % >:< br/> <% = f. text_field: execute_name %> <br/> </P> <br/> <p> <br/> <% = f. label: report_definition_id % >:< br/> <% = report_definitions_control %> <br/> </P> <br/> <p> <br/> <% = f. label: report_template_id % >:< br/> <% = report_templates_control %> <br/> </P> <br/> <% for I in 0 .. 5%> <br/> <p> <br/> parameter <% = I %>: <% = text_field_tag 'parameter _ name [] '%> <br/> value <% = I %>: <% = text_field_tag 'parameter _ value [] '%> <br/> </P> <br/> <% end %> </P> <p> <br/> <% = f. submit 'create' %> <br/> </P> <br/> <% end %> </P> <p> <% = link_to 'back ', report_executions_path %>

 

7. Modify views/report_executions/edit.html. ERB.

<H1> editing report_execution </p> </P> <p> <% form_for (@ report_execution) Do | f |%> <br/> <% = f. error_messages %> </P> <p> <br/> <% = f. label: execute_code % >:< br/> <% = f. text_field: execute_code %> <br/> </P> <br/> <p> <br/> <% = f. label: execute_name % >:< br/> <% = f. text_field: execute_name %> <br/> </P> <br/> <p> <br/> <% = f. label: report_definition_id % >:< br/> <% = report_definitions_control %> <br/> </P> <br/> <p> <br/> <% = f. label: report_template_id %>: <br/> <% = report_templates_control %> <br/> </P> <br/> <% if @ report_parameters.length> 0%> <br/> <p> report parameters </p> <p/> <br/> <% for I in 0..@report_parameters.length-1%> <br/> <p> <br/> <% = hidden_field_tag 'parameter _ id [] ', @ report_parameters [I]. report_parameter_id %> <br/> parameter <% = I %>: <% = text_field_tag 'parameter _ name [] ', @ report_parameters [I]. parameter_name %> <br/> value <% = I %>: <% = text_field_tag 'parameter _ value [] ', @ report_parameters [I]. parameter_value %> <br/> </P> <br/> <% end %> <br/> <p> <br/> <% = f. submit 'update' %> <br/> </P> <br/> <% end %> </P> <p> <% = link_to 'show ', @ report_execution %> | <br/> <% = link_to 'back', report_executions_path %>

 

8. Modify views/report_executions/show.html. ERB.

<P> <br/> <B> execute code: </B> <br/> <% = H @report_execution.exe cute_code %> <br/> </P> <p> <br/> <B> execute Name: </B> <br/> <% = H @report_execution.exe cute_name %> <br/> </P> <p> <br/> <B> report definition: </B> <br/> <% = H @ report_execution.report_definition_id %> <br/> </P> <p> <br/> <B> Report Template: </B> <br/> <% = H @ report_execution.report_template_id %> <br/> </P> <br/> <p> Parameters </p> <br/> <% if @ report_parameters.length> 0%> <br/> <Table Frame = Box> <br/> <tr> <TD> parameter name </TD> <TD> parameter value </TD> </tr> <br/> <% @ report_parameters.each do | report_parameter | %> <br/> <tr> <br/> <TD width = "150"> <% = H report_parameter.parameter_name %> </TD> <br/> <TD width = "150"> <% = H report_parameter.parameter_value %> </TD> <br/> </tr> <br/> <% end %> <br/> </table> <br/> <% end %> </P> <p> <% = link_to 'edit ', edit_report_execution_path (@ report_execution) %> | <br/> <% = link_to 'back', report_executions_path %>

 

Demo effect:

Define the logic of report fetch:

 

 

Define a Report Template

 

 

Define report execution

 

 

Execute RePort Printing

 

 

 

 

Print 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.