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