Notes report
Data entry for our notes application is done. We have filter which ensures that only documents belonging to user are shown to user. Documents can be filtered with full text search. We have option to create new documents and edit already saved documents. All we need now is to create report.
It was decided to create report as Excel spreadsheet file. This way result data can further be processed by spreadsheet editor. It is also easier to create report with help of spreadsheet gem as to create pdf report.
Lets first introduce report date entry form.
title: Enter start and end date for report
form:
height: 120px;
actions:
1:
type: ajax
method: post
controller: reports
action: notes_report
caption: Run report
icon: cog
fields:
10:
name: date_from
type: date_picker
caption: Start date
20:
name: date_to
type: date_picker
caption: End date
Since our data entry form is not related to collection model we define dc_memory as table option. Our report form will have two data entry fields. Fields date_from and date_to.
Form is automatically resized when it is displayed. Height is sufficient for normal data entry. But when date_picker field is opened it is drawn over end of form. Therefor we fix form height to 120px.
Run report action will create ajax call to ReportsController.notes_report action. Report will create and save report file to file system and will instruct browser to download file and present it to user.
Report file can be found in application/reports_controller.rb file. Lets expose only code specific to DRG CMS. Rest of the code can be found in DRG examples gem.
def notes_report
date_from = DrgcmsFormFields::DatePicker.get_data( params, 'date_from' ).beginning_of_day
date_to = DrgcmsFormFields::DatePicker.get_data( params, 'date_to' ).end_of_day
#....
#data processing code
#....
tmp = 'report.xls'
book.write Rails.root.join('public',tmp)
render json: { :window_report => "/#{tmp}" }
end
end
DrgcmsFormFields::DatePicker.get_data( params, 'date_from' ) will return value of date_from field as DateTime object. If it would be plain text field its value could be accessed as params[:record][:date_from].
Other important part is also the end of report. Report will be saved to 'public/report.xls' file. Return result will be rendered as json ajax response. Return will contain directive :window_report with value of file name to be loaded. As a result report file will be downloaded in a new window.