Good morning,
I’ve been working on my first Rails app. I’m replacing an old Perl
script that scrapes a bunch of data from an Oracle database, generates a
bunch of charts then builds a static webpage with the charts.
I’ve re-built it in Rails, with about 25% of the code it took to do in
Perl and the users can now get dynamic charts instead of day old charts
(or whenever the batch runs).
I have a view that contains two columns, one for a list of charts and
the other with a div container for the charts (called chartDisplay).
I have a link for a chart like below:
<%= link_to_remote( “Activities by Request”,
:update => “chartDisplay”,
:url =>{ :action => :activities_by_request,
:name => @project.name }) %>
This in turn calls the following in my controller:
def activities_by_request
@chart_data = ActiveRecord::Base.connection().execute(“very long
complicated SQL query that I haven’t figured out how to refactor with
ActiveRecord yet…”)
outfile =
File.open(‘public/data/activities_by_request_’+params[:name]+‘.csv’,
‘wb’)
CSV::Writer.generate(outfile) do |csv|
@chart_data.each do |cd|
csv << [cd[“count”],cd[“request_title”],cd[“state”]]
end
end
outfile.close
exec(“charting/RequestByProject.jar”,params[:name])
render_text ‘
<img
src="…/…/images/charts/requestByProjectBurnSmall_’+params[:name]+‘.png’
end
(Pastebin URL: http://pastebin.com/m8445781)
My problem is occurring at the exec() statement, it completely takes
over and freezes everything until it’s finished at which time the
render_text inserts the generated chart into my div. This process only
takes 4 or so seconds but in that 4 or so seconds I would like to put a
message inside the div saying “Your chart is being built” with my
spinner animation under it so the user knows that something is going on.
I put the exec in a Thread.new{} but I’m not too familiar with how
threads function so my render_text line to put up the image was firing
before the chart was built. How would I correctly make a look to check
thread.alive? to see if it’s finished or not?
Ultimately I would like for the user to click the link, populate the div
with the text and spinner, generate the chart and place it in the div
when finished. I also wasn’t sure if this was a job for an RJS template
but the few screencasts I watched didn’t give me the impression I could
do what I needed with an RJS template (which I could be very well
wrong).
However, as my first Rails app, what I’ve accomplished so far just by
tutorials and the Agile Rails book is far greater than what I could do
in the Perl tool (not to mention adding further capabilities and making
it easier to maintain).
Thanks in advance for any responses!