diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index e05ba14792..22f5337ee5 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -30,6 +30,11 @@ Spree::Admin::ProductsController.class_eval do @show_latest_import = params[:latest_import] || false end + def new + @object.shipping_category = DefaultShippingCategory.find_or_create + super + end + def create delete_stock_params_and_set_after do super diff --git a/app/controllers/spree/admin/shipping_methods_controller.rb b/app/controllers/spree/admin/shipping_methods_controller.rb index d4c9c29898..b561759757 100644 --- a/app/controllers/spree/admin/shipping_methods_controller.rb +++ b/app/controllers/spree/admin/shipping_methods_controller.rb @@ -19,6 +19,11 @@ module Spree collection end + def new + @object.shipping_categories = [DefaultShippingCategory.find_or_create] + super + end + def destroy # Our reports are not adapted to soft deleted shipping_methods so here we prevent # the deletion (even soft) of shipping_methods that are referenced in orders diff --git a/app/services/default_shipping_category.rb b/app/services/default_shipping_category.rb new file mode 100644 index 0000000000..10959de659 --- /dev/null +++ b/app/services/default_shipping_category.rb @@ -0,0 +1,13 @@ +# Encapsulates the concept of default stock location in creation of a product or a shipping method. + +class DefaultShippingCategory + NAME = 'Default'.freeze + + def self.create! + Spree::ShippingCategory.create!(name: NAME) + end + + def self.find_or_create + Spree::ShippingCategory.find_or_create_by_name(NAME) + end +end diff --git a/app/views/spree/admin/products/_shipping_category_form.html.haml b/app/views/spree/admin/products/_shipping_category_form.html.haml index 1e324a25b1..0f3635d698 100644 --- a/app/views/spree/admin/products/_shipping_category_form.html.haml +++ b/app/views/spree/admin/products/_shipping_category_form.html.haml @@ -1,4 +1,4 @@ = f.field_container :shipping_categories do = f.label :shipping_category_id, t(:shipping_category) - = f.collection_select(:shipping_category_id, Spree::ShippingCategory.all, :id, :name, {:include_blank => true}, {:class => 'select2 fullwidth'}) + = f.collection_select(:shipping_category_id, Spree::ShippingCategory.all, :id, :name, {:include_blank => false}, {:class => 'select2 fullwidth'}) = f.error_message_on :shipping_category_id diff --git a/db/seeds.rb b/db/seeds.rb index 1cb78e94ff..e6cac36c2c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -52,3 +52,4 @@ end require File.join(File.dirname(__FILE__), 'default', 'users') DefaultStockLocation.find_or_create +DefaultShippingCategory.find_or_create diff --git a/lib/tasks/sample_data/product_factory.rb b/lib/tasks/sample_data/product_factory.rb index 09cc60d3d3..01e92dddb7 100644 --- a/lib/tasks/sample_data/product_factory.rb +++ b/lib/tasks/sample_data/product_factory.rb @@ -72,7 +72,7 @@ class ProductFactory variant_unit: "weight", variant_unit_scale: 1, unit_value: 1, - shipping_category: Spree::ShippingCategory.find_or_create_by_name('Default') + shipping_category: DefaultShippingCategory.find_or_create ) product = Spree::Product.create_with(params).find_or_create_by_name!(params[:name]) product.variants.first.update_attribute :on_demand, true diff --git a/spec/factories.rb b/spec/factories.rb index 450984f89c..4f9c6478f5 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -194,4 +194,9 @@ FactoryBot.modify do # sets the default value for variant.on_demand backorderable_default false end + + factory :shipping_category, class: Spree::ShippingCategory do + initialize_with { DefaultShippingCategory.find_or_create } + transient { name 'Default' } + end end diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index 1756cdc539..9e1d0a4895 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -9,7 +9,7 @@ feature ' let!(:taxon) { create(:taxon) } let!(:stock_location) { create(:stock_location, backorderable_default: false) } - let!(:shipping_category) { create(:shipping_category, name: 'Test Shipping Category') } + let!(:shipping_category) { DefaultShippingCategory.find_or_create } background do @supplier = create(:supplier_enterprise, name: 'New supplier') @@ -32,6 +32,8 @@ feature ' click_link 'Products' click_link 'New Product' + expect(find_field('product_shipping_category_id').text).to eq(shipping_category.name) + select 'New supplier', from: 'product_supplier_id' fill_in 'product_name', with: 'A new product !!!' select "Weight (kg)", from: 'product_variant_unit_with_scale' @@ -40,7 +42,6 @@ feature ' fill_in 'product_price', with: '19.99' fill_in 'product_on_hand', with: 5 select 'Test Tax Category', from: 'product_tax_category_id' - select 'Test Shipping Category', from: 'product_shipping_category_id' page.find("div[id^='taTextElement']").native.send_keys('A description...') click_button 'Create' @@ -80,7 +81,6 @@ feature ' fill_in 'product_on_hand', with: 0 check 'product_on_demand' select 'Test Tax Category', from: 'product_tax_category_id' - select 'Test Shipping Category', from: 'product_shipping_category_id' page.find("div[id^='taTextElement']").native.send_keys('In demand, and on_demand! The hottest cakes in town.') click_button 'Create' @@ -122,7 +122,6 @@ feature ' select 'Weight (g)', from: 'product_variant_unit_with_scale' fill_in 'product_unit_value_with_description', with: '500' select taxon.name, from: "product_primary_taxon_id" - select 'Test Shipping Category', from: 'product_shipping_category_id' select 'None', from: "product_tax_category_id" # Should only have suppliers listed which the user can manage diff --git a/spec/features/admin/shipping_methods_spec.rb b/spec/features/admin/shipping_methods_spec.rb index 53e58c595a..ca00af3457 100644 --- a/spec/features/admin/shipping_methods_spec.rb +++ b/spec/features/admin/shipping_methods_spec.rb @@ -75,6 +75,7 @@ feature 'shipping methods' do let(:sm1) { create(:shipping_method, name: 'One', distributors: [distributor1]) } let(:sm2) { create(:shipping_method, name: 'Two', distributors: [distributor1, distributor2]) } let(:sm3) { create(:shipping_method, name: 'Three', distributors: [distributor3]) } + let(:shipping_category) { create(:shipping_category) } before(:each) do enterprise_user.enterprise_roles.build(enterprise: distributor1).save @@ -97,10 +98,12 @@ feature 'shipping methods' do expect(page).to have_css 'div#shipping_method_zones_field' expect(page).to have_field 'shipping_method_require_ship_address_true', checked: true + # Auto-check default shipping category + expect(page).to have_field shipping_category.name, checked: true + fill_in 'shipping_method_name', with: 'Teleport' check "shipping_method_distributor_ids_#{distributor1.id}" - check "shipping_method_shipping_categories_" find(:css, "tags-input .tags input").set "local\n" within(".tags .tag-list") do expect(page).to have_css '.tag-item', text: "local" diff --git a/spec/models/stock/package_spec.rb b/spec/models/stock/package_spec.rb index 6b3fe4012a..726eed6c37 100644 --- a/spec/models/stock/package_spec.rb +++ b/spec/models/stock/package_spec.rb @@ -48,7 +48,7 @@ module Stock describe '#shipping_categories' do it "returns shipping categories that are not shipping categories of the order's products" do package - other_shipping_category = create(:shipping_category) + other_shipping_category = Spree::ShippingCategory.create(name: "Custom") expect(package.shipping_categories).to eq [shipping_method1.shipping_categories.first, other_shipping_category] diff --git a/spec/services/default_shipping_category_spec.rb b/spec/services/default_shipping_category_spec.rb new file mode 100644 index 0000000000..cab005df8a --- /dev/null +++ b/spec/services/default_shipping_category_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe DefaultShippingCategory do + describe '.create!' do + it "names the location 'Default'" do + shipping_category = described_class.create! + expect(shipping_category.name).to eq 'Default' + end + end + + describe 'find_or_create' do + context 'when a Default category already exists' do + let!(:category) do + Spree::ShippingCategory.create!(name: 'Default') + end + + it 'returns the category' do + expect(described_class.find_or_create).to eq category + end + + it 'does not create another category' do + expect { described_class.find_or_create }.not_to change(Spree::ShippingCategory, :count) + end + end + + context 'when a Default category does not exist' do + it 'returns the category' do + category = described_class.find_or_create + expect(category.name).to eq 'Default' + end + + it 'does not create another category' do + expect { described_class.find_or_create } + .to change(Spree::ShippingCategory, :count).from(0).to(1) + end + end + end +end