Some of my camping trips take advantage of ActiveRecord's acts_as_tree declarative, which as of the newest version of ActiveRecord> (for Rails 2 and above) is no longer included. What's a microframework junky supposed to do? Camping without trees is like camping in Indiana! Bleak.
Well, there are a couple of solutions.
- Use an old version of ActiveRecord. This is probably the easiest, taking advantage of Gem's version management. But, I rather like some of the new ActiveRecord features—DRY migrations especially make my camping load even easier to shoulder. So, I went with
- Take the pluginified acts_as_tree and use it in your camping apps.
It turns out this is very easy to do. Here's how!
- Make a new clean Rails 2 app:
rails myapp - Install the acts_as_tree plugin:
cd myapp; script/plugin install acts_as_tree. The acts_as_tree code we want now lives in vendor/plugins. - Copy the acts_as_tree code out of the rails directory:
cp vendor/plugins/acts_as_tree/lib/active_record/acts/tree.rb ../acts_as_tree.rb - The init.rb file for the acts_as_tree plugin includes some code we want, including the Acts::Tree behavior in ActiveRecord::Base. It's a one-liner, and we want to append it to acts_as_tree.rb.
cat vendor/plugins/acts_as_tree/init.rb >> ../acts_as_tree.rb - We're all done with our dummy rails ap.
cd ..; rm -rf myapp
Alright, now you have an acts_as_tree.rb file, ready to be included in your camping apps. I keep all mine in their own camping directory. I added a 'lib' directory here; then, you just add 'lib/acts_as_tree.rb' to the includes of your camping apps:
%w{rubygems camping redcloth lib/acts_as_tree}.each {|lib| require lib}
That's all! Here's my acts_as_tree.rb file, if you don't want to go through all the foofrah. Happy treeful camping!