From b9229f91664a69b73e01494791d0691f9b160ed8 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 22 Jun 2012 15:15:20 +1000 Subject: [PATCH] DB seeds create some default taxonomies, suppliers, distributors and products, and are now idempotent --- db/seeds.rb | 86 +++++++++++++++++++++++++++++++++++---- spec/support/factories.rb | 8 ++-- 2 files changed, 81 insertions(+), 13 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 91c2fdae80..f21d529d8c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,13 +1,81 @@ # 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) + +require File.expand_path('../../spec/support/factories', __FILE__) -Spree::Core::Engine.load_seed if defined?(Spree::Core) -Spree::Auth::Engine.load_seed if defined?(Spree::Auth) -default_path = File.join(File.dirname(__FILE__), 'default') -ActiveRecord::Fixtures.create_fixtures(default_path, ['spree/states']) \ No newline at end of file +# -- Spree +unless Spree::Country.find_by_name 'Australia' + puts "[db:seed] Seeding Spree" + Spree::Core::Engine.load_seed if defined?(Spree::Core) + Spree::Auth::Engine.load_seed if defined?(Spree::Auth) +end + + +# -- States +# States are created from factories instead of fixtures because models loaded from fixtures +# are not available to the app in the remainder of the seeding process (probably because of +# activerecord caching). +unless Spree::State.find_by_name 'Victoria' + puts "[db:seed] Seeding states" + + [ + ['ACT', 'ACT'], + ['New South Wales', 'NSW'], + ['Northern Territory', 'NT'], + ['Queensland', 'QLD'], + ['South Australia', 'SA'], + ['Tasmania', 'Tas'], + ['Victoria', 'Vic'], + ['Western Australia', 'WA'] + ].each do |state| + + # country_id 12 == Australia. See db/default/spree/countries.yaml + FactoryGirl.create(:state, :name => state[0], :abbr => state[1], :country_id => 12) + end +end + + +# -- Taxonomies +unless Spree::Taxonomy.find_by_name 'Products' + puts "[db:seed] Seeding taxonomies" + taxonomy = Spree::Taxonomy.find_by_name('Products') || FactoryGirl.create(:taxonomy, :name => 'Products') + taxonomy_root = taxonomy.root + + ['Vegetables', 'Fruit', 'Oils', 'Preserves and Sauces', 'Dairy', 'Meat and Fish'].each do |taxon_name| + FactoryGirl.create(:taxon, :name => taxon_name, :parent_id => taxonomy_root.id) + end +end + + +# -- Suppliers and distributors +unless Spree::Supplier.count > 0 + puts "[db:seed] Seeding suppliers and distributors" + + 3.times { FactoryGirl.create(:supplier) } + 3.times { FactoryGirl.create(:distributor) } +end + + +# -- Products +unless Spree::Product.count > 0 + puts "[db:seed] Seeding products" + + FactoryGirl.create(:product, + :name => 'Garlic', :price => 20.00, + :supplier => Spree::Supplier.all[0], + :distributors => [Spree::Distributor.all[0]], + :taxons => [Spree::Taxon.find_by_name('Vegetables')]) + + FactoryGirl.create(:product, + :name => 'Fuji Apple', :price => 5.00, + :supplier => Spree::Supplier.all[1], + :distributors => Spree::Distributor.all, + :taxons => [Spree::Taxon.find_by_name('Fruit')]) + + FactoryGirl.create(:product, + :name => 'Beef - 5kg Trays', :price => 50.00, + :supplier => Spree::Supplier.all[2], + :distributors => [Spree::Distributor.all[2]], + :taxons => [Spree::Taxon.find_by_name('Meat and Fish')]) +end diff --git a/spec/support/factories.rb b/spec/support/factories.rb index b3fd78299d..c02f2702a3 100644 --- a/spec/support/factories.rb +++ b/spec/support/factories.rb @@ -9,8 +9,8 @@ FactoryGirl.define do address '4 McDougal Rd' city 'Austinvale' postcode '2312' - state Spree::State.find_by_name('Victoria') - country Spree::Country.find_by_name('Australia') + state { Spree::State.find_by_name('Victoria') } + country { Spree::Country.find_by_name('Australia') } end factory :distributor, :class => Spree::Distributor do @@ -23,8 +23,8 @@ FactoryGirl.define do pickup_times "Whenever you're free" city 'Cheshire' post_code '2312' - state Spree::State.find_by_name('Victoria') - country Spree::Country.find_by_name('Australia') + state { Spree::State.find_by_name('Victoria') } + country { Spree::Country.find_by_name('Australia') } end end