From 58105030682d6dc4f213f2ffb07cf0bd708291b6 Mon Sep 17 00:00:00 2001 From: Andrew Spinks Date: Fri, 26 Jul 2013 11:47:25 +1000 Subject: [PATCH] Move test data seeding into separate rake task. Add new role to seeds file. --- db/seeds.rb | 83 ++++------------------------------------------ lib/tasks/dev.rake | 76 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 76 deletions(-) create mode 100644 lib/tasks/dev.rake diff --git a/db/seeds.rb b/db/seeds.rb index 85fc9e4d86..ea43787661 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,10 +1,6 @@ # 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). -require File.expand_path('../../spec/factories', __FILE__) -require File.expand_path('../../spec/support/spree/init', __FILE__) - - # -- Spree unless Spree::Country.find_by_name 'Australia' puts "[db:seed] Seeding Spree" @@ -12,12 +8,9 @@ unless Spree::Country.find_by_name 'Australia' 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' + country = Spree::Country.find_by_name('Australia') puts "[db:seed] Seeding states" [ @@ -30,74 +23,12 @@ unless Spree::State.find_by_name 'Victoria' ['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) + Spree::State.create!({"name"=>state[0], "abbr"=>state[1], :country=>country}, :without_protection => true) end end - -# -- Shipping / payment information -unless Spree::Zone.find_by_name 'Australia' - puts "[db:seed] Seeding shipping / payment information" - zone = FactoryGirl.create(:zone, :name => 'Australia', :zone_members => []) - country = Spree::Country.find_by_name('Australia') - Spree::ZoneMember.create(:zone => zone, :zoneable => country) - FactoryGirl.create(:shipping_method, :zone => zone) - FactoryGirl.create(:payment_method, :environment => 'development') -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 - - -# -- Enterprises -unless Enterprise.count > 0 - puts "[db:seed] Seeding enterprises" - - 3.times { FactoryGirl.create(:supplier_enterprise) } - 3.times { FactoryGirl.create(:distributor_enterprise) } -end - - -# -- Products -unless Spree::Product.count > 0 - puts "[db:seed] Seeding products" - - prod1 = FactoryGirl.create(:product, - :name => 'Garlic', :price => 20.00, - :supplier => Enterprise.is_primary_producer[0], - :taxons => [Spree::Taxon.find_by_name('Vegetables')]) - - ProductDistribution.create(:product => prod1, - :distributor => Enterprise.is_distributor[0], - :shipping_method => Spree::ShippingMethod.first) - - - prod2 = FactoryGirl.create(:product, - :name => 'Fuji Apple', :price => 5.00, - :supplier => Enterprise.is_primary_producer[1], - :taxons => [Spree::Taxon.find_by_name('Fruit')]) - - ProductDistribution.create(:product => prod2, - :distributor => Enterprise.is_distributor[1], - :shipping_method => Spree::ShippingMethod.first) - - prod3 = FactoryGirl.create(:product, - :name => 'Beef - 5kg Trays', :price => 50.00, - :supplier => Enterprise.is_primary_producer[2], - :taxons => [Spree::Taxon.find_by_name('Meat and Fish')]) - - ProductDistribution.create(:product => prod3, - :distributor => Enterprise.is_distributor[2], - :shipping_method => Spree::ShippingMethod.first) -end +# -- Roles +unless Spree::Role.find_by_name 'enterprise' + puts "seeding roles" + Spree::Role.create!(:name => "enterprise") +end \ No newline at end of file diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake new file mode 100644 index 0000000000..abbf1214de --- /dev/null +++ b/lib/tasks/dev.rake @@ -0,0 +1,76 @@ + +namespace :openfoodweb do + + namespace :dev do + + desc 'load sample data' + task :load_sample_data => :environment do + require File.expand_path('../../../spec/factories', __FILE__) + + # -- Shipping / payment information + unless Spree::Zone.find_by_name 'Australia' + puts "[db:seed] Seeding shipping / payment information" + zone = FactoryGirl.create(:zone, :name => 'Australia', :zone_members => []) + country = Spree::Country.find_by_name('Australia') + Spree::ZoneMember.create(:zone => zone, :zoneable => country) + FactoryGirl.create(:shipping_method, :zone => zone) + FactoryGirl.create(:payment_method, :environment => 'development') + 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 + + + # -- Enterprises + unless Enterprise.count > 0 + puts "[db:seed] Seeding enterprises" + + 3.times { FactoryGirl.create(:supplier_enterprise) } + 3.times { FactoryGirl.create(:distributor_enterprise) } + end + + + # -- Products + unless Spree::Product.count > 0 + puts "[db:seed] Seeding products" + + prod1 = FactoryGirl.create(:product, + :name => 'Garlic', :price => 20.00, + :supplier => Enterprise.is_primary_producer[0], + :taxons => [Spree::Taxon.find_by_name('Vegetables')]) + + ProductDistribution.create(:product => prod1, + :distributor => Enterprise.is_distributor[0], + :shipping_method => Spree::ShippingMethod.first) + + + prod2 = FactoryGirl.create(:product, + :name => 'Fuji Apple', :price => 5.00, + :supplier => Enterprise.is_primary_producer[1], + :taxons => [Spree::Taxon.find_by_name('Fruit')]) + + ProductDistribution.create(:product => prod2, + :distributor => Enterprise.is_distributor[1], + :shipping_method => Spree::ShippingMethod.first) + + prod3 = FactoryGirl.create(:product, + :name => 'Beef - 5kg Trays', :price => 50.00, + :supplier => Enterprise.is_primary_producer[2], + :taxons => [Spree::Taxon.find_by_name('Meat and Fish')]) + + ProductDistribution.create(:product => prod3, + :distributor => Enterprise.is_distributor[2], + :shipping_method => Spree::ShippingMethod.first) + end + end + end +end