factories-and-workers updated as gem plugin

Factories-and-workers is now updated to not only be a plugin, but also a gem. Thanks to Jonathan Barket and Nick Hoffman for their help!

It is also now automatically loaded for test and development environments (as a plugin, or do it manually as below with the unless statement). I find this really comes in handy for using it in the rails console to populate your database.

Get It

Add it as a gem dependency to your rails environment.rb:


  config.gem 'dfl-factories-and-workers', :lib => 'factories-and-workers', :source => 'http://gems.github.com' unless RAILS_ENV=='production'

Or install it as a gem manually:

  gem sources -a http://gems.github.com
  sudo gem install dfl-factories-and-workers

Or grab the source:

  git clone git://github.com/dfl/factories-and-workers.git

rake task to generate factory templates

I’ve been working more on factories-and-workers

Yesterday I came up with the idea to make a rake task to ease the initial entry into factory-land. It grabs info from the schema, via the model.columns hash, and prints out a factory template, to be pasted and edited in your factories.rb file.

Check it out:



dfl% rake factory:generate MODEL=task_estimate

factory :task_estimate, {
  :task => :belongs_to_model,
  :date => lambda{ Time.zone.today - 7 },
  :hours => 1.23,
}

If called without the MODEL argument, it will print factory templates for all models.

friendly_fixtures plugin on github

I previously posted about my friendly_fixtures plugin

It’s now available at github: http://github.com/dfl/friendly_fixtures/tree/master

factories-and-workers at github

Factories and Workers is a Rails plugin originally written by Nathan Herald @ myobie.com after being inspired by Dan Manges’ blog post on factories. It uses some slick metaprogramming to generate factory methods for your ActiveRecord models. Over the past few months I’ve refactored the code, added a bunch of new features, and most importantly wrote some tests!

See it in action:

>> factory :monkey, { :name => "George" }
=> #<FactoryBuilder:0x1a4f2a8>

>> valid_monkey_attributes
=> {:name=>"George"}

>> build_monkey
=> #<Monkey id: nil, name: "George">

>> build_monkey( :name => "Bob" )
=> #<Monkey id: nil, name: "Bob">

>> Monkey.count
=> 0

>> create_monkey
=> #<Monkey id: 1, name: "George">

>> Monkey.count
=> 1

There’s much more info in the README.

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

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