creating YAML fixtures from existing data

I just came across this cool tip from Tom Preston-Werner of rubyisawesome.com. Basically, if you end a mysql commandline query with \G instead of a semicolon, you’ll get a nicely formatted query that is suitable for pasting into a YAML file.

But what if you are not using mysql? We have a project with postgres. There’s probably a psql command to do something similar, but there is also a database agnostic way: Just use the rails console! :)


>> puts User.find(:all).to_yaml
---
- !ruby/object:User 
  attributes: 
    status: unverified
    salt: L52b2pxGCL
    can_invite: "0" 
    hashed_password: L50/yIQjPCBiU
    is_admin: "0" 
    id: "478674008" 
    first_name: Mickey
    last_name: Mouse
    watchlist_by_email: "0" 
    created_at: 2008-01-30 15:34:26
    email: mickey@mouse.com

		
		
	

friendly_fixtures plugin for Rails

In a previous post, I posted a monkey patch to enhance the behavior of fixtures. I converted it into a plugin format for convenience and testing purposes. I am still aiming to submit it as a patch to Rails 2.0. I’d appreciate any feedback!

get it here:

svn export --username=public svn://internautdesign.com/public/plugins/friendly_fixtures vendor/plugins/friendly_fixtures

UPDATE: now available at github: http://github.com/dfl/friendly_fixtures/tree/master

from the README:

FriendlyFixtures

This plugin is a simple extension to add some cool features to the fixtures macro in Test::Unit. It is intended for Rails 2.0, but works with 1.2.3 as well.

It enables you to:
  • load dependent models, which are found by object introspection on a model’s ActiveRecord associations.
  • assert that all the loaded fixtures are valid. This can be very helpful in finding bugs.

Example Usage:


class SomeTest < Test::Unit::TestCase
  fixtures :user, :dependencies => true, :validate => true
end

this replaces my earlier post about testing for valid fixtures. Rails 2.0 patch coming soon…?

UPDATE: This monkey patch has been replaced by a plugin (with tests!): friendly_fixtures.

Rathole, a fixtures plugin so sweet it’s been incorporated into edge rails, is great—but unfortunately it doesn’t support polymorphic associations. That is, until now! :)

Get the patch: here.

UPDATE: I learned that the author, John Burnette, has renamed the project to foxy fixtures. He also massaged my patch into a contribution for rails 2.0: Ticket 10183.

There is some discussion that this patch is not necessary.

actionmailer ActionView ajax alphadecimal audio autotest BDD blocks capistrano ssh ruby controller css dashboard widget delegate dog puppy naming name DRM email obfuscation exceptions factories-and-workers factory pattern filemerge find and replace finder fink fixtures fun gem git google maps helper helpers imagemagick Intertrust javascript logo math meetup model openssl OS X patch Pioneer Electronics plugin polymorphism prototype.js rails railsconf rake rmagick RSA encryption ruby script shoulda subversion SyncTV TDD testing textmate tricks unique hashes unix shell validation view yaml zebra stripes