Be sure where your methods are chained to…

I’ve just figured out a quite obscure bug in our app. It all started like this:


record.freeze.things # record is an ActiveRecord, and "things" is an association on that record.
TypeError: can't modify frozen object
	from (irb):2:in `instance_variable_set'
	from (irb):2

The code above shouldn’t crash, because ActiveRecord hast its own #freeze method, which will still allow access to the associations. But our record behaved as if Object#freeze had been called on it. What happend?

Continue reading “Be sure where your methods are chained to…”

Factory Girls secret savings

We like to use FactoryGirl for our test setup. Unlike Rails’ fixtures, it will give you an individual setup for each tests, and thus preventing that tests have side effects on other tests. However, saving objects on each tests comes with a performance penalty.

For your specs, however, you’ll only need to call Factory.build in many cases, creating an new object without saving it to the database. Only that if you have a factory like the following, the associated objects will always be saved:


Factory.define :project do |f|
  f.association :carrier, :factory => :organisation
end

That is, even if you call Factory.build(:project), Factory Girl will internally call Factory.create(:organisation), saving the associated object. If you don’t want that behaviour in your tests, build your objects “manually” in the tests.

Rails forms and params

Another not to self: Rails has the cool feature that you it will map all the values from your submitted forms into the params hash. And, if you create input fields that are named “something[bla]“, it will automatically create a nested hash so that you will be able to access params[:thingyform][:something].

But what if you just have a list of things that you need to submit? In this case, just name multiple form fields something[], and the params will contain an array.

git submodules

Today’s lesson is mainly for the people who want to use the Talia source code, but it could be useful for many git users, so I’m putting it here. In our code we use git submodules a lot; they are a bit like the good old svn externals but have different quirks that are not that easy to understand. And of course neither the official documentation nor the tutorial explain it in more detail. (I’ll assume that you’re already familiar with the general concept):

Continue reading “git submodules”

Building Mac Packages

Since we’ll need to deploy our application soon in a Mac environment, I’m in the process of building an installer package that our partner’s local staff will be able to handle.

Usually the preferred installation (and deinstallation) method on the Mac is drag-and-drop, which is really the best method I’ve ever encountered on any operating system. But for packages that need to be integrated into the system, such as ours, Apple has a built-in-installer package mechanism.

The installer itself is not too bad, although it doesn’t even consider that you might want to uninstall anything. But the worst part is definitively the “PackageMaker” application that comes with Apples developers’ tools.

I’ve seldom seen such a sorry excuse for a tool – it will actually loose settings if you uncheck a certain box in the UI, and more fun like that.

Fortunately, I found a nifty tool called Iceberg, which let’s you build Apple-compatible installer packages. Though it’s still a bit rough in some places, it’s already richer in features and works much better than the default Apple solution. Check it out if you need to create any Mac installer package.

A little routing trickery

Documentation in Rails seems notoriously bad – the documentation appears to be painfully incomplete. Anyway, all the cool kiddies will tell you how to do resource routing; but I wanted a bit more. In particular, i wanted to have URLs that work like http://xxx/things//. Where is the id of the thing you’re watching, and is an additonal identifier that is passed to the controller. For example, you could have a rout that let’s you do http://xxx/cars/bug/green, to show you the green version of the bug.

After a little trial and error, I found that this will be possible using a route like this:


map.resources :cars do |cars|
  cars.connect ':colour', :controller => 'cars',
    :action => 'car_with_colour'
end

Which defines this in a much nicer fashion than matching the route “manually”.

Blog at WordPress.com.

Up ↑

%d bloggers like this: