Tag Archives: rails

Rails page specific JavaScript and CSS

In a Rails project that I was working on, I wanted to come up with a way to separate the JavaScript and CSS code  to be page specific.    I could do this in JavaScript with some namespace techniques such as discussed in the article Javascript Namespace Declaration   CSS appears to pose more of a problem and I was not able to find a good namespace solution other than to use prefixes.

Using Helper Methods

I finally stumbled upon a nice technique using helper methods as described in the selected answer to the post Rails 3.1 asset pipeline: how to load controller-specific scripts?

The following addresses the problem with JavaScript  – I’ll show how this can be extended to CSS later.

Continue reading

Ruby file inconsistency found in latest 1.9.3 release

I was working on a Rails project and needed to parse some dates, obtaining the times in seconds since the Epoch (January 1, 1970).

I was using code such as in the following snippet using examples from the documentation for Class: DateTime

date_time = DateTime.strptime("01-01-1930", '%m-%d-%Y')
secs = date_time.strftime('%s')

This worked fine in ruby 1.9.3p392 which I was using on a production server, returning the time in seconds as -1262304000

Using an older version of Ruby on my development machine, I was getting a positive number.   After some searches I found that a bug had been found and submitted with the title “DateTime#strftime(“%s”) overflow on 32-bit platform”.  The fix was made in revision r36265.

Latest ruby 1.9.3 release has inconsistent file revision

I downloaded Ruby 1.9.3-p448 which corresponds to revision 41675.    All well and good?

Not quite  – I was still seeing the same issue with older dates before 12/24/1935.

After some digging, I discovered that the file ext/date/date_core.c in this download does not match what should be in revision 41675.   After a lot of searching, I was unable to find an exact match for this file in its revision history, but the closest revision to what was included in the download seemed to be 35002.

Ruby Bug filed on 1.9.3 release.   Ruby 2.0 has the proper fix.

I filed bug 8580 to report the issue.   I checked the latest Ruby 2.0 release (Ruby 2.0.0-p247) and found that the fix made in r36265 was present in date_core.c.  I’ve successfully switched to using 2.0.0-p247 and now my code is happy again 🙂