From 8a86c0473d7a8b0384c0e4827e787ad57dc9121e Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sun, 27 Jan 2019 17:35:49 +0000 Subject: [PATCH 1/2] Change app config to keep using the same v1 shipping method calculators and not the ones spree v2 introduces, these are copy pasted versions that work with packages See this commit for more details: https://github.com/openfoodfoundation/spree/commit/18e5b98f5cc31a973b63cf89eabb513e7230e3ca#diff-b0846898827183f530c113ad7b83b8ea Also: - remove shipping method restriction on calculators to inherit from Spree::Shipping::ShippingCalculator so that OFN customized calculators keep working - add shipping method serializer spec to test serialization of all shipping methods configured --- app/models/spree/shipping_method_decorator.rb | 8 +++++++ config/application.rb | 10 ++++++++- .../api/shipping_method_serializer_spec.rb | 21 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 spec/serializers/api/shipping_method_serializer_spec.rb diff --git a/app/models/spree/shipping_method_decorator.rb b/app/models/spree/shipping_method_decorator.rb index bd2a16af12..dd4c7d71de 100644 --- a/app/models/spree/shipping_method_decorator.rb +++ b/app/models/spree/shipping_method_decorator.rb @@ -42,6 +42,14 @@ Spree::ShippingMethod.class_eval do ] end + # This method is overriden so that we can remove the restriction added in Spree + # Spree restricts shipping method calculators to the ones that inherit from Spree::Shipping::ShippingCalculator + # Spree::Shipping::ShippingCalculator makes sure that calculators are able to handle packages and not orders as input + # This is not necessary in OFN because calculators in OFN are already customized to work with different types of input + def self.calculators + spree_calculators.send model_name_without_spree_namespace + end + def has_distributor?(distributor) self.distributors.include?(distributor) end diff --git a/config/application.rb b/config/application.rb index f80c91aee1..de267a8956 100644 --- a/config/application.rb +++ b/config/application.rb @@ -55,7 +55,15 @@ module Openfoodnetwork # Register Spree calculators initializer 'spree.register.calculators' do |app| - app.config.spree.calculators.shipping_methods << Calculator::Weight + app.config.spree.calculators.shipping_methods = [ + Spree::Calculator::FlatPercentItemTotal, + Spree::Calculator::FlatRate, + Spree::Calculator::FlexiRate, + Spree::Calculator::PerItem, + Spree::Calculator::PriceSack, + Calculator::Weight + ] + app.config.spree.calculators.add_class('enterprise_fees') config.spree.calculators.enterprise_fees = [ Calculator::FlatPercentPerItem, diff --git a/spec/serializers/api/shipping_method_serializer_spec.rb b/spec/serializers/api/shipping_method_serializer_spec.rb new file mode 100644 index 0000000000..7ee9cb4d53 --- /dev/null +++ b/spec/serializers/api/shipping_method_serializer_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe Api::ShippingMethodSerializer do + let(:shipping_method) { create(:shipping_method) } + + it "serializes a test shipping_method" do + serializer = Api::ShippingMethodSerializer.new shipping_method + + expect(serializer.to_json).to match(shipping_method.name) + end + + it "can serialize all configured shipping method calculators" do + Rails.application.config.spree.calculators.shipping_methods.each do |calculator| + shipping_method.calculator = calculator.new + serializer = Api::ShippingMethodSerializer.new shipping_method + allow(serializer).to receive(:options).and_return(current_order: create(:order)) + + expect(serializer.price).to eq(0.0) + end + end +end From 3595abc08aab70460ab83554f4f95b6a3d866b16 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 29 Jan 2019 20:19:29 +0000 Subject: [PATCH 2/2] Fix seeds script for cases where default stock location already exists in the DB --- db/seeds.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 9f0868cb6b..d1ae7f4a3d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -51,4 +51,4 @@ end spree_user = Spree::User.first spree_user && spree_user.confirm! -DefaultStockLocation.create! +DefaultStockLocation.find_or_create