Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Include more data when seeding database (#12694)
Damian Legawiecf7bb043a8977
diff --git a/core/app/services/spree/seeds/all.rb b/core/app/services/spree/seeds/all.rbindex 8c3e4c1569..73430304ad 100644--- a/core/app/services/spree/seeds/all.rb+++ b/core/app/services/spree/seeds/all.rb@@ -14,9 +14,11 @@ module SpreeRoles.call# additional data- DefaultReimbursementTypes.call+ ReturnsEnvironment.callShippingCategories.callStoreCreditCategories.call+ TaxCategories.call+ DigitalDelivery.call# store & stock locationStores.calldiff --git a/core/app/services/spree/seeds/digital_delivery.rb b/core/app/services/spree/seeds/digital_delivery.rbnew file mode 100644index 0000000000..ba6ec49628--- /dev/null+++ b/core/app/services/spree/seeds/digital_delivery.rb@@ -0,0 +1,20 @@+module Spree+ module Seeds+ class DigitalDelivery+ prepend Spree::ServiceModule::Base++ def call+ digital_shipping_category = Spree::ShippingCategory.find_or_create_by!(name: 'Digital')+ zones = Spree::Zone.all++ digital_shipping_method = Spree::ShippingMethod.find_or_initialize_by(name: Spree.t('digital.digital_delivery'))++ digital_shipping_method.display_on = 'both'+ digital_shipping_method.shipping_categories = [digital_shipping_category]+ digital_shipping_method.calculator ||= Spree::Calculator::Shipping::DigitalDelivery.create!+ digital_shipping_method.zones = zones+ digital_shipping_method.save!+ end+ end+ end+enddiff --git a/core/app/services/spree/seeds/returns_environment.rb b/core/app/services/spree/seeds/returns_environment.rbnew file mode 100644index 0000000000..161a6a71b3--- /dev/null+++ b/core/app/services/spree/seeds/returns_environment.rb@@ -0,0 +1,27 @@+module Spree+ module Seeds+ class ReturnsEnvironment+ prepend Spree::ServiceModule::Base++ def call+ Spree::RefundReason.find_or_create_by!(name: 'Return processing', mutable: false)+ [+ 'Better price available',+ 'Missed estimated delivery date',+ 'Missing parts or accessories',+ 'Damaged/Defective',+ 'Different from what was ordered',+ 'Different from description',+ 'No longer needed/wanted',+ 'Accidental order',+ 'Unauthorized purchase',+ ].each do |name|+ Spree::ReturnAuthorizationReason.find_or_create_by!(name: name)+ end+ Spree::ReimbursementType.find_or_create_by!(name: 'Store Credit', type: 'Spree::ReimbursementType::StoreCredit')+ Spree::ReimbursementType.find_or_create_by!(name: 'Exchange', type: 'Spree::ReimbursementType::Exchange')+ Spree::ReimbursementType.find_or_create_by!(name: 'Original payment', type: 'Spree::ReimbursementType::OriginalPayment')+ end+ end+ end+enddiff --git a/core/app/services/spree/seeds/tax_categories.rb b/core/app/services/spree/seeds/tax_categories.rbnew file mode 100644index 0000000000..ac5ee719eb--- /dev/null+++ b/core/app/services/spree/seeds/tax_categories.rb@@ -0,0 +1,12 @@+module Spree+ module Seeds+ class TaxCategories+ prepend Spree::ServiceModule::Base++ def call+ Spree::TaxCategory.find_or_create_by(name: 'Default', is_default: true)+ Spree::TaxCategory.find_or_create_by(name: 'Non-taxable')+ end+ end+ end+enddiff --git a/core/spec/services/spree/seeds/all_spec.rb b/core/spec/services/spree/seeds/all_spec.rbnew file mode 100644index 0000000000..8467657176--- /dev/null+++ b/core/spec/services/spree/seeds/all_spec.rb@@ -0,0 +1,9 @@+require 'spec_helper'++RSpec.describe Spree::Seeds::All do+ subject { described_class.call }++ it 'runs without raising errors' do+ expect { subject }.not_to raise_error+ end+enddiff --git a/core/spec/services/spree/seeds/digital_delivery_spec.rb b/core/spec/services/spree/seeds/digital_delivery_spec.rbnew file mode 100644index 0000000000..d953ad3e61--- /dev/null+++ b/core/spec/services/spree/seeds/digital_delivery_spec.rb@@ -0,0 +1,35 @@+require 'spec_helper'++RSpec.describe Spree::Seeds::DigitalDelivery do+ subject { described_class.call }++ describe 'ShippingMethod' do+ it 'creates a Digital Delivery shipping method' do+ expect { subject }.to change(Spree::ShippingMethod, :count).by(1)++ shipping_method = Spree::ShippingMethod.find_by(name: Spree.t('digital.digital_delivery'))+ expect(shipping_method).to be_present+ expect(shipping_method.display_on).to eq('both')+ expect(shipping_method.shipping_categories.first.name).to eq('Digital')+ expect(shipping_method.calculator).to be_a(Spree::Calculator::Shipping::DigitalDelivery)+ expect(shipping_method.zones).to match_array(Spree::Zone.all)+ end++ context 'when Digital Delivery shipping method already exists' do+ before do+ shipping_category = Spree::ShippingCategory.find_or_create_by(name: 'Digital')+ Spree::ShippingMethod.create!(+ name: Spree.t('digital.digital_delivery'),+ display_on: 'both',+ shipping_categories: [shipping_category],+ calculator: Spree::Calculator::Shipping::DigitalDelivery.create!,+ zones: Spree::Zone.all+ )+ end++ it "doesn't create a new shipping method" do+ expect { subject }.not_to change(Spree::ShippingMethod, :count)+ end+ end+ end+enddiff --git a/core/spec/services/spree/seeds/returns_environment_spec.rb b/core/spec/services/spree/seeds/returns_environment_spec.rbnew file mode 100644index 0000000000..c5a2d3579b--- /dev/null+++ b/core/spec/services/spree/seeds/returns_environment_spec.rb@@ -0,0 +1,89 @@+require 'spec_helper'++RSpec.describe Spree::Seeds::ReturnsEnvironment do+ subject { described_class.call }++ describe 'RefundReason' do+ it 'creates a Return processing RefundReason' do+ expect { subject }.to change { Spree::RefundReason.where(name: 'Return processing', mutable: false).count }.by(1)+ end++ context 'when the RefundReason already exists' do+ before do+ Spree::RefundReason.create!(name: 'Return processing', mutable: false)+ end++ it "doesn't create a new RefundReason" do+ expect { subject }.not_to change(Spree::RefundReason, :count)+ end+ end+ end++ describe 'ReturnAuthorizationReason' do+ let(:expected_reasons) do+ [+ 'Better price available',+ 'Missed estimated delivery date',+ 'Missing parts or accessories',+ 'Damaged/Defective',+ 'Different from what was ordered',+ 'Different from description',+ 'No longer needed/wanted',+ 'Accidental order',+ 'Unauthorized purchase'+ ]+ end++ it 'creates all ReturnAuthorizationReasons' do+ expect { subject }.to change(Spree::ReturnAuthorizationReason, :count).by(expected_reasons.count)++ expected_reasons.each do |reason|+ expect(Spree::ReturnAuthorizationReason.find_by(name: reason)).to be_present+ end+ end++ context 'when ReturnAuthorizationReasons already exist' do+ before do+ expected_reasons.each do |reason|+ Spree::ReturnAuthorizationReason.create!(name: reason)+ end+ end++ it "doesn't create new ReturnAuthorizationReasons" do+ expect { subject }.not_to change(Spree::ReturnAuthorizationReason, :count)+ end+ end+ end++ describe 'ReimbursementType' do+ let(:expected_types) do+ [+ { name: 'Store Credit', type: 'Spree::ReimbursementType::StoreCredit' },+ { name: 'Exchange', type: 'Spree::ReimbursementType::Exchange' },+ { name: 'Original payment', type: 'Spree::ReimbursementType::OriginalPayment' }+ ]+ end++ it 'creates all ReimbursementTypes' do+ expect { subject }.to change(Spree::ReimbursementType, :count).by(expected_types.count)++ expected_types.each do |type_attrs|+ reimbursement_type = Spree::ReimbursementType.find_by(name: type_attrs[:name])+ expect(reimbursement_type).to be_present+ expect(reimbursement_type.type).to eq(type_attrs[:type])+ end+ end++ context 'when ReimbursementTypes already exist' do+ before do+ expected_types.each do |type_attrs|+ Spree::ReimbursementType.create!(name: type_attrs[:name], type: type_attrs[:type])+ end+ end++ it "doesn't create new ReimbursementTypes" do+ expect { subject }.not_to change(Spree::ReimbursementType, :count)+ end+ end+ end+enddiff --git a/core/spec/services/spree/seeds/tax_categories_spec.rb b/core/spec/services/spree/seeds/tax_categories_spec.rbnew file mode 100644index 0000000000..e0b9ce787a--- /dev/null+++ b/core/spec/services/spree/seeds/tax_categories_spec.rb@@ -0,0 +1,36 @@+require 'spec_helper'++RSpec.describe Spree::Seeds::TaxCategories do+ subject { described_class.call }++ describe 'TaxCategory' do+ let(:expected_categories) do+ [+ { name: 'Default', is_default: true },+ { name: 'Non-taxable', is_default: false }+ ]+ end++ it 'creates all TaxCategories' do+ expect { subject }.to change(Spree::TaxCategory, :count).by(expected_categories.count)++ expected_categories.each do |category_attrs|+ tax_category = Spree::TaxCategory.find_by(name: category_attrs[:name])+ expect(tax_category).to be_present+ expect(tax_category.is_default).to eq(category_attrs[:is_default])+ end+ end++ context 'when TaxCategories already exist' do+ before do+ expected_categories.each do |category_attrs|+ Spree::TaxCategory.create!(name: category_attrs[:name], is_default: category_attrs[:is_default])+ end+ end++ it "doesn't create new TaxCategories" do+ expect { subject }.not_to change(Spree::TaxCategory, :count)+ end+ end+ end+enddiff --git a/sample/db/samples/reimbursement_types.rb b/sample/db/samples/reimbursement_types.rbdeleted file mode 100644index 2a8b1bf90c..0000000000--- a/sample/db/samples/reimbursement_types.rb+++ /dev/null@@ -1,11 +0,0 @@-reasons = [- ['Exchange', 'Spree::ReimbursementType::Exchange'],- ['Original payment', 'Spree::ReimbursementType::OriginalPayment'],- ['Store credit', 'Spree::ReimbursementType::StoreCredit'],-].map do |name, kind|- { name: name, type: kind }-end--reasons.each do |reason|- Spree::ReimbursementType.find_or_create_by!(name: reason[:name], type: reason[:type])-enddiff --git a/sample/db/samples/return_authorization_reasons.rb b/sample/db/samples/return_authorization_reasons.rbdeleted file mode 100644index bee4c2359a..0000000000--- a/sample/db/samples/return_authorization_reasons.rb+++ /dev/null@@ -1,19 +0,0 @@-reasons = [- 'Better price available',- 'Missed estimated delivery date',- 'Missing parts or accessories',- 'Damaged/Defective',- 'Different from what was ordered',- 'Different from description',- 'No longer needed/wanted',- 'Accidental order',- 'Unauthorized purchase',-].map do |name|- {name: name}-end--if ActiveRecord::Base.connection.adapter_name == 'Mysql2'- Spree::ReturnAuthorizationReason.insert_all(reasons)-else- Spree::ReturnAuthorizationReason.upsert_all(reasons, unique_by: :name)-enddiff --git a/sample/lib/spree_sample.rb b/sample/lib/spree_sample.rbindex a4ead882bd..f10d7911ea 100644--- a/sample/lib/spree_sample.rb+++ b/sample/lib/spree_sample.rb@@ -28,7 +28,6 @@ module SpreeSampleSpree::Sample.load_sample('adjustments')Spree::Sample.load_sample('payments')Spree::Sample.load_sample('reimbursements')- Spree::Sample.load_sample('return_authorization_reasons')Spree::Sample.load_sample('posts')end