mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Moved order.shipping_method to the OrderShippingMethod concern
This commit is contained in:
21
app/models/concerns/order_shipping_method.rb
Normal file
21
app/models/concerns/order_shipping_method.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
22
spec/models/concerns/order_shipping_method_spec.rb
Normal file
22
spec/models/concerns/order_shipping_method_spec.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user