HOWTO: Fix “rake/rdoctask is deprecated. use rdoc/task instead”

Seeing this?

rake/rdoctask is deprecated. Use rdoc/task instead (in RDoc 2.4.2+)

Edit your Rakefile and change these lines:

require 'rake/rdoctask'
Rake::RDocTask.new(:rdoc) do |rdoc|
 ...

to look like this:

require 'rdoc/task'
RDoc::Task.new do |rdoc|
  ...

You may need to add gem 'rdoc' to your Gemfile, too. While you’re at it, you might want to add rdoc/ to your .gitignore, too

HOWTO: Wrangle the pg (postgresql) gem with macports and rvm

Seeing this?

Installing pg (0.11.0) with native extensions /Users/mrm/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:533:in `rescue in block in build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

        /Users/mrm/.rvm/rubies/ruby-1.9.2-p180/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***

Wondering how or where to get this mythical pg_config? It’s part of the PostgreSQL package. If you’re running MacPorts, it’s easy:

sudo port install postgresql90

Then, following the instructions on the RVM website, run

gem install pg -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config

Creating acts_as_… gems for Rails 3.1.x

There are a lot of posts on how to build rubygems. With Rails 3.1, though, they’re all old and busted.

The new hotness is built right into rails now. Just incant

rails plugin new APP_PATH

You’ll get:

  • a .gemspec and Gemfile to get started
  • a dummy rails app to integration test your new gem against
  • a (deprecated) RDoc rake task

When you run rake, you’ll see “rake/rdoctask is deprecated. Use rdoc/task instead (in RDoc 2.4.2+)”. To remedy, follow these steps.

The Edge edition of the Ruby on Rails Guides has more information.

Hierarchical Tagging with Rails 3 and Closure Trees

In rebuilding PhotoStructure on Rails, I was surprised that one of the most popular gem for trees used a nested set model. Nested sets are performant for reads, but for adding and deleting nodes, it’s extremely expensive — on average, half of the rows for your model acting as a nested set have to be updated for every add or delete. That’s crazy-talk! It requires a table-level lock for something that, at least for PhotoStructure, is a very common task. To top it off, the gem doesn’t even do the lock!

I then found the ancestory gem, which works by materializing the ancestral path as a string and storing that as a column. Bill Karwin’s excellent Models for hierarchical data presentation describes this as the Path Enumeration algorithm, which doesn’t have referential integrity, and relies on performant LIKE selects.

As the tag hierarchies in PhotoStructure are never moved, closure trees should prove to be ideal. It’s an excellent excuse to learn how to build and publish a rails plugin gem, so I’ve built a new gem to support closure trees: github.com/mceachen/closure_tree.

Update, May 24

1.0.0.beta1 released. All public methods have test coverage.

Update, May 25

1.0.0.beta2 and 1.0.0.beta3 were released, which added find_or_create class and instance methods, ancestry_path, and root instance methods. Documentation is on the README and in the rdocs.

Update, May 29

1.0.0.beta5 is released, which cleaned up the ancestor and descendant relationships to use has_and_belongs_to_many, and adds leaves class and instance methods.

Update, Oct 26

2.0.0.beta1 is released, which added the :dependant option, switched from an embedded dummy rails app for testing to rspec, and much better test coverage (including tests and fixes for a couple reported issues) under sqlite, MySQL, and PostgreSQL.

Update, Nov 27

3.0.0 is released, which supports polymorphic trees.

Switching between Rails 2 and Rails 3 on Mac OS X or Ubuntu with RVM

I’m migrating AdGrok to Rails 3 this weekend–what with the new ActiveRecord query functionality, and MongoMapper, and all our gems migrating to Rails 3, it seems like it’s time.

The Railscast (and ASCIIcast) is great, but a couple steps are now outdated. There also aren’t any instructions on how to switch between rails 2 and rails 3 gracefully.

Continue reading

HOWTO: Fix Ruby on Rails 2.3.x textarea Value Truncation

Turns out that Rails versions 2.3.6 through 2.3.8 have a pretty horrid parameter-parsing bug, and I’m surprised there hasn’t been more hoopla about it.

If you have a textarea form that someone types a quote character into, the value you get back from params[:key] is truncated at the point of the quote.

Monkey patching to the rescue!

I decided to add a new raw_params method to ActiveRecord::Request, rather than fixing the bug and dealing with more potential side-effects, assuming this mess will be fixed in Rails 3. If it isn’t, we should fix ActionController::Base.param_parsers[:url_encoded_form] to return the properly-parsed parameters from the raw_post.

In your rails application, create config/initializers/request.rb, with the following contents:

Continue reading

Installing Phusion Passenger on Ruby 1.9.1, Nginx, & Ubuntu 10.04

I wrote this a while back, before I knew about RVM. The Ruby Version Manager has a bunch of features that makes it better than macports for ruby and gem installation. Read about it here.

Getting ruby 1.9.1 and nginx and passenger and ubuntu to all play nicely is fairly straightforward, but it’s not just “apt-get” and “gem install” lovin’.

Making ruby 1.9.1 the default ruby is OK. Follow these steps:

Continue reading