Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fill your database, using rake db:seed
Ruby
Tree: 19cd3be9ca

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
README.rdoc
Rakefile
VERSION

README.rdoc

Fill

Rails 2.3.4 introduced db/seeds. Basically that means that the rake task “rake db:seed” will run db/seeds.rb, so you can have a centralized place to prepare your database by adding seed data.

“Fill” takes this one step further and provides methods to easily define data in db/seeds.rb. Here's how:

Fill.database do  |db|

  db.produce :projects do
    10.times { Factory(:project) }
  end

end

(I use factory_girl here, do whatever you like off course)

Now, when running “rake db:seed”, it'll delete all records in the Project model and run the contents off the produce block. It'll also measure how long it took, resulting in this output:

+-------+--------+---------+----------+
| After | Before | Models  | Time     |
+-------+--------+---------+----------+
| 10    | 0      | Project | 0.018627 |
+-------+--------+---------+----------+

You'll need to install hirb for this pretty output. Otherwise it'll print out somthing simpler. I recommend using Hirb anyway. Find it at github.com/cldwalker/hirb

Why?

At my company, we taught testers, customers and developers to use webistrano and run the db:seed“ rake task themselves. This way, they can test and experiment as much as they want, and they easily reset the database when they fucked it up.

Usage

db.produce

By specifying a block, do whatever you need to fill a model.

Example:

db.produce :users, :memberships do
  10.times do
    user = Factory(:user)
    membership = Factory(:membership, :user => user)
  end
end

Specify the models that you want. The models that you specified will be emptied, which is handy if you're building relational models, like users and their memberships.

The models are named plural.

db.fill

Provide a simple list of values.

Example

db.fill :projects, :name, "Foo", "Bar", "Baz", "etc"

For simple models with only one distinct attribute, you can just specify the model, attribute and the values.

See also the iain/root_table plugin if you have many of these.

db.invoke

Invoke a rake task.

db.invoke "some:task", :projects

I use a seperate rake task whenever I need to import files. Of course you can do that in the db.produce, but that clutters your seeds.rb.

Global options

These options work on all above mentioned methods.

:needs

Specify one or many dependencies, tables to be filled before filling the ones you're specifying now.

Example

db.produce :memberships, :needs => :users do
  User.all.each { |user| Factory(:membership, :user => user) }
end

:delete

Set to false if you don't want to delete all records before filling it.

Example:

db.invoke "import:zipcodes", :zips, :delete => false

In this example, the zipcodes import takes a long time to complete, so it doesn't insert them when the database is already filled.

:name

The output uses Rails i18n methods to determine how the output calls the models filled, but if you want to specify your own name, use this option.

Example:

db.produce :users, :name => "Accounts" do
  ....
end

Installation

Add this to config/environment.rb:

config.gem "fill", :source => "http://gemcutter.org"

Or install it by hand:

# unless you added gemcutter.org to your sources:
gem install gemcutter
gem tumble
gem install fill

Copyright 2009, Iain Hecker - iain.nl - Released under the MIT License

Something went wrong with that request. Please try again.