From a793fc9f99ef2d3cbe8f7ecf7e6c36bfb27b72ef Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 26 Dec 2022 15:47:43 +0100 Subject: [PATCH] Create a "None" calculator for shipping and payment methods Only for payment and shipping methods --- app/models/calculator/none.rb | 13 +++++++++++++ config/application.rb | 6 ++++-- spec/system/admin/payment_method_spec.rb | 8 ++++++++ spec/system/admin/shipping_methods_spec.rb | 9 +++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 app/models/calculator/none.rb diff --git a/app/models/calculator/none.rb b/app/models/calculator/none.rb new file mode 100644 index 0000000000..92e3cbdfac --- /dev/null +++ b/app/models/calculator/none.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: false + +module Calculator + class None < Spree::Calculator + def self.description + I18n.t(:none) + end + + def compute(_object = nil) + 0 + end + end +end diff --git a/config/application.rb b/config/application.rb index 24880eafe0..106b3ed1a4 100644 --- a/config/application.rb +++ b/config/application.rb @@ -116,7 +116,8 @@ module Openfoodnetwork Calculator::FlexiRate, Calculator::PerItem, Calculator::PriceSack, - Calculator::Weight + Calculator::Weight, + Calculator::None ] app.config.spree.calculators.add_class('enterprise_fees') @@ -135,7 +136,8 @@ module Openfoodnetwork Calculator::FlatRate, Calculator::FlexiRate, Calculator::PerItem, - Calculator::PriceSack + Calculator::PriceSack, + Calculator::None ] app.config.spree.calculators.add_class('tax_rates') diff --git a/spec/system/admin/payment_method_spec.rb b/spec/system/admin/payment_method_spec.rb index 5af8257dd7..6210f6b72e 100644 --- a/spec/system/admin/payment_method_spec.rb +++ b/spec/system/admin/payment_method_spec.rb @@ -251,6 +251,14 @@ describe ' let!(:payment_method) { create(:payment_method, calculator: calculator) } before { login_as_admin_and_visit spree.edit_admin_payment_method_path payment_method } + it "handle the 'None' calculator" do + select2_select "None", from: 'calc_type' + click_button 'Update' + expect(page).to have_content("Payment Method has been successfully updated!") + expect(payment_method.reload.calculator_type).to eq "Calculator::None" + expect(page).to have_select "calc_type", selected: "None" + end + context "using Flat Percent calculator" do before { select2_select "Flat Percent", from: 'calc_type' } diff --git a/spec/system/admin/shipping_methods_spec.rb b/spec/system/admin/shipping_methods_spec.rb index 5ba477da16..ca3958fc4f 100644 --- a/spec/system/admin/shipping_methods_spec.rb +++ b/spec/system/admin/shipping_methods_spec.rb @@ -83,6 +83,15 @@ describe 'shipping methods' do expect(@shipping_method.reload.calculator_type).to eq("Calculator::PerItem") end + + it "handle when updating calculator type to 'None'" do + visit spree.edit_admin_shipping_method_path(@shipping_method) + + select2_select 'None', from: 'calc_type' + click_button 'Update' + + expect(@shipping_method.reload.calculator_type).to eq "Calculator::None" + end end context "as an enterprise user", js: true do