I am working to generate some PDF reports in our Rails application with Ruport. I created an initial prototype outside of Rails that works fine. The problem happened when I moved it into Rails, now whenever I generate the report it will run the formatter another time. For example if I have generated the report 3 times my complete report will appear three times in the PDF. In addition it is not a cache of the previously generated reports being kept around. If I change the data that I am generating the report off of then all of the repeated reports will be changed. I have pasted the report below, does anybody have any ideas of what might be going own? Thanks in advance.
Josh
Code:
class Reports::NewPatientController < ApplicationController
filter_access_to :all, :context => :payments
layout false
def show
table = nil
table = Ruport::Data::Table.load('public/rancho new patients by clinic.csv')
data = nil
data = FormattedController.render_pdf(:data => table, :header_text => 'Rancho Physical Therapy')
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
send_data(data,
{:type => 'application/pdf',
:filename => 'rancho new patients by clinic.pdf',
:disposition => 'inline'})
end
end
class FormattedController < Ruport::Controller::Table
stage :header, :body
def setup
table = nil
table = self.data
data_column_names = table.column_names - [table.column_names[0]]
table.add_column('Total') {|r|
t = 0
data_column_names.each {|n| t += r[n].to_i}
t
}
table << ['','','','','']
column_totals = ['Total']
data_column_names = table.column_names - [table.column_names[0]]
data_column_names.each do |name|
t = 0
table.column(name).collect{|e| t += e.to_i}
column_totals << t
end
table << column_totals
table.add_column('', :default => ' ', :position => 1)
table.add_column(' ', :default => ' ', :position => table.column_names.count - 1)
self.data= table
end
end
class FormattedPDF < Ruport::Formatter::PDF
renders :pdf, :for => FormattedController
def build_header
pdf_writer().select_font "Helvetica-Bold"
add_text options.header_text, :font_size => 20, :justification => :center
pdf_writer.select_font "Helvetica"
add_text "New Patients - By Clinic\n",:font_size => 16, :justification => :center
add_text "Jan 1, 2010 to Apr 30, 2010\n",:font_size => 16, :justification => :center
add_text "\n"
end
def build_body
build_table_body
end
end
Actually this happens with any type of formatter that I am creating custom. I am using rails 2.3.5 and ruport 1.6.3. I have done some debugging and what I have found is that the build methods in the formatter are being called the number of times that I have generated the report since Loading the rails application. I found a solution. Once I moved the code out of the rails controller I worked fine. I think something with the fact that Rails reloads the controllers for every request. Now that it is only being loaded once with all the stuff from the lib directory it is working great. I am not sure if that is a bug or is just how it works.
Josh
> --
> You received this message because you are subscribed to the Google Groups "Ruby Reports" group.
> To post to this group, send email to ruby-r...@googlegroups.com.
> To unsubscribe from this group, send email to ruby-reports...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ruby-reports?hl=en.
>
--Bob