Merge pull request #4262 from daningenthron/daningenthron/default-shipping-category

Prefill shipping category forms with default values
This commit is contained in:
Luis Ramos
2019-10-13 19:52:43 +01:00
committed by GitHub
11 changed files with 77 additions and 8 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -52,3 +52,4 @@ end
require File.join(File.dirname(__FILE__), 'default', 'users')
DefaultStockLocation.find_or_create
DefaultShippingCategory.find_or_create

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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"

View File

@@ -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]

View File

@@ -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