mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
Add itemwise shipping cost calculator
This commit is contained in:
13
app/models/open_food_web/calculator/itemwise.rb
Normal file
13
app/models/open_food_web/calculator/itemwise.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module OpenFoodWeb
|
||||
class Calculator::Itemwise < Spree::Calculator
|
||||
|
||||
def self.description
|
||||
"Itemwise Shipping"
|
||||
end
|
||||
|
||||
def compute(object)
|
||||
# Given an order, sum the shipping on each individual item
|
||||
object.line_items.map { |li| li.itemwise_shipping_cost }.inject(:+)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -24,6 +24,12 @@ module Openfoodweb
|
||||
end
|
||||
end
|
||||
|
||||
# Register Spree calculators
|
||||
initializer "spree.register.calculators" do |app|
|
||||
app.config.spree.calculators.shipping_methods << OpenFoodWeb::Calculator::Itemwise
|
||||
end
|
||||
|
||||
|
||||
# Settings in config/environments/* take precedence over those specified here.
|
||||
# Application configuration should go into files in config/initializers
|
||||
# -- all .rb files in that directory are automatically loaded.
|
||||
|
||||
13
spec/models/calculator/itemwise_spec.rb
Normal file
13
spec/models/calculator/itemwise_spec.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe OpenFoodWeb::Calculator::Itemwise do
|
||||
it "computes the shipping cost for each line item in an order" do
|
||||
line_item = double(:line_item)
|
||||
line_item.should_receive(:itemwise_shipping_cost).exactly(3).times.and_return(10)
|
||||
|
||||
order = double(:order)
|
||||
order.stub(:line_items).and_return([line_item]*3)
|
||||
|
||||
subject.compute(order).should == 30
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user