Extend PaymentMethod with calculated_adjustments

This commit is contained in:
Maikel Linke
2016-05-11 12:33:14 +10:00
parent 21b8445753
commit 16b80a2f1b
2 changed files with 17 additions and 0 deletions

View File

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

View File

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