[ANN] Seed -- a seed data/fuzzing plugin

Seed is a plugin to help with generating and/or inserting seed data
into your database. Seed will
generate data for you or load it from fixtures or arbitrary Yaml, CSV,
or (eventually) XML
files. The basic workflow goes like this:

  1. Create seed files. These live in db/seeds/ and are named for the
    tables of your models,
    like +Person+ (model), +people+ (table) would yield +people_seed.rb+.

  2. Create a class named for the file (+people_seed.rb+ would have
    +PeopleSeed+) that inherits from

  3. Fill in your directives for attributes or file loads, or you can
    leave it empty and Seed will
    enumerate your columns and generate data for you.

  4. Run rake db:seed to seed your database.

You can also generate static versions of the seeds for your current
models using the db:seeds:generate
Rake task.

So, let’s say you have a model +User+, with attributes +username+
(string), +password+ (string),
+email_address+ (string), favorite_quote (string), and administrator
(boolean). To get a basic seed
file, you need to execute rake db:seeds:generate. This task
will yield a file like this:

class UsersSeed < Seed::Planter
  attribute :username, :string
  attribute :password, :string
  attribute :email_address, :string
  attribute :favorite_quote, :string
  attribute :adminstrator, :boolean

Now, since we have a few special fields, we can use Seed’s special
types for +username+, +password+, and +email_address+.

class UsersSeed < Seed::Planter
  attribute :username, :username
  attribute :password, :password
  attribute :email_address, :email
  attribute :favorite_quote, :string
  attribute :adminstrator, :boolean

This will generate proper data (for example, “jmcanally” rather than
“Lorem ispum dolormet” for username).

Run rake db:seed and voila! A seeded database!

You can also skip specifying a type and provide the data for the field
in a +Proc+. For example, if you wanted to
have some specialized logic for setting the value of a field:

class JobsSeed < Seed::Planter
  attribute :description, :string
  attribute :script, Proc { ['upload', 'update',

‘find_files’].sort { rand(3) - 1 }[0] }

You can also call methods from the seed class in the +Proc+ if you’d
like (direct support for method class on the
seed class will be added soon).


# db/seeds/products_seed.rb
class ProductsSeed < Seed::Planter
  attribute :name, :string

  # Address attribute types are special and generate
  # proper data.
  attribute :address, :address

  attribute :email, proc { "[email protected]" }

# db/seeds/people_seed.rb
class PeopleSeed < Seed::Planter
  # Tells Seed that you want to load seed data from
  # the file db/seeds/people.yml
  loads_from "people.yml"

# db/seeds/things_seed.rb
class ThingsSeed < Seed::Planter
  # Tells Seed that you want to load seed data from
  # the fixtures file (test/fixtures/things.yml)
  loads_from :fixtures

This point of the plugin is to let you easily seed data/have seed data
for you to get your app up and going quickly. Nothing is more
irritating than having
to spend time in script/console or YourSQL to create some base data to
work from.

It’s on GitHub at http://www.github.com/jeremymcanally/seed or you can
clone from
here: git://github.com/jeremymcanally/seed.git .

Pull requests, suggestions, and bug reports are appreciated.



Read my books:
Ruby in Practice (http://manning.com/mcanally/)
My free Ruby e-book (http://humblelittlerubybook.com/)

Or, my blogs:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs