mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-05 02:41:33 +00:00
Move Calculator::Weight from models/open_food_network/calculator to models/calculator
This commit is contained in:
30
app/models/calculator/weight.rb
Normal file
30
app/models/calculator/weight.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
module Calculator
|
||||
class Weight < Spree::Calculator
|
||||
preference :per_kg, :decimal, default: 0.0
|
||||
attr_accessible :preferred_per_kg
|
||||
|
||||
def self.description
|
||||
I18n.t('spree.weight')
|
||||
end
|
||||
|
||||
def compute(object)
|
||||
line_items = line_items_for object
|
||||
total_weight = line_items.sum { |li| ((li.variant.andand.weight || 0) * li.quantity) }
|
||||
total_weight * preferred_per_kg
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def line_items_for(object)
|
||||
if object.respond_to? :order
|
||||
object.order.line_items
|
||||
elsif object.respond_to? :line_items
|
||||
object.line_items
|
||||
elsif object.respond_to?(:variant) && object.respond_to?(:quantity)
|
||||
[object]
|
||||
else
|
||||
raise "Unknown object type: #{object.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,32 +0,0 @@
|
||||
module OpenFoodNetwork
|
||||
module Calculator
|
||||
class Weight < Spree::Calculator
|
||||
preference :per_kg, :decimal, default: 0.0
|
||||
attr_accessible :preferred_per_kg
|
||||
|
||||
def self.description
|
||||
I18n.t('spree.weight')
|
||||
end
|
||||
|
||||
def compute(object)
|
||||
line_items = line_items_for object
|
||||
total_weight = line_items.sum { |li| ((li.variant.andand.weight || 0) * li.quantity) }
|
||||
total_weight * preferred_per_kg
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def line_items_for(object)
|
||||
if object.respond_to? :order
|
||||
object.order.line_items
|
||||
elsif object.respond_to? :line_items
|
||||
object.line_items
|
||||
elsif object.respond_to?(:variant) && object.respond_to?(:quantity)
|
||||
[object]
|
||||
else
|
||||
raise "Unknown object type: #{object.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -55,7 +55,7 @@ module Openfoodnetwork
|
||||
|
||||
# Register Spree calculators
|
||||
initializer 'spree.register.calculators' do |app|
|
||||
app.config.spree.calculators.shipping_methods << OpenFoodNetwork::Calculator::Weight
|
||||
app.config.spree.calculators.shipping_methods << Calculator::Weight
|
||||
app.config.spree.calculators.add_class('enterprise_fees')
|
||||
config.spree.calculators.enterprise_fees = [
|
||||
Calculator::FlatPercentPerItem,
|
||||
@@ -63,7 +63,7 @@ module Openfoodnetwork
|
||||
Spree::Calculator::FlexiRate,
|
||||
Spree::Calculator::PerItem,
|
||||
Spree::Calculator::PriceSack,
|
||||
OpenFoodNetwork::Calculator::Weight
|
||||
Calculator::Weight
|
||||
]
|
||||
app.config.spree.calculators.add_class('payment_methods')
|
||||
config.spree.calculators.payment_methods = [
|
||||
|
||||
@@ -188,7 +188,7 @@ namespace :openfoodnetwork do
|
||||
name: 'Pickup',
|
||||
zone_id: 3,
|
||||
require_ship_address: true,
|
||||
calculator_type: 'OpenFoodNetwork::Calculator::Weight',
|
||||
calculator_type: 'Calculator::Weight',
|
||||
distributor_ids: [enterprise2.id]
|
||||
)
|
||||
enterprise2.payment_methods << Spree::PaymentMethod.last
|
||||
|
||||
@@ -275,7 +275,7 @@ FactoryBot.define do
|
||||
enterprise_role 'distributor'
|
||||
end
|
||||
|
||||
factory :weight_calculator, :class => OpenFoodNetwork::Calculator::Weight do
|
||||
factory :weight_calculator, :class => Calculator::Weight do
|
||||
after(:build) { |c| c.set_preference(:per_kg, 0.5) }
|
||||
after(:create) { |c| c.set_preference(:per_kg, 0.5); c.save! }
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe OpenFoodNetwork::Calculator::Weight do
|
||||
describe Calculator::Weight do
|
||||
it "computes shipping cost for an order by total weight" do
|
||||
variant1 = double(:variant, weight: 10)
|
||||
variant2 = double(:variant, weight: 20)
|
||||
|
||||
Reference in New Issue
Block a user