From db6fb2b55b7786f69d66deaf00d40ed5efb59cff Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 8 Apr 2021 00:03:53 +0100 Subject: [PATCH 1/2] Switch from map, that does not exist in ActionController::Parameters, to each --- app/services/cart_service.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/services/cart_service.rb b/app/services/cart_service.rb index 35dcb720f0..c4f3b5a5c0 100644 --- a/app/services/cart_service.rb +++ b/app/services/cart_service.rb @@ -106,13 +106,15 @@ class CartService end def read_variants_hash(data) - (data[:variants] || []).map do |variant_id, quantity| - if quantity.is_a?(Hash) - { variant_id: variant_id, quantity: quantity[:quantity], max_quantity: quantity[:max_quantity] } + variants_array = [] + (data[:variants] || []).each do |variant_id, quantity| + if quantity.is_a?(ActionController::Parameters) + variants_array.push({ variant_id: variant_id, quantity: quantity[:quantity], max_quantity: quantity[:max_quantity] }) else - { variant_id: variant_id, quantity: quantity } + variants_array.push({ variant_id: variant_id, quantity: quantity }) end end + variants_array end def cart_remove(variant_id) From 7c2b3993c612219dde32980faa5e346dd88dc7e2 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 8 Apr 2021 21:26:01 +0100 Subject: [PATCH 2/2] Fix cart_service_spec to adapt to the actual usage of it from cart_controller, params are now sent inside a Parameters object, not as a hash --- spec/services/cart_service_spec.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/spec/services/cart_service_spec.rb b/spec/services/cart_service_spec.rb index b8144f867b..51e8f2b882 100644 --- a/spec/services/cart_service_spec.rb +++ b/spec/services/cart_service_spec.rb @@ -27,7 +27,8 @@ describe CartService do describe "#populate" do it "adds a variant" do cart_service.populate( - { variants: { variant.id.to_s => { quantity: '1', max_quantity: '2' } } }, + ActionController::Parameters.new({ variants: { variant.id.to_s => { quantity: '1', + max_quantity: '2' } } }), true ) li = order.find_line_item_by_variant(variant) @@ -41,7 +42,8 @@ describe CartService do order.add_variant variant, 1, 2 cart_service.populate( - { variants: { variant.id.to_s => { quantity: '2', max_quantity: '3' } } }, + ActionController::Parameters.new({ variants: { variant.id.to_s => { quantity: '2', + max_quantity: '3' } } }), true ) li = order.find_line_item_by_variant(variant) @@ -54,7 +56,7 @@ describe CartService do it "removes a variant" do order.add_variant variant, 1, 2 - cart_service.populate({ variants: {} }, true) + cart_service.populate(ActionController::Parameters.new({ variants: {} }), true) order.line_items.reload li = order.find_line_item_by_variant(variant) expect(li).not_to be @@ -67,7 +69,7 @@ describe CartService do it "does not add the deleted variant to the cart" do variant.delete - cart_service.populate({ variants: { variant.id.to_s => { quantity: '2' } } }, true) + cart_service.populate(ActionController::Parameters.new({ variants: { variant.id.to_s => { quantity: '2' } } }), true) expect(relevant_line_item).to be_nil expect(cart_service.errors.count).to be 0 @@ -82,7 +84,7 @@ describe CartService do it "removes the line_item from the cart" do variant.delete - cart_service.populate({ variants: { variant.id.to_s => { quantity: '3' } } }, true) + cart_service.populate(ActionController::Parameters.new({ variants: { variant.id.to_s => { quantity: '3' } } }), true) expect(Spree::LineItem.where(id: relevant_line_item).first).to be_nil expect(cart_service.errors.count).to be 0