From 3577f3790df980eea1ff91f4b22093a71da3e7e2 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 10 Sep 2018 16:50:34 +0100 Subject: [PATCH] Moved order.shipping_method to the OrderShippingMethod concern --- app/models/concerns/order_shipping_method.rb | 21 ++++++++++++++++++ app/models/spree/order_decorator.rb | 10 +++------ .../concerns/order_shipping_method_spec.rb | 22 +++++++++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 app/models/concerns/order_shipping_method.rb create mode 100644 spec/models/concerns/order_shipping_method_spec.rb diff --git a/app/models/concerns/order_shipping_method.rb b/app/models/concerns/order_shipping_method.rb new file mode 100644 index 0000000000..75e729f24e --- /dev/null +++ b/app/models/concerns/order_shipping_method.rb @@ -0,0 +1,21 @@ +require 'active_support/concern' + +# This module is an adapter for OFN to work with Spree 2 code. +# +# Although Spree 2 supports multiple shipments per order, in OFN we have only one shipment per order. +# A shipment is associated to a shipping_method through a selected shipping_rate. +# See https://github.com/openfoodfoundation/openfoodnetwork/wiki/Spree-Upgrade:-Migration-to-multiple-shipments +# for details. +# +# Methods in this module may become deprecated. +module OrderShippingMethod + extend ActiveSupport::Concern + + # Returns the shipping method of the first and only shipment in the order + # + # @return [ShippingMethod] + def shipping_method + return if shipments.empty? + shipments.first.shipping_method + end +end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 779162b9b2..8a88078b05 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -2,12 +2,15 @@ require 'open_food_network/enterprise_fee_calculator' require 'open_food_network/distribution_change_validator' require 'open_food_network/feature_toggle' require 'open_food_network/tag_rule_applicator' +require 'concerns/order_shipping_method' ActiveSupport::Notifications.subscribe('spree.order.contents_changed') do |name, start, finish, id, payload| payload[:order].reload.update_distribution_charge! end Spree::Order.class_eval do + include OrderShippingMethod + belongs_to :order_cycle belongs_to :distributor, class_name: 'Enterprise' belongs_to :customer @@ -333,13 +336,6 @@ Spree::Order.class_eval do payments.select {|p| p.state == "checkout"} # Original definition end - # Although Spree 2 supports multi shipments per order, in OFN we keep the rule one shipment per order - # Thus, this method returns the shipping method of the first and only shipment in the order - def shipping_method - return if shipments.empty? - shipments.first.shipping_method - end - private def address_from_distributor diff --git a/spec/models/concerns/order_shipping_method_spec.rb b/spec/models/concerns/order_shipping_method_spec.rb new file mode 100644 index 0000000000..86c178e115 --- /dev/null +++ b/spec/models/concerns/order_shipping_method_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe OrderShippingMethod do + let(:order) { create(:order) } + + describe '#shipping_method' do + context 'when order has no shipments' do + it 'returns nil' do + expect(order.shipping_method).to be_nil + end + end + + context 'when order has single shipment' do + it 'returns the shipments shipping_method' do + shipment = create(:shipment_with_flat_rate) + order.shipments = [shipment] + + expect(order.shipping_method).to eq shipment.shipping_method + end + end + end +end