From e010f7081bad0d3e3b58e0ae5f433549dbf76588 Mon Sep 17 00:00:00 2001 From: Loos Date: Wed, 17 Sep 2014 22:18:14 -0400 Subject: [PATCH] Louis | Add dummy data generator --- db/schema.rb | 1 + db/seeds.rb | 7 ---- lib/tasks/dummydata.rake | 69 +++++++++++++++++++++++++++++++++++++++ spec/factories/clients.rb | 7 +++- 4 files changed, 76 insertions(+), 8 deletions(-) delete mode 100644 db/seeds.rb create mode 100644 lib/tasks/dummydata.rake diff --git a/db/schema.rb b/db/schema.rb index 9404615..6f9ed88 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -22,6 +22,7 @@ ActiveRecord::Schema.define(version: 20140917020156) do t.string "model" t.string "bike_type" t.string "color" + t.string "frame_size" t.string "serial_number" t.text "notes" t.text "tag_info" diff --git a/db/seeds.rb b/db/seeds.rb deleted file mode 100644 index 4edb1e8..0000000 --- a/db/seeds.rb +++ /dev/null @@ -1,7 +0,0 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). -# -# Examples: -# -# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) -# Mayor.create(name: 'Emanuel', city: cities.first) diff --git a/lib/tasks/dummydata.rake b/lib/tasks/dummydata.rake new file mode 100644 index 0000000..0b84802 --- /dev/null +++ b/lib/tasks/dummydata.rake @@ -0,0 +1,69 @@ +namespace :db do + desc "Add some random sample data" + + task dummy_data: :environment do + puts "clearing users" + User.destroy_all + puts "clearing bikes" + Bike.destroy_all + puts "clearing clients" + Client.destroy_all + + def random_client + return { + first_name: Faker::Name.first_name, + last_name: Faker::Name.last_name, + application_date: rand(5.years).ago, + gender: ["male", "female"].sample, + age: rand(45) + 15, + weight: rand(100) + 100, + helmet: [true, false].sample, + lock: [true, false].sample, + agency: Faker::Company.name, + completion_date: rand(30.days).ago + } + end + + def random_bike + return { + entry_date: rand(3.years).ago, + brand: ["Windsor", "Schwinn", "Magna", "Fuji", "Cannondale", "Bianchi", "Jamis", "Felt"].sample, + model: Faker::Hacker.verb, + bike_type: ["Cruiser", "BMX", "Fixie", "Utility", "Road", "Hybrid", "Kids"].sample, + color: Faker::Commerce.color, + serial_number: Faker::Number.number(10), + notes: Faker::Lorem.sentence, + tag_info: Faker::Lorem.sentence, + completion_date: rand(100.days).ago, + price: rand(400) + 50, + seat_tube_size: rand(45), + top_tube_size: rand(45), + log_number: Faker::Number.number(8), + purpose: ["Sale", "Freecyclery"].sample, + mechanic: Faker::Name.first_name + } + end + + puts "creating a new user" + user = User.new(email: "user@example.com", password: "password").save + + puts "creating some clients" + 10.times do + Client.new(random_client).save + end + + clients = Client.all + + puts "creating some bikes" + + 30.times do + Bike.new(random_bike).save + end + bikes = Bike.all + + puts "assigning bikes to clients" + Client.all.each_with_index do |client, index| + client.update_attribute("bike_id", Bike.all[index].id) + end + end +end diff --git a/spec/factories/clients.rb b/spec/factories/clients.rb index ea749f1..694bfeb 100644 --- a/spec/factories/clients.rb +++ b/spec/factories/clients.rb @@ -1,4 +1,9 @@ FactoryGirl.define do - factory :client do + factory :random_client do + first_name "John" + last_name "Doe" + application_date Date.new(2010, 03, 02) + gender "Male" + end end