July 2011


So, say you’re in a fantasy world, and the rif-raf is beating down your doors…

Or, maybe, you’re trying to write a sane json rendering of your model, and the model has a polymorphic belongs_to.

As a bit of review, let’s start with the easy case. You have some models with a simple association.

class Child < ActiveRecord::Base
  belongs_to :parent
end

class Parent < ActiveRecord::Base
  has_many :children
end

The rabl for a child is pretty easy. In app/views/children/show.rabl you might have:

object @child
attributes :id, :name
child(:parent) { partial "children/parent" }

As it turns out, you’re actually writing an app that’s used to track orphans left behind after UFO visits, so you need to support children of aliens, too. So your models look more like this:

class Child < ActiveRecord::Base
  belongs_to :parent, :polymorphic => true
end

class HumanParent < ActiveRecord::Base
  has_many :children, :as => :parent
end

class AlienParent < ActiveRecord::Base
  has_many :children, :as => :parent
end

And of course you need to output different information for alien parents (app/views/children/alien_parent.rabl) than for human parents (app/views/children/human_parent.rabl). So your rabl in app/views/children/show.rabl will look more like this:

object @child
attributes :id, :name
code(:parent) { |child|
  partial "children/#{child.parent.class.name.underscore}",
   :object => child.parent
}
Advertisements

I’m giving squid a spin as a local cache for gems. My goal is to speed up the time git clean -whatever ; bundle install --path .bundle takes.

This solution isn’t perfect. It doesn’t make the bundle install as fast as I’d like. Also, squid needs to be restarted (sudo launchctl stop squid) when I change locations and/or sleep my laptop, because it forgets how to look up DNS (it seems to be caching the DNS server list).

I’m using homebrew and on a Mac running Snow Leopard, so this will be most helpful for anyone running the same.

Install squid

brew install squid

This installed squid 3.1.9.

Configure squid

Open /usr/local/etc/squid.conf, and uncomment the cache_dir line.

Start squid

First, you need to initialize squid. Some of this stuff may be equivalent to throwing your machine under a bus, so please be aware of what you’re copying.

chown nobody /usr/local/var/cache
chgrp everyone /usr/local/var/logs
chmod g+w /usr/local/var/logs
sudo squid -z

I wanted squid to run any time the machine is running. So, I created /Library/LaunchDaemons/squid.plist to look like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>squid</string>
    <key>OnDemand</key>
    <false/>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/sbin/squid</string>
      <string>-N</string>
      <string>-d 1</string>
      <string>-D</string>
    </array>
    <key>ServiceIPC</key>
    <false/>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

Start it by doing sudo launchctl -w /Library/LaunchDaemons/squid.plist

Configure bundler/rubygems

Open ~/.gemrc and add:

http_proxy: http://localhost:3128

My .gemrc looks like this:

---
gem: --no-ri --no-rdoc
http_proxy: http://localhost:3128/