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/app/models/spree/payment_method.rb b/app/models/spree/payment_method.rb index d09c3c7c4c..f90a526f7c 100644 --- a/app/models/spree/payment_method.rb +++ b/app/models/spree/payment_method.rb @@ -113,7 +113,7 @@ module Spree end def init - self.calculator ||= ::Calculator::FlatRate.new(preferred_amount: 0) + self.calculator ||= ::Calculator::None.new end def has_distributor?(distributor) diff --git a/app/models/spree/shipping_method.rb b/app/models/spree/shipping_method.rb index 589bc8ce8c..b675e8fa1e 100644 --- a/app/models/spree/shipping_method.rb +++ b/app/models/spree/shipping_method.rb @@ -32,6 +32,8 @@ module Spree validate :at_least_one_shipping_category validates :display_on, inclusion: { in: DISPLAY_ON_OPTIONS.values }, allow_nil: true + after_initialize :init + after_save :touch_distributors scope :managed_by, lambda { |user| @@ -67,10 +69,6 @@ module Spree tracking_url.gsub(/:tracking/, tracking) unless tracking.blank? || tracking_url.blank? end - def self.calculators - spree_calculators.__send__ model_name_without_spree_namespace - end - # Some shipping methods are only meant to be set via backend def frontend? display_on != "back_end" @@ -110,6 +108,10 @@ module Spree where(display_on: [nil, ""]) end + def init + self.calculator ||= ::Calculator::None.new if new_record? + end + private def no_active_or_upcoming_order_cycle_distributors_with_only_one_shipping_method? diff --git a/config/application.rb b/config/application.rb index 9e6b47d006..da1ad4e109 100644 --- a/config/application.rb +++ b/config/application.rb @@ -114,7 +114,8 @@ module Openfoodnetwork Calculator::FlexiRate, Calculator::PerItem, Calculator::PriceSack, - Calculator::Weight + Calculator::Weight, + Calculator::None ] app.config.spree.calculators.add_class('enterprise_fees') @@ -133,7 +134,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/lib/reports/enterprise_fee_summary/enterprise_fee_summary_report_spec.rb b/spec/lib/reports/enterprise_fee_summary/enterprise_fee_summary_report_spec.rb index 007667eb80..6ea086c7dd 100644 --- a/spec/lib/reports/enterprise_fee_summary/enterprise_fee_summary_report_spec.rb +++ b/spec/lib/reports/enterprise_fee_summary/enterprise_fee_summary_report_spec.rb @@ -646,12 +646,12 @@ describe Reporting::Reports::EnterpriseFeeSummary::Base do describe "for specified payment methods" do let!(:payment_method_a) do - method = create(:payment_method, name: "Payment A", distributors: [distributor]) + method = create(:payment_method, :flat_rate, name: "Payment A", distributors: [distributor]) method.calculator.update_attribute(:preferred_amount, 1) method end let!(:payment_method_b) do - method = create(:payment_method, name: "Payment B", distributors: [distributor]) + method = create(:payment_method, :flat_rate, name: "Payment B", distributors: [distributor]) method.calculator.update_attribute(:preferred_amount, 1) method end diff --git a/spec/system/admin/payment_method_spec.rb b/spec/system/admin/payment_method_spec.rb index 5af8257dd7..55b2a412ec 100644 --- a/spec/system/admin/payment_method_spec.rb +++ b/spec/system/admin/payment_method_spec.rb @@ -247,10 +247,21 @@ describe ' end describe "Setting transaction fees", js: true do - let(:calculator) { build(:calculator) } - let!(:payment_method) { create(:payment_method, calculator: calculator) } + let!(:payment_method) { create(:payment_method) } before { login_as_admin_and_visit spree.edit_admin_payment_method_path payment_method } + it "set by default 'None' as calculator" do + expect(page).to have_select "calc_type", selected: "None" + end + + 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' } @@ -265,9 +276,11 @@ describe ' end context "using Flat Rate (per order) calculator" do - # flat rate per order is the default calculator; no need select it and update page + before { select2_select "Flat Rate (per order)", from: 'calc_type' } it "inserts values which persist" do + expect(page).to have_content("you must save first before") + click_button 'Update' fill_in "Amount", with: 2.2 click_button 'Update' expect(page).to have_content("Payment Method has been successfully updated!") 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