From 5a2545f4bfeebf8c3056d295f3d2e05d2048a831 Mon Sep 17 00:00:00 2001 From: Michael Yockey Date: Sun, 22 Oct 2017 22:10:46 -0400 Subject: [PATCH 1/4] Improving seed data --- db/seed/items.yml | 119 ++++++++++++++++++++++++++++++++++++++++++ db/seed/users.yml | 18 +++++++ db/seed/wishlists.yml | 6 +++ db/seeds.rb | 65 +++++++++++++---------- 4 files changed, 180 insertions(+), 28 deletions(-) create mode 100644 db/seed/items.yml create mode 100644 db/seed/users.yml create mode 100644 db/seed/wishlists.yml diff --git a/db/seed/items.yml b/db/seed/items.yml new file mode 100644 index 0000000..25f7fb1 --- /dev/null +++ b/db/seed/items.yml @@ -0,0 +1,119 @@ +- name: Louis the Corgi + amazon_url: https://www.amazon.com/Douglas-1713-Toys-Louie-Corgi/dp/B00TFT77ZS + price_cents: 1345 + image_url: https://images-na.ssl-images-amazon.com/images/I/71u1YGcc-FL._SL1500_.jpg + asin: B00TFT77ZS + +- name: BRIO Monorail Airport Set + amazon_url: https://www.amazon.com/dp/B00B0IE3RW + price_cents: 10890 + image_url: https://images-na.ssl-images-amazon.com/images/I/51vWDVHdiVL._SL1000_.jpg + asin: B00B0IE3RW + +- name: Tommee Tippee Closer To Nature Newborn Starter Gift Set, Blue + amazon_url: https://www.amazon.com/dp/B01GG2C9Z4 + price_cents: 3579 + image_url: https://images-na.ssl-images-amazon.com/images/I/81nb4LzA-zL._SL1500_.jpg + asin: B01GG2C9Z4 + +- name: Burt's Bees Baby Bee 100% Natural Diaper Rash Ointment, 3 Ounces + amazon_url: https://www.amazon.com/dp/B00YRKV1U2 + price_cents: 778 + image_url: https://images-na.ssl-images-amazon.com/images/I/61Z-sDUNwPL._SL1300_.jpg + asin: B00YRKV1U2 + +- name: Amazon Elements Baby Wipes, Unscented, 720 Count, Resealable Packs with Tub + amazon_url: https://www.amazon.com/dp/B00M4M2ZSC + price_cents: 1614 + image_url: https://images-na.ssl-images-amazon.com/images/I/814UrY9LP9L._SL1500_.jpg + asin: B00M4M2ZSC + +- name: Big Bug + amazon_url: https://www.amazon.com/dp/1442498978 + price_cents: 989 + image_url: https://images-na.ssl-images-amazon.com/images/I/51hu-vdgcTL._SY498_BO1,204,203,200_.jpg + asin: 1442498978 + +- name: Summer Infant Lil' Loo Potty, White and Teal + amazon_url: https://www.amazon.com/dp/B00A0ID90A + price_cents: 923 + image_url: https://images-na.ssl-images-amazon.com/images/I/81usMG68MeL._SX522_.jpg + asin: B00A0ID90A + +- name: Yellow Ball + amazon_url: https://www.amazon.com/dp/1930900791 + price_cents: 1893 + image_url: https://images-na.ssl-images-amazon.com/images/I/51ueH65wM5L.jpg + asin: 1930900791 + +- name: Summer Infant 3Dlite Convenience Stroller, Citrus + amazon_url: https://www.amazon.com/dp/B00O20NX30 + price_cents: 6300 + image_url: https://images-na.ssl-images-amazon.com/images/I/91m%2BP9gryyL._SX522_.jpg + asin: B00O20NX30 + +- name: Pull-Ups Learning Designs Training Pants for Girls, 2T-3T, 74 Count + amazon_url: https://www.amazon.com/dp/B01B649P3G + price_cents: 2374 + image_url: https://images-na.ssl-images-amazon.com/images/I/91EGkpUWd3L._SX522_.jpg + asin: B01B649P3G + +- name: Philips AVENT BPA Free Night Time Pacifier, 6-18 Months, 2 Count Pack + amazon_url: https://www.amazon.com/dp/B001L2SA3A + price_cents: 398 + image_url: https://images-na.ssl-images-amazon.com/images/I/915lXaqQX9L._SX522_.jpg + asin: B001L2SA3A + +- name: Philips Avent BPA Free Contemporary Freeflow Pacifier, 0-6 Months, 2 Count + amazon_url: https://www.amazon.com/dp/B001L2SA6M + price_cents: 554 + image_url: https://images-na.ssl-images-amazon.com/images/I/71OhSyR0qAL._SX522_.jpg + asin: B001L2SA6M + +- name: LeapFrog Shapes And Sharing Picnic Basket + amazon_url: https://www.amazon.com/LeapFrog-Shapes-Sharing-Picnic-Basket/dp/B004560BPY + price_cents: 1520 + image_url: https://images-na.ssl-images-amazon.com/images/I/81UGxtwm1YL._SL1500_.jpg + asin: B004560BPY + +- name: Graco SnugRide Click Connect 35 Infant Car Seat, Gotham + amazon_url: https://www.amazon.com/dp/B00H8MQ4US + price_cents: 12869 + image_url: https://images-na.ssl-images-amazon.com/images/I/61klvwBSdAL._SX522_.jpg + asin: B00H8MQ4US + +- name: Graco Snugrider Elite Stroller and Car Seat Carrier, Black + amazon_url: https://www.amazon.com/dp/B00UVW3WHK + price_cents: 5544 + image_url: https://images-na.ssl-images-amazon.com/images/I/518QkoPoGyL._SX522_.jpg + asin: B00UVW3WHK + +- name: Pull-Ups Hug Learning, 4t-5t Boy, 18 Count + amazon_url: https://www.amazon.com/dp/B01B649SZ6 + price_cents: 1506 + image_url: https://images-na.ssl-images-amazon.com/images/I/91ylbSUpy3L._SX522_.jpg + asin: B01B649SZ6 + +- name: Baby Teething Toys - Tinabless Teething Keys Set Soft Silicone BPA-Free Baby Fruit Teethers Toys with Pacifier Clip/Holder for Infant and Toddler (4 pack) + amazon_url: https://www.amazon.com/dp/B01MSRC4WJ + price_cents: 1423 + image_url: https://images-na.ssl-images-amazon.com/images/I/61dEUOKA-dL._SL1100_.jpg + asin: B01MSRC4WJ + +- name: Fisher-Price My First Thomas & Friends Rail Rollers Spiral Station + amazon_url: https://www.amazon.com/Fisher-Price-Thomas-Friends-Rollers-Station/dp/B00VJKT8CG + price_cents: 2895 + image_url: https://images-na.ssl-images-amazon.com/images/I/815ITgK8dCL._SL1500_.jpg + asin: B00VJKT8CG + +- name: Cra-Z-Art Peppa Pig Travel Magna Doodle - Magnetic Screen Drawing Toy + amazon_url: https://www.amazon.com/Cra-Z-Art-Peppa-Travel-Magna-Doodle/dp/B019DQFZLU + price_cents: 899 + image_url: https://images-na.ssl-images-amazon.com/images/I/81geXlY7xVL._SL1500_.jpg + asin: B019DQFZLU + +- name: My Little Pony - Rainbow Dash 8" + amazon_url: https://www.amazon.com/My-Little-Pony-Rainbow-Dash/dp/B00EIFS8AC + price_cents: 677 + image_url: https://images-na.ssl-images-amazon.com/images/I/51XirJp%2BfmL.jpg + asin: B00EIFS8AC diff --git a/db/seed/users.yml b/db/seed/users.yml new file mode 100644 index 0000000..7d52970 --- /dev/null +++ b/db/seed/users.yml @@ -0,0 +1,18 @@ + +- name: Gus Grissom + email: grissom@example.com + +- name: Ed White + name: white@example.com + +- name: Roger Chafee + email: chafee@example.com + +- name: Neil Armstrong + email: armstrong@example.com + +- name: Michael Collins + email: collins@example.com + +- name: Buzz Aldrin + email: aldrin@example.com diff --git a/db/seed/wishlists.yml b/db/seed/wishlists.yml new file mode 100644 index 0000000..b755b69 --- /dev/null +++ b/db/seed/wishlists.yml @@ -0,0 +1,6 @@ +- name: DC General +- name: Turning Point Center for Women & Children +- name: DASH Cornerstone Transitional Living Program +- name: New Beginning Temporary Shelter +- name: Community of Hope – Hope Apartments +- name: Quality Inn Overflow Shelter Program diff --git a/db/seeds.rb b/db/seeds.rb index 240f36c..7e515af 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,34 +1,43 @@ -# 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 rails db:seed command (or created alongside the database with db:setup). -# -# Examples: -# -# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) -# Character.create(name: 'Luke', movie: movies.first) +# Read the seed data into memory. Do this before deleting data to be sure there isn't a problem +seed_data = Dir.glob(Rails.root.join('db', 'seed', '**', '*.yml')).reduce({}) { |collection, file| + collection[File.basename(file, '.yml')] = YAML::load_file(Rails.root.join('db', 'seed', file)) + collection +} -admin = User.create!( - name: "Rebecca Staples", - email: "rebecca@playtime.org", - admin: true -) +# Remove all seeded data. +Wishlist.destroy_all +User.destroy_all +Item.destroy_all +Pledge.destroy_all +WishlistItem.destroy_all +SiteManager.destroy_all -user = User.create!(name: "Micah Bales", email: "micahbales@gmail.com") +wishlists = Wishlist.create!(seed_data['wishlists']) +users = User.create(seed_data['users']) +items = Item.create(seed_data['items']) -wishlist = Wishlist.create!(name: "DC General") +# Associate users with wishlists in order +site_managers = (0..5).collect { |index| + SiteManager.create!(user: users[index], wishlist: wishlists[index]) +} -SiteManager.create!(user: user, wishlist: wishlist) +# Randomly assign several items to a wishlist +wishlist_items = wishlists.map { |wishlist| + 5.times.map { + WishlistItem.create!( + quantity: 1, + wishlist: wishlist, + item: items.sample + ) + } +}.flatten -item = Item.create!( - amazon_url: "https://www.amazon.com/Douglas-1713-Toys-Louie-Corgi/dp/B00TFT77ZS/ref=sr_1_1?ie=UTF8&qid=1495377177&sr=8-1&keywords=corgi+toy", - price_cents: 1320, - asin: "B00TFT77ZS", - image_url: "https://images-na.ssl-images-amazon.com/images/I/71u1YGcc-FL._SL1500_.jpg", - name: "Louis the Corgi" -) +# Owned pledges +Pledge.create!(user: users.first, wishlist_item: wishlist_items.first) +Pledge.create!(user: users.second, wishlist_item: wishlist_items.second) +Pledge.create!(user: users.last, wishlist_item: wishlist_items.last) -WishlistItem.create!( - quantity: 1, - wishlist: wishlist, - item: item, - staff_message: "Item for the 3-10 age group. Our shelter cannot support pets, we find these stuffed doggos to be therapeutic for the children." -) +# Anonymous pledges +Pledge.create!(wishlist_item: wishlist_items[10]) +Pledge.create!(wishlist_item: wishlist_items[11]) +Pledge.create!(wishlist_item: wishlist_items[12]) From e813fcf3e564a0cda30cfd6608940fe6da90de59 Mon Sep 17 00:00:00 2001 From: Michael Yockey Date: Mon, 23 Oct 2017 20:31:14 -0400 Subject: [PATCH 2/4] Seed admin user --- db/seeds.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/db/seeds.rb b/db/seeds.rb index 7e515af..a024ecc 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -12,6 +12,13 @@ WishlistItem.destroy_all SiteManager.destroy_all +# First create the admin +User.create( + name: 'Sally Ride', + email: 'ride@example.com', + admin: true +) + wishlists = Wishlist.create!(seed_data['wishlists']) users = User.create(seed_data['users']) items = Item.create(seed_data['items']) From c82389ba2375705dbcf96144e1078733c969f697 Mon Sep 17 00:00:00 2001 From: Michael Yockey Date: Mon, 23 Oct 2017 20:33:23 -0400 Subject: [PATCH 3/4] Put deletes in dependent order In Rails, `delete_all` is faster than `destroy_all`. To keep things fast, this reorders the destroy operations and turns them into deletes. --- db/seeds.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index a024ecc..33ca2e1 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,12 +5,12 @@ } # Remove all seeded data. -Wishlist.destroy_all -User.destroy_all -Item.destroy_all -Pledge.destroy_all -WishlistItem.destroy_all -SiteManager.destroy_all +Pledge.delete_all +WishlistItem.delete_all +SiteManager.delete_all +Wishlist.delete_all +User.delete_all +Item.delete_all # First create the admin User.create( From 17f5820ff349b5e697bbf36ba27c965936ac77ce Mon Sep 17 00:00:00 2001 From: Michael Yockey Date: Mon, 23 Oct 2017 20:36:47 -0400 Subject: [PATCH 4/4] Assign each item to random wishlists Inverts the logic from wishlist -> items to items -> wishlists. This ensures that all items are assigned and that no wishlist can have the same item twice. --- db/seeds.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 33ca2e1..9b8cfc3 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -29,12 +29,13 @@ } # Randomly assign several items to a wishlist -wishlist_items = wishlists.map { |wishlist| - 5.times.map { +wishlist_items = items.map { |item| + wishlist_sample = wishlists.shuffle.take(Random.rand(3)) + wishlist_sample.map { |wishlist| WishlistItem.create!( quantity: 1, wishlist: wishlist, - item: items.sample + item: item ) } }.flatten