Wednesday, March 14, 2012

Ruby and RoR Course with exercise

@engineyard and @pivotallabs release an excellent course material and exercise for Ruby and Ruby on Rails.

It is one of the best I have seen so far. Quotes from the repo: "This course teaches experienced developers Ruby and Ruby-on-Rails. It's designed to be taught by a practicing rubyist and a teaching assistant. Both individuals must have expert understanding of Ruby, Rack and Ruby-on-Rails." The list of contributors: https://github.com/generalassembly/ga-ruby-on-rails-for-devs/blob/master/CONTRIBUTORS.md

Course is structured to cover all the platforms (Linux, Mac and yesss Windows). It starts with basics and covers some advanced development guidelines.

An excellent presentation by @dblockdotorg: http://www.slideshare.net/dblockdotorg/crafting-a-rubyonrails-course-for-developers

More resources: http://code.dblock.org/crafting-a-ruby-on-rails-course-for-developers

It is CDE --> Community Driven Education :)





Sunday, March 11, 2012

File download indicator in browser

For last few days I have been struggling with a requirement to show file download indicator. Here's the requirement.

Web page has a link "Download File". On click of this link - a JSON post request is sent to the server. The server generates the files based on the JSON input and streams back with Content-Disposition "attachment" for the browser to download. There are several ways to visually indicate the click of the link:
  • Normal form post - Create a dynamic form and post it. On the server-side make sure content disposition is set to attachment. When browser receives the response, it pops the "Open-Save-Cancel" dialogue box. 
           Cons:
               - No control of the page response time, page dies in case of timeouts
               - Page refreshes and the current context of the user is lost
  • Ajax post - Do an Ajax form post with JSON data. Download "waiting" indicator can be easily initiated with "Ajax prefilter". In the response from the server send back the URL to download. Remove the "waiting" indicator. Change the window location to the URL and let the browser do the rest. 
           Cons:
               - Retain the file on the server and run a background cleanup
  • Iframe post (recommended solution) - Create a dynamic form and post (target of the form) it to an iframe. Disable the download link, show the "waiting" indicator. Start the timer and look for presence of a cookie. If the cookie is found, remove the indicator and enable the link. This requires some server side code as well: adding the cookie. The detailed sample code:
          
          
          The client side code: 
          
       
         The server-side code:
         
       
           Cons:
               - Server has to set an extra cookie

The third approach has helped in solving for the following:
1. Submit a JSON request to the server
2. Show/Hide a "waiting" indicator
3. Controlling the timeouts and indicate the user of delays (if any)
4. File download handle

Let me know if there are other ways of making it work.