Ruby on Rails

From tommorris.org
Jump to: navigation, search

This is my opinionated guide to Rails.

Contents

CoffeeScript

Add --no-coffee to ~/.railsrc and you never have to deal with CoffeeScript ever again.

Why wouldn't you want to use the amazing magical powers of CoffeeScript? Because... it doesn't actually fix what's wrong with JavaScript. The reason I hate JavaScript is because the type system sucks: it's dynamically and weakly typed. I like static typing. I like strong types. I like "==" to mean equals not "equals unless you feel like doing some type coercion magic".

I don't have a problem with writing C style languages: I like Scala, I quite like C#, I don't mind Java too much. What makes languages annoying isn't the syntax, it is the underlying logic of the semantics of those languages. No amount of CoffeeScript-style plastic surgery fixes the rot in JavaScript. But you do then get to have to deal with all the extra baggage of CoffeeScript. And you then can't easily delegate JavaScript off to front-end people... because they then have to learn CoffeeScript. Fuck that. Just turn it off.

Database

Postgres. Just use Postgres. In Rails-land, you've basically got four choices: SQLite, MySQL, Postgres or some NoSQL thing. I'm not wild about the NoSQL thing given they usually don't have any type checking... and I like type checking.

MySQL we can get rid of from the list: you can use MariaDB if you like, but Postgres is pretty much just better than MySQL or MariaDB. Postgres gives you nice things like GIS functionality, full-text search, crazy mad stuff like foreign key constraints (gem install foreigner).

SQLite isn't much use for anything beyond testing. And you should test with the same database you are going to use in production... because otherwise the differences in the type system (or the complete lack of type enforcement by SQLite rather) will bite you, as will the lack of features.

Postgres is just awesome. Use it. And turn on HStore and arrays.

HStore and testing

If you are using HStore, Postgres' excellent key-value-in-column store, you'll need to enable it for your test database too. But, the test database gets created when you run the tests and torn down afterwards. So you need to set your local Postgres to create new databases with HStore enabled by default.

Follow this post to do so. If you can't write to the template, follow this.

Testing

Dependency testing

One very simple pattern I like: dependency testing. Create an integration test called "dependencies test". In here, every external module you use, write a test for. This may just be as simple as

import "foo"
assert Foo::Bar.class != nil, "foo isn't loading"

You can now know quickly what hasn't installed properly.

Ah, but what about Gemfiles?

That's fine... but Gems sometimes have native dependencies. And sometimes the author doesn't specify the native dependency. Or the native dependency is broken. Or you are running it on JRuby and the FFI is broken. Or RubyGems reckons you've got it, but some prat went through and removed the files from the file system by hand... or a hundred other reasons. You don't have to test whether the code does anything particularly interesting (the library itself should ideally have a test suite). But if you call import and it doesn't load the module of code that you expected, you have a problem with your deployment that will affect your application...

Deployment

Phusion Passenger with Nginx. Or Apache if absolutely necessary. Nginx is nicer.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox