Skip to content

Commit

Permalink
Added a db:populate rake task. Added the gem requirements for the db:…
Browse files Browse the repository at this point in the history
…populate task to the environment. Added an example database.yml. Updated README with instructions for the db:populate task.
  • Loading branch information
Daniel Grünthal committed Sep 12, 2008
1 parent 6ddec71 commit e228f4a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ Here are the files you want to glance at:
1. All of the controllers and their respective views
2. config/initializers/searchgasm.rb

=== Generate Test Data

First you need to install the Populator and Faker gems. You can do this by either using rails' gems task

<tt>rake gems:install</tt>

or by installing the gems manually

<tt>sudo gem install populator</tt>
<tt>sudo gem install faker</tt>

After you have set up the gems and migrated your database you can generate the data:

<tt>rake db:populate</tt>

The populate task will create 8 user groups with 0 to 150 users each. Each user can have upto 7 orders. (see lib/tasks/populate.rb)


Copyright (c) 2008 [Ben Johnson](http://github.com/binarylogic) of [Binary Logic](http://www.binarylogic.com), released under the MIT license
24 changes: 24 additions & 0 deletions config/database.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
login: &login
username: root
password:

development:
adapter: mysql
database: searchgasm_development
encoding: utf8
socket: /tmp/mysql.sock
<<: *login

test:
adapter: mysql
database: searchgasm_test
encoding: utf8
socket: /tmp/mysql.sock
<<: *login

production:
adapter: mysql
database: searchgasm_production
encoding: utf8
socket: /tmp/mysql.sock
<<: *login
2 changes: 2 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
# config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
# config.gem "aws-s3", :lib => "aws/s3"
#config.gem :searchgasm
config.gem "populator", :version => ">=0.2.4"
config.gem "faker", :version => ">=0.3.1"

# Only load the plugins named here, in the order given. By default, all plugins
# in vendor/plugins are loaded in alphabetical order.
Expand Down
30 changes: 30 additions & 0 deletions lib/tasks/populate.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace :db do
desc "Erase and fill the database with test data"
task :populate => :environment do
require 'populator'
require 'faker'

[UserGroup, User, Order].each(&:delete_all)

# Generates 8 user groups with 0 to 150 users.
# Each user has 0 to 7 orders..
UserGroup.populate 8 do |user_group|
user_group.name = Populator.words(1..2).titleize
user_group.active = [true, false]
User.populate 0..150 do |user|
user.user_group_id = user_group.id
user.first_name = Faker::Name.first_name
user.last_name = Faker::Name.last_name
user.email = "#{user.first_name.downcase}.#{user.last_name.downcase}@#{Faker::Internet.domain_name}"
user.born_on = 42.years.ago..18.years.ago
user.created_at = 2.years.ago..Time.now
Order.populate 0..7 do |order|
order.user_id = user.id
order.total = [ 29.95, 4.99, 795.00, 31.70, 1.99 ]
order.description = Populator.sentences(2..6)
order.created_at = user.created_at..Time.now
end
end
end
end
end

0 comments on commit e228f4a

Please sign in to comment.