Thursday, December 16, 2010

Self-Referential Rails Association

Or, doing terrible things to my relational db with a fork, part one.

I often want trees of objects. To do this with ActiveRecord, establish a self-referential association. Let's get started with a simple model and a join table:

rails generate scaffold Item name:string description:text
rails generate model ItemItem parent_id:integer child_id:integer rank:integer
rake db:migrate

Now lets wire things up:

class ItemItem < ActiveRecord::Base
belongs_to :parent, :class_name => "Item"
belongs_to :child, :class_name => "Item"
end
class Item < ActiveRecord::Base
has_many :below, :class_name => "ItemItem", :foreign_key => "parent_id"
has_many :children, :through => :below
has_one :above, :class_name => "ItemItem", :foreign_key => "child_id"
has_one :parent, :through => :above
end

Set aside a few minutes to work through this with the Rails guide. You'll find it's all standard association stuff, just without the default naming by convention. In the meantime, enjoy:

irb(main):001:0> i = Item.new :name => 'Buy Presents'
=> #<item>
irb(main):002:0> j = Item.new :name => 'Big Newegg Order'
=> #<item>
irb(main):003:0> i.children << j
=> [#<item>]

Note that hideous db interactions (i.save and j.reload) must be engaged in before j.parent works :(.

Wednesday, December 15, 2010

Adding a per-device layout in Sitecore

Sitecore lets you render an item differently for different devices (i.e. rss, mobile, etc.). Once you've developed the layout, here are the steps for moving it to acceptance or production:
  1. Create a new layout of the same name via the Content Editor.
  2. Overwrite the aspx file generated in step 1 with your layout. Copy over aspx.cs and designer files too.
  3. Copy other files (fresh dll's, css's, images, and so on).
  4. Create the device via the Content Editor. Usually specify a query string (device=mobile). Save!!
  5. Associate item template, device, and layout via Content Editor: templates -> UserDefined -> Item Template -> Std Values -> Presentation ->Details -> Edit Device -> select layout. Save!!
  6. Do a sitewide smart publish.
  7. Enjoy! (after waiting out the cache, that is)
Developing the layout in the first place is quite similar ...

Thursday, December 2, 2010

KVM Streamliner

Machine virtualization can be done quite simply on Linux.

First check the hardware with kvm-ok:

$ kvm-ok
INFO: Your CPU supports KVM extensions
INFO: /dev/kvm exists
KVM acceleration can be used

Next, create a couple disk images with qemu-img:

$ qemu-img create -f raw d0.raw 8G
Formatting 'd0.raw', fmt=raw size=8589934592
$ qemu-img create -f raw swap.raw 2G
Formatting 'swap.raw', fmt=raw size=2147483648

Setup a 'usermode' network interface with tunctl:

$ sudo /usr/sbin/tunctl -t tap7
Set 'tap7' persistent and owned by uid 0

Enjoy!:

$ sudo kvm -M pc -m 1024 -smp 1 -name streamliner -monitor pty \
-cdrom ubuntu-10.10-server-amd64.iso \
-drive file=d0.raw,if=virtio,boot=on \
-drive file=swap.raw,if=virtio \
-net nic,macaddr=52:54:00:d0:bb:27,model=virtio \
-net tap,script=no,ifname=tap7 \
-vnc 127.0.0.1:7
char device redirected to /dev/pts/3

Simple, too simple, yes? How about cracking open a vnc client and installing from the boot iso? (f4 -> virtual):

$ xvncviewer 127.0.0.1:7

Also, one can poke the soft underbelly via serial 'port':

$ sudo minicom -o -p pts/3

Well, that's all for now folks. Next time I'll write about a simple network setup.