From 1a4e83d63388409e9748656bca443528f6454beb Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 31 May 2019 20:40:25 +0100 Subject: [PATCH] Change products controller to clear variants unit description if variant_unit is items --- .../admin/products_controller_decorator.rb | 8 ++++++ .../spree/admin/products_controller_spec.rb | 28 ++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index ed3f8039df..f48ac0fef4 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -39,6 +39,8 @@ Spree::Admin::ProductsController.class_eval do delete_stock_params_and_set_after do super end + + clear_variants_unit_description if @object.variant_unit == 'items' end def bulk_update @@ -154,4 +156,10 @@ Spree::Admin::ProductsController.class_eval do def set_product_master_variant_price_to_zero @product.price = 0 if @product.price.nil? end + + def clear_variants_unit_description + @object.variants.each do |variant| + variant.update_attribute :unit_description, '' + end + end end diff --git a/spec/controllers/spree/admin/products_controller_spec.rb b/spec/controllers/spree/admin/products_controller_spec.rb index 1f6356b98b..e64a69d8bc 100644 --- a/spec/controllers/spree/admin/products_controller_spec.rb +++ b/spec/controllers/spree/admin/products_controller_spec.rb @@ -150,17 +150,31 @@ describe Spree::Admin::ProductsController, type: :controller do end describe "updating" do + let(:producer) { create(:enterprise) } + let!(:product) { create(:simple_product, supplier: producer) } + + before do + @request.env['HTTP_REFERER'] = 'http://test.com/' + login_as_enterprise_user [producer] + end + + describe "product variant unit is items" do + it "clears unit description of all variants of the product" do + product.variants.first.update_attribute :unit_description, "grams" + spree_put :update, + id: product, + product: { + variant_unit: "items", + variant_unit_name: "bag" + } + expect(product.reload.variants.first.unit_description).to be_empty + end + end + describe "product properties" do context "as an enterprise user" do - let(:producer) { create(:enterprise) } - let!(:product) { create(:simple_product, supplier: producer) } let!(:property) { create(:property, name: "A nice name") } - before do - @request.env['HTTP_REFERER'] = 'http://test.com/' - login_as_enterprise_user [producer] - end - context "when a submitted property does not already exist" do it "does not create a new property, or product property" do spree_put :update,