From c3029c612ae57a3cbde520a15ff4803cfeb5a74a Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 8 Jan 2019 15:39:53 +1100 Subject: [PATCH] Fix sample product creation and use bang methods Mistakes like the missing fee when creating product distributions were hidden, because I didn't use the bang methods to create records. --- lib/tasks/sample_data/addressing.rb | 2 +- lib/tasks/sample_data/inventory_factory.rb | 4 ++-- lib/tasks/sample_data/product_factory.rb | 17 ++++++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/tasks/sample_data/addressing.rb b/lib/tasks/sample_data/addressing.rb index 1a50a8df95..cfdf23c505 100644 --- a/lib/tasks/sample_data/addressing.rb +++ b/lib/tasks/sample_data/addressing.rb @@ -15,7 +15,7 @@ module Addressing def zone zone = Spree::Zone.find_or_create_by_name!("Australia") - zone.members.create(zonable: country) + zone.members.create!(zonable: country) zone end diff --git a/lib/tasks/sample_data/inventory_factory.rb b/lib/tasks/sample_data/inventory_factory.rb index d37557f67f..b4147e024d 100644 --- a/lib/tasks/sample_data/inventory_factory.rb +++ b/lib/tasks/sample_data/inventory_factory.rb @@ -18,7 +18,7 @@ class InventoryFactory enterprise: shop, variant: product.variants.first, visible: true - ).find_or_create_by_variant_id(product.variants.first.id) + ).find_or_create_by_variant_id!(product.variants.first.id) create_override(shop, product) end @@ -29,6 +29,6 @@ class InventoryFactory price: 12, on_demand: false, count_on_hand: 5 - ).find_or_create_by_variant_id(product.variants.first.id) + ).find_or_create_by_variant_id!(product.variants.first.id) end end diff --git a/lib/tasks/sample_data/product_factory.rb b/lib/tasks/sample_data/product_factory.rb index b5b6544cb0..260ddb49cc 100644 --- a/lib/tasks/sample_data/product_factory.rb +++ b/lib/tasks/sample_data/product_factory.rb @@ -76,15 +76,18 @@ class ProductFactory unit_value: 1, on_demand: true ) - create_product_with_distribution(params) + create_product_with_distribution(params, hash[:supplier]) end - def create_product_with_distribution(params) - product = Spree::Product.create_with(params).find_or_create_by_name(params[:name]) - ProductDistribution.create( - product: product, - distributor: params[:distributor] - ) + def create_product_with_distribution(params, supplier) + product = Spree::Product.create_with(params).find_or_create_by_name!(params[:name]) + + distribution_params = { + distributor_id: params[:distributor].id, + enterprise_fee_id: supplier.enterprise_fees.first.id + } + ProductDistribution.create_with(distribution_params).find_or_create_by_product_id!(product.id) + product end end