mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
Add by-weight shipping calculator
This commit is contained in:
15
app/models/open_food_web/calculator/weight.rb
Normal file
15
app/models/open_food_web/calculator/weight.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
module OpenFoodWeb
|
||||
class Calculator::Weight < Spree::Calculator
|
||||
preference :per_kg, :decimal, :default => 0.0
|
||||
attr_accessible :preferred_per_kg
|
||||
|
||||
def self.description
|
||||
"Weight (per kg)"
|
||||
end
|
||||
|
||||
def compute(object)
|
||||
total_weight = object.line_items.inject(0) { |sum, li| sum+li.variant.weight }
|
||||
total_weight * self.preferred_per_kg
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -27,6 +27,7 @@ module Openfoodweb
|
||||
# Register Spree calculators
|
||||
initializer "spree.register.calculators" do |app|
|
||||
app.config.spree.calculators.shipping_methods << OpenFoodWeb::Calculator::Itemwise
|
||||
app.config.spree.calculators.shipping_methods << OpenFoodWeb::Calculator::Weight
|
||||
end
|
||||
|
||||
|
||||
|
||||
16
spec/models/calculator/weight_spec.rb
Normal file
16
spec/models/calculator/weight_spec.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe OpenFoodWeb::Calculator::Weight do
|
||||
it "computes shipping cost for an order by total weight" do
|
||||
variant_1 = double(:variant, :weight => 10)
|
||||
variant_2 = double(:variant, :weight => 20)
|
||||
|
||||
line_item_1 = double(:line_item, :variant => variant_1)
|
||||
line_item_2 = double(:line_item, :variant => variant_2)
|
||||
|
||||
order = double(:order, :line_items => [line_item_1, line_item_2])
|
||||
|
||||
subject.set_preference(:per_kg, 10)
|
||||
subject.compute(order).should == 300
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user