Merge pull request #10208 from jibees/9424-calculator-provide-a-calculator-none

Admin: Include a "None" option for calculator type user in shipping method and payment method forms
This commit is contained in:
Filipe
2023-01-26 14:48:41 +00:00
committed by GitHub
7 changed files with 51 additions and 12 deletions

View File

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

View File

@@ -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)

View File

@@ -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?

View File

@@ -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')

View File

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

View File

@@ -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!")

View File

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