mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Extend PaymentMethod with calculated_adjustments
This commit is contained in:
@@ -4,6 +4,10 @@ Spree::PaymentMethod.class_eval do
|
||||
|
||||
attr_accessible :distributor_ids
|
||||
|
||||
calculated_adjustments
|
||||
|
||||
after_initialize :init
|
||||
|
||||
validates :distributors, presence: { message: "^At least one hub must be selected" }
|
||||
|
||||
# -- Scopes
|
||||
@@ -31,6 +35,11 @@ Spree::PaymentMethod.class_eval do
|
||||
where('spree_payment_methods.environment=? OR spree_payment_methods.environment=? OR spree_payment_methods.environment IS NULL', Rails.env, '')
|
||||
}
|
||||
|
||||
def init
|
||||
self.class.calculated_adjustments unless reflections.keys.include? :calculator
|
||||
self.calculator ||= Spree::Calculator::FlatRate.new(preferred_amount: 0)
|
||||
end
|
||||
|
||||
def has_distributor?(distributor)
|
||||
self.distributors.include?(distributor)
|
||||
end
|
||||
|
||||
@@ -25,5 +25,13 @@ module Spree
|
||||
# Testing else condition
|
||||
Spree::Gateway::BogusSimple.clean_name.should == "BogusSimple"
|
||||
end
|
||||
|
||||
it "computes the amount of fees" do
|
||||
pickup = create(:payment_method, name: 'pickup')
|
||||
order = create(:order)
|
||||
expect(pickup.compute_amount(order)).to eq 0
|
||||
delivery = create(:payment_method, name: 'delivery', calculator: Calculator::FlatRate.new(preferred_amount: 10))
|
||||
expect(delivery.compute_amount(order)).to eq 10
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user