diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index 915eb65ae8..97ae3c6bc7 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -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 diff --git a/spec/models/spree/payment_method_spec.rb b/spec/models/spree/payment_method_spec.rb index b61fd3bffd..0e7c61592b 100644 --- a/spec/models/spree/payment_method_spec.rb +++ b/spec/models/spree/payment_method_spec.rb @@ -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