Rails 3.1 include CoffeeScript, jQuery, Sass as default

Posted on by tkwong

This gallery contains 3 photos.

Yes, the coming Rails 3.1 is going to be bring new library as default: CoffeeScript, jQuery, and Sass.

In true Ruby and Rails style, there’s been a little controversy over today’s CoffeeScript and Sass news. While the jQuery migration was pretty much accepted due to Prototype’s position six feet under, plenty of developers are familiar with JavaScript and either feel slighted by CoffeeScript’s inclusion as a default or are concerned that it’ll act as a barrier for newcomers to Rails in future.

Given that you can “opt out” of having CoffeeScript and Sass included in your Rails projects by making a minor change to your Gemfile, this faux-controversy is nowhere near as interesting to me as the legitimate RSpec vs Test::Unit debate that DHH kicked off.

However, if you want to grab some popcorn and enjoy the bullshit, the comments on this GitHub commit page are epic. People have even gone so far to produce graphics to express their opinions. Rock and roll. There’s also a significant level of pie-slinging going on on Twitter too. lets quickly go through the details now.

jQuery – Trivial

A lot of Rails applications lean on JavaScript to provide client-side and AJAX functionality. Till now, the Prototype library had been included in Rails by default as a way to make various things easier to achieve across different browsers, but jQuery has become significantly more popular over the last few years.

Given this, DHH’s announcement passed with little controversy since Rails developers had become used to using jQuery and installing the jquery-rails gem anyway. A good move and a progression with the times – awesome.

CoffeeScript – JavaScript, Improved

CoffeeScipt, which may be new to all. From CoffeeScript : CoffeeScript is a little language that compiles into JavaScript. If started life as a Ruby project that converted a cleaner, JavaScript-esque language into JavaScript. CoffeeScript’s syntax enables you to write JavaScript in a cleaner and, often, more logical way.

Sass – CSS (yes) – HAML – HTML (no)

Sass is to CSS as CoffeeScript is to JavaScript, though to a less extreme level. In its modern form, Sass looks just like CSS but adds support for things like variables, functions, nesting, and similar useful features. Crucially, Sass is just a superset of CSS so you can still use regular CSS with Sass and it’ll Just Work™. You can then learn new features one by one and start using them as you like (I must admit, I’m a massive fan of Sass and I mostly stick to the variables and nesting features).

Reference:

How to Play with Rails 3.1, CoffeeScript and All That Jazz Right Now

Time Zones Problem

Posted on by tkwong

Time functions in Rails at some points rather shaky. So you would expect Time.now and 0.seconds.ago equal. That is not so:

Time.now => Thu May 28 23:28:37 +0200 2009 >> 0.seconds.ago => Thu, 28 May 2009 23:28:42 CEST +02:00

Closer inspection shows that Time.now a normal Ruby Time instance, while 0.seconds.ago an ActiveSupport::TimeWithZone object.

And that is quite a difference! In this code example:

>> Time.now.to_s(:db) => "2009-05-28 23:33:08" >> 0.seconds.ago.to_s(:db) => "2009-05-28 21:33:16"

The to_s (: db) method is used to find queries and named scopes, and the above difference is that the use of Time.now , Time.parse and Time.atinadvisable when the database as a parameter can be used. Rails convertsTime instances will in TimeWithZone items from ActiveRecord attributes.Alternatively Time.zone.now etc. used.
In short: u.published_at = Time.now works fine, but when you are in your environment.rb you are a local time zone, this is wrong in Rails 2.3.2:

named_scope :begun, proc{{:conditions => ['started_at < ?', Time.now]}}

Beware!