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.
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.
