August 3, 2008
Getting back to the drawing board…

Drawing boards are so much more fun than paper’n pencil ![]()

| Online Casinos - A guide to the best online casinos, gambling sites and casino bonuses |

Drawing boards are so much more fun than paper’n pencil ![]()
Introducing - LITBox Helper for Rails!
Disclaimer: This is a work in progress and is available on “as is” basis.
For long, I was trying to integrate Criag Ambros’s RedBox plugin into ApnaBill.com project for implementing modal dialogs but somehow, the CSS always used to get upset, rendering the lightbox in such a way that the content which is to be shown inside it, shows below it.
Then I came across Ryan J Lowe’s LITBox(The site seems to down, try Google cache)- and boy, it rocks! Just 10 seconds setup, very light weight and looks brilliant! The code is nicely written too. But the only drawback was that it was plain JS stuff - so that means no Rails helpers available.
So, once the test implementation was done with, I decided to write a small helper myself. I’ve seen how Jorge’s Prototype Window Class’s rails helper works - so decided to base my code on the same lines. And since he’s already written the Ruby->JS part for converting between optional arguments, this didn’t seemed a daunting task even for a Wednesday night
I’m trying to make it feature complete so that I can make it available for download.
Till then… happy hacking!
Its almost 4 AM now & past 3 hours saw a lot of commits going into ApnaBill.com code. After realizing that we were not generating (X)HTML code, the effort was targeted at containment, if not eradication
Many thanks to Zundra for pointing this out to me.
Due to an open div tag, the new (& slick looking) navbar wasn’t showing up on IE. HTML cleanup frenzy atleast solved that problem…
Here is what’ve changed in the code so far (straight out of our changelog)
Phew! Thats enough work for the night… onto some hollywood flick now!
Oh yes, btw, tomorrow’s a pretty hectic day. I’m watching “Horton Hears a Who” at 11AM, then going for LCG Meet at 3PM and meeting folks from www.entrip.com at 6PM. Wow! Now thats some schedule!
Rails2’s client side sessions were starting to give us troubles as we were ‘kinda’ overshooting the 4kb limit almost every now and then. Idealists would say that thats a bad practice - and I agree - but the changes now run too deep to fix them, withought having to change/throw a lot of code.
It seemed much better to fall back to server side sessions than client side. A quick search and ActiveRecordStore is one of the chosen techniques after PStore. Another quick search and you’ll come across SqlSessionStore which is much faster than ActiveRecordStore. [Comparison of various session management techniques]
Installation and configuration was super quick (thanks to the plugin) - follow the README.
However, one issue that you’ll need to address is to get mysql-ruby installed.
The next issue that’ll stare at you in the face is to constantly keep the sessions table cleaned up. And I faced a peculiar problem solving this. The timestamps of “updated_at” were running 4 hours behind the Time.now value. So stock solutions available for this problem were not working perfectly. I had to subtract an offset of “4.hours” from XX.minutes.ago value to get the correct timeouts.
desc "Cleans up stale sessions." task :clean_stale_sessions => :environment do # Determined by manual inspection. offset = 4.hours CGI::Session::ActiveRecordStore::Session.destroy_all( ['updated_at <?', YOUR_TIMEOUT.minutes.ago - offset] ) end
Hence Solved Bug #69, which solved Bug #67 and Bug #15 - wheehaa!
We just finished porting ApnaBill.com from Rails 1.2.5 to Rails 2.0.2 - the port wasn’t that tough, specially with Ben’s Rails2 Upgrade Notes on SlashDotDash.
The steps we took…
With Rails2, comes the caching options for your assets - mainly JS and CSS assets. Checkout changeset 6164 for more details - but somehow combining all css/js into single files at runtime was taking more time than loading them separately in succession. Hence I switched back to asset_packager plugin by Scott Becker - and boy, it works like a charm! It churns out a combined (white-spaces removed) combined css and js files in production mode, while keeping them separately in development mode.


JS and CSS in development mode.

![]()
JS and CSS in production code
And thanks to YSlow, I managed to squeeze a bit more of speed into rendering of the page by moving JS content to as low in the HTML content as possible. As explained by YSlow, CSS should be kept at the top of your HTML while JS as low as possible - reason being - only CSS (most of the times) is required to render the page correctly. So why waste time fetching JS content when it’ll not be needed till the page is fully rendered…

Manage to pimp ApnaBill.com’s YSlow performance grade to D (65) from below 45
Next in queue is how to manage GZip headers in the HTTP requests from ApnaBill.com
If Rails was not cool enough, caching is icing on the cake
Check this out - pulled right from the production log…
Without Caching
Completed in 0.02199 (45 reqs/sec) | Rendering: 0.02001 (90%) | DB: 0.00106 (4%) | 200 OKWith Caching
Completed in 0.00520 (192 reqs/sec) | Rendering: 0.00430 (82%) | DB: 0.00086 (16%) | 200 OK% Kick
((0.02199 - 0.00520)*100)/0.02199 == 76.35%Thats a whopping 76% speed increase in rendering the ApnaBill.com front page
Tools: content_cache plugin from Coda Hale. Check out other plugins he wrote…
Articles: RoR caching tutorial - Part1 and Part2 from Rails Envy guys…
This weekend, I was bogged down with concurrency related problems with ApnaBill.com - all of which were solved with the 62nd svn commit.
Different scenarios which we thought could have been possible
More problems arise specially when you have no control over the payment gateway pages (ie. when you are using a redirect to a payment gateway).

The newest addition to ApnaBill.com code in the payment gateway interaction arsenal
Luckily, almost all problems were solved for ApnaBill - All we expect from the user is to follow what’s shown on screen. Even otherwise, he should be good! For everything else - as they say - is the dear support staff
Like that alert box? Try http://www.bioneural.net/2006/04/01/create-a-valid-css-alert-message/
Hmm, I think, we should profile all the user displayable messages before we launch. I want to make sure that all have the same tone.
PS: Me and Sandy have never brainstormed more on any other issue than concurrency handling.
ApnaBill.com’s server is hosted on Rails Playground - which from the “Time.now” output - is not running as per IST…
This was creating problems when generating invoices & other billing related information - because Time.now was returning time which was in UTC - while 99.99% of our users expect it to be in IST.
This is how I managed to get the time in IST - You’ll need “TZInfo” gem installed on your system (gem install tzinfo -y)
>> ENV[’TZ’]=nil
=> nil
>> TZInfo::Timezone.get(”Asia/Calcutta”).utc_to_local(Time.now)
=> Mon Jan 14 07:30:53 UTC 2008
>> ENV[’TZ’]=”IST”
=> “IST”
>> TZInfo::Timezone.get(”Asia/Calcutta”).utc_to_local(Time.now)
=> Mon Jan 14 12:31:08 UTC 2008
This little trick just saved me a ton of worries - Bug fixes would be up shortly!
–Rails Ho!
This weekend was like a dream - 2 days, 2 movies, 2 major features implemented for ApnaBill, many bugs solved, slept for more than 12 hours at a stretch, played tennis & what not…
ApnaBill.com’s SVN repository is buzzing with code commits. In last two days, I have checked in code which I was expected to write in the coming week - working almost 18 hours over the two day weekend. With just a few more changes, we’ll have a full blown invoicing system - using which, a user can save or email his invoice PDF anytime for any of the coupons bought through ApnaBill.com - all registered users will enjoy this feature.
Sandy is also picking up pace & is almost about to finish his modules. With his code saftely into SVN, we would be just a few modules short of the feature complete stage. The coming two weeks would be dead hectic for me!
ApnaBill.com touched 300 beta signups this weekend. So, if you havn’t - please signup now on ApnaBill.com - our Beta Signups are going on. I can bet, it wont take more that 10 seconds!
I am at a position where I have to think in advance that how should I be coding the interactions with our (redirecting, not API based) payment gateway. What can be the best design practice - for us (as a coder), for our website (as a product) and for the end user (usability).
Keeping everything in mind together and working towards a solution is interesting. It made me think pretty nicely to come up with a solution which satisfies all scenarios (except the ones, over which I have no control).
Scenario
Your website X uses a Payment Gateway (PG). When a user Z initiates a transaction (Tx) - a Tx ID and amount value (minimalistic variables) are sent to the payment gateway. On the shopping cart side, the product which Z is buying, is locked inside the database so that some other user should not buy the same product. If the user makes a successful Tx with PG, your shopping cart is notified of the status & everyone is happy!
Problem Statement
Issues arise when the shopping cart is dependent on timeout values from the PG. There can be two worst cases in this scenario…
Solution
Note - I have not yet implemented this in any of my products - this is just a theory!
In the first scenario, open up the payment gateway as a frame inside your website - with you controlling the top header like frame & the PG controlling the other frame. Further, create a constant pingback AJAX connection to your shopping cart through the top frame. Once the AJAX connection dies, you have an almost sure shot way of knowing that the browser is no more alive, or Z’s is no longer connected. Your cart logic can then take appropriate actions of unlocking your product. - This solves problem scenario 1
The second scenario is the worst of the worsts! Browser has not been closed, and the user Z can make the Tx anytime before PG expires his Tx. What we can try here is put up a javascript clock which keeps ticking like a timer - again, we can use our top frame as thats the only part that we control when a PG is displayed. This will keep informing the user about the urgency to finish the Tx. This timer can change colors from green to yellow to red, signifying increasing priority, etc. Moreover, on the server side - one can either have a separate thread/cron-job to unlock the locked products - or - before any new access is made to the locked up DB, the cart logic an check up which all Tx’s are in a stale state & free them up. This will surely slow down the system a bit, but this would almost render the DoS attack useless. That solves problem scenario 2.
This is just my maiden attempt at Payment Gateways. If you think, I can improve my idea, feel free to add your suggestions in the comments section.
ApnaBill.com would be definitely using some (if not all) of these concepts.
Syntax highlighting for *.rhtml files - no more need to click view/highlight-mode/markup/html
http://robzon.aenima.pl/2007/10/ubuntu-710-rails-gedit-and.html
Enjoy RoR more!
Some weeks ago, I was looking for a hosting provider for my Rails project - for which I added myself to SliceHost’s waiting list.
For some reasons, I dont need the invite they’ve sent me now (I am now a happy RailsPlayground customer). If anyone from my readers is interested, I can forward you the invite. Slices start from 256Slice - $20.00/month - 10GB HD, 100GB BW
Ping me - maku at makuchaku dot info
The slice invite is valid only for 4 days from now.
If SliceHost guys think this post is against their policy, they are welcome to approach me about the same. I respect their business guidelines.
RailsPlayground has some excellent and fast customer support, but this time, I wanted to deploy my app without any of it being used - that means I had to debug the weirdo “Application Error - Rails application failed to start properly” error myself. The RailsPlayground staff had managed it the last time for me…
The best solutions, which worked instantly for me were
Though I followed 6brand’s guidelines very closely, my app was already setup in the way that was mentioned in the blog post.
The Gotcha mentioned on RubyOnRails wiki did the trick for me
REMOVE ANY pp’s AND puts FROM YOUR CODE!!!
Once done, your code would start running like it always does on your dear 127.0.0.1
Another trick that will help you alot during your initial deployments is to run your code in “development” mode by hand-editing config/environment.rb file, replacing “production” mode to “development”. There is far more information to look at in a devel log than in a production log.
Have fun!
Since login/logout functionality was so common in almost all rails apps that a gem had to exist for an “out-of-the-box” support - ask google and you’ll soon be reading LoginGenerator. Setting it up using migrations is super easy and once you go through the code, using it is almost as easy.
Upon a successful login, the generated controller sets up a “user” variable in your session which points to a copy of current user object - though you need to add a bit of code to make it function exactly like you want - specially if a user is logged in and he hits /login again, he should get the welcome message, not the login form.
Though very simplistic, LoginGenerator stores the user object inside session - which can lead to data inconsistencies. I’ll be have to be double careful when dealing with rw user objects in the session.
My app’s login/logout/signup functionality was up in just less than 2 hours, without writing (almost)any line of code. Yay!!!

Ruby Meetup, 10th Oct 07 - Atlanta
Leftmost - Zundre in white and Bob next to him ![]()
Where am I? (think green! wink wink
)