Skip to content

You should already use CoffeeScript in your Rails app!

13/12/2010

In the latest weeks, I had to write some Javascript at work and I decided to evaluate CoffeeScript.

As you may know, CoffeeScript is a language written in Javascript that compiles into… Javascript. But it has a great syntax that takes some great ideas from Ruby and Python.
If you want to discover CoffeeScript, just go through this great presentation.

I wanted to know if it is now possible and a good thing to use CoffeeScript in a production Rails application.
After some evaluations I decided that I’ll use CoffeeScript to develop my next JavaSript code. Here is why…

The most important thing is that CoffeeScript compiles into readable JavaScript. You can open your ‘compiled’ Javascript and clearly understand the code, ie. : it does not change your variables names or function names or add a lot of garbage code… So if one day I decide to stop using CoffeeScript for whatever reason, I’ll still be able to work and go on on the compiled script directly.

IMHO, the Rails gems to integrate CoffeeScript into a Rails app are know mature and work very well.

If you have never used CoffeeScript before you may want to know if it’s possible to use CoffeeScript with your existing JS librairies (jQuery, etc.), to mix JS code with CoffeeScript code, etc… And of course, it’s possible and very easy ! So you should really give it a try.

How to use CoffeeScript in your Rails application?

It’s really easy.
Barista is the Rails gem that will take care of your CoffeeScript files and compiles them into JavaScript when you request them in your application.

The latest versions of Barista depends on the coffee-script gem. This gem only auto-detects the available compilers on your computer as there are several ways to install CoffeeScript. You may choose to follow the official documentation and install it using node.js, but if you don’t want to setup node.js, you can use the therubyracer gem and you won’t have to install anything else!
therubyracer includes V8 (the fast Javascript engine from Google).

Finally the coffee-script gem depends on the coffee-script-source gem. This gem only contains the CoffeeScript source code and it is synced with official releases. Thanks to this gem, you can lock your application to a particular CoffeeScript version.

So, this was a great explanation speech… But in summary, if you want a fast way to include CoffeeScript in your Rails app, here is what you have to add to your Gemfile :

  gem 'therubyracer', :require => false
  gem 'barista', '0.7.0.pre3'

Then you may run :

  rails generate barista:install

this will generate the initializer config/initializers/barista_config.rb which you may edit to change the Barista configuration.
As I’m using SASS/SCSS which compiles from app/stylesheets to public/stylesheets/compiled, I thought that a cool configuration would be to let Barista compiles my CoffeeScript into public/javascripts/compiled :-).

So I added this line to my Barista configuration file :

  c.output_root = Rails.root.join("public", "javascripts", "compiled")

Now, restart your server and test your installation.
Create a file app/coffeescripts/hello.coffee, then add :

  $(document).ready ->
    alert "Hello world from Coffee!"

Add javascript_include_tag « compiled/hello » to one of your view and check that everything works!
Congrats! Now, a good idea would be to let Barista and Coffee in your app and write your next JS code in Coffee…
Barista will detect changes in your CoffeeScript files and compiles them after each request to your webserver (only in development environment).

To avoid writing compiled/ in front of all my compiled scripts, I added this simple helper to my project :

  def coffee_script_include_tag(*sources)
    javascript_include_tag(*(sources.map { |js| "compiled/#{js}" }))
  end

Now for the deployment part. You may version the CoffeeScript compiled files…
But I prefer to ignore them and to lock the project to a specific coffee-script-source gem version. That will ensure that all the developers work with the same CoffeeScript version.

Then I use the barista:brew rake task in my Capistrano file using an after hook.
This rake task will just compile all the CoffeeScript source files during deployment. So you can safely pack them using your prefered asset packager after compilation…

Now, have a coffee!

Publicités
3 commentaires
  1. I’ve been using coffescript for some time, I thing you should go using node.js, and just run on your app
    coffee -w -c public/javascripts
    and all coffee script will be compiled.
    Try coffee-script it makes more readable and easy to develop javascript.

  2. I haven’t used Rails and jQuery for a while and I struggled with hello.coffee because I didn’t include jQuery. Maybe you can mention to include it below the hello.coffee code.

Trackbacks & Pingbacks

  1. Delicious Bookmarks for January 16th from 11:03 to 20:54 « Lâmôlabs

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s

%d blogueurs aiment cette page :