From 9490da329a1af5901f821087b6e06cb6340e78b2 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 25 Oct 2018 20:07:34 +0100 Subject: [PATCH] Move Calculator::Weight from models/open_food_network/calculator to models/calculator --- app/models/calculator/weight.rb | 30 +++++++++++++++++ .../open_food_network/calculator/weight.rb | 32 ------------------- config/application.rb | 4 +-- lib/tasks/dev.rake | 2 +- spec/factories.rb | 2 +- spec/models/calculator/weight_spec.rb | 2 +- 6 files changed, 35 insertions(+), 37 deletions(-) create mode 100644 app/models/calculator/weight.rb delete mode 100644 app/models/open_food_network/calculator/weight.rb diff --git a/app/models/calculator/weight.rb b/app/models/calculator/weight.rb new file mode 100644 index 0000000000..ae65026a46 --- /dev/null +++ b/app/models/calculator/weight.rb @@ -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 diff --git a/app/models/open_food_network/calculator/weight.rb b/app/models/open_food_network/calculator/weight.rb deleted file mode 100644 index 64f1ee9999..0000000000 --- a/app/models/open_food_network/calculator/weight.rb +++ /dev/null @@ -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 diff --git a/config/application.rb b/config/application.rb index a872fdfe40..14c0b82b86 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 = [ diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 6fa9af6f3e..5f7f09481f 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -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 diff --git a/spec/factories.rb b/spec/factories.rb index c440b54048..80bbc6182e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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 diff --git a/spec/models/calculator/weight_spec.rb b/spec/models/calculator/weight_spec.rb index eec06a5c46..dee2f3d067 100644 --- a/spec/models/calculator/weight_spec.rb +++ b/spec/models/calculator/weight_spec.rb @@ -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)