railsconf2011: JavaScript the newest first class citizen

This is actually from a combined set of sessions. One of the larger problems in Rails has always been how to deal with JavaScript in a proper way. There are the .js.erb files but they are not really a proper solution to the problem.

In many ways Rails has always treated JavaScript as a second class citizen. As it wasn’t a real programming language… This while in the current state of the web standards is embracing JavaScript more and more. Rails is not running far behind actually. From Rails 3.1 CoffeeScript and Sass are both introduced as first class citizens to the Rails community with the introduction of the asset pipeline. But is it really becoming that first class citizen?

JavaScript in the asset pipeline

Okay, in Rails 3.1 we present the asset pipeline… TADA! It’s not that simple… It is a new way of thinking about assets. JavaScript, image and CSS files are now all part of a properly segmented way of working.

DHH talked during the first railsconf keynotes about providing empty files and folders. For the asset pipeline in app/assets the new files and folders will be created for all the assets. This allows for better separation of concerns just by putting into different files and folders. These different assets are then combined by using SASS for CSS and Sprockets for JavaScript. Adding the magic of Uglify optimizes the JavaScript.

So that is the asset pipeline in a nutshell. Separation of concerns, dependency management and optimization are added to turn the assets into first class citizens.

JavaScript testing

Rails 3.1 is not answering all questions… JavaScript is still a second class citizen when it comes to testing. Rails has a good basic testing framework. There is not a default testing framework for JavaScript included into Rails. There are however several non-default ways to do it.

Capybara is a good way to do integration testing. The big advantage of Capybara is that it is possible to use different “driver” to run the tests. This makes it easier to test different browsers. Drawback is that it is kinda slow compared to Ruby testing. It does however have the same sexy syntax that you would expect from a testing framework.

Evergreen is a Ruby wrapper for Jasmine, a BDD JavaScript testing framework. It is best used for unit testing of stand alone functions and objects. Other then Capybara Jasmine is not good at testing integration and end results. This is better left to Capybara that can actually render the results. The beauty of Jasmine is that it’s spec for JavaScript. This allows you to have one way of doing BDD testing throughout your application, very cool!

Even though JavaScript does not have a testing framework ready to go in Rails I think they will get there some day. Maybe not Capybara, maybe not Jasmine. But it is possible to have a good set of testing tools operational for JavaScript.

Java or Coffee?

CoffeeScript is now automatically added by Rails 3.1. CoffeeScript brings a sexy syntax to JavaScript and add a couple of ways to get your code more efficient. CoffeeScript is compiled into JavaScript (live in development mode and on rake task in production) and is then deployed through the asset pipeline.

So what do I think!? I actually have no idea yet… I used CoffeeScript once and I wasn’t really that impressed. Now I see it working in the asset pipeline and I hear that it actually became better I’m really in limbo if I want to give it another shot. Sure it has a sexier syntax, sure the integration with rails looks better and it can make coding easier. On the other hand there is the idea that is compiled into something else. In ruby that is not unseen but in JavaScript it seems kinda… dumb. The language is not bad on its own (there are some quirks but you can deal with that) and what does the syntax really add beyond making it more complex… I do not have the answer, yet.

The beauty it though that it will work without CoffeeScript… if you want to! So, yes I’ll give it another go and maybe I’ll like it. If I do… expect another blog post on it ;-).


It is an exiting time to be working with Rails and especially if you have something to do with JavaScript! Better integration in the front and back, nice testing frameworks and some sexy new syntax to write JavaScript in… I’m going to be busy for a couple of weeks at least!

, , , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: