Working with Ruby
Occasional blog posts about Ruby updates, tools, editor tweaks, and random snippets. You might also be interested in my newer project that docuements lesser-known features in Ruby: Idiosyncratic Ruby.

The CARB stack: Coffee + Angular + Rails + Bower

AngularJS is a great way to build modern web apps and this affects Rails programmers. The combination of both is an excellent choice: Build your single-page app in Angular and let it communicate via JSON with your Rails-API backend (btw, this is exactly what we did for palava). However, you might ask yourself, what the best way to combine Angular and Rails is. Should you use the JavaScript tools world (grunt, yoeman, etc.) or should you prefer the Ruby tools (thor, sprockets)?

Of course, this decision depends on you and your background. For me, the answer is clear: I want to stay in Ruby land. I am familiar with it and it works very well. Why should I throw the tools away for the same functionality, just because they are built in JavaScript? Don’t get me wrong, you should try out yoeman for a new angular project, it is fantastic. But nevertheless.. I like the Ruby stack. The CARB stack.

What’s out there?

Before I’ll promote my own gem, a quick overview of projects with similar scope:

angularjs-rails is an easy way to make Angular available from sprockets. However, I will use a more generic method to achieve this, so we won’t need it.

Ebenezer is an approach to normally use Rails, but use the JavaScript stack at the same time. Check out the blog posts, if this sounds interesting to you.

AngularJS Rails Resource sits on the angular side. It makes communicating with your Rails backend super simple. Awesome!

The bottom two projects are generators for working with Angular. I find generators extremely useful. Their purpose is not to automatically create tons of code, it is more about creating the right files in the right places with minimal boilerplate content to guide you. Especially the ng-rails gem is designed very well!

To conclude, what should an auxiliary gem do for you? In my opinion, it should:

  • manage including AngularJS in Rails
  • contain generators
  • give me tools on the Angular side to work with Rails

It should enforce conventions, but leave the programming to the programmer.

Bower is simple

I am not fan of using bundler/gems to include a frontend JavaScript library, since it adds more complexity to dependency management. There are better solutions for managing frontend libraries and one of them is Bower by twitter. It is easy to use and super lightweight (some people even say, it is not much more than a fancy link shortener). While it does not have too much features, using it allows you to always have the overview of which versions of libraries you are running. I am often confused about this information when using a JavaScript library that comes out of a gem. And, of course, it is better than using plain JavaScript files without dependency management at all!

This is the B of CARB.

Coffee is good

You should use CoffeeScript. It is popular amongst Ruby programmers. For a reason.

This is the C of CARB.

Introducing Regular (Rails + Angular)

Regular is my attempt to implement the CARB stack. It depends on Bower to provide Angular. It comes with generators that promote a convention to organize your Angular project files. It generates Coffee. And it advices you to use AngularJS Rails Resource, because other developers have already thought about how to work with Rails from Angular.

Creative Commons License