From b4be2cc2d41ebbce5f22852b874ca0457124b034 Mon Sep 17 00:00:00 2001 From: Dan Ingenthron Date: Tue, 17 Sep 2019 23:02:19 -0500 Subject: [PATCH] Add default shipping category service and update create product form --- app/services/default_shipping_category.rb | 13 +++++++++++++ .../products/_shipping_category_form.html.haml | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 app/services/default_shipping_category.rb diff --git a/app/services/default_shipping_category.rb b/app/services/default_shipping_category.rb new file mode 100644 index 0000000000..ae515fa59a --- /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 \ No newline at end of file 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 0f3635d698..6e8e1a5dbd 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 => false}, {:class => 'select2 fullwidth'}) + = f.collection_select(:shipping_category_id, Spree::ShippingCategory.all, :id, :name, {:include_blank => false, :selected => DefaultShippingCategory.find_or_create.id}, {:class => 'select2 fullwidth'}) = f.error_message_on :shipping_category_id