Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spree 2 Upgrade] - Added adapted order.shipping_method #2654

Merged
merged 4 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/models/concerns/order_shipping_method.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddUniquenessOfOrderIdToSpreeShipments < ActiveRecord::Migration
def change
remove_index :spree_shipments, :order_id
add_index :spree_shipments, :order_id, unique: true
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20180812214434) do
ActiveRecord::Schema.define(:version => 20180910155506) do

create_table "account_invoices", :force => true do |t|
t.integer "user_id", :null => false
Expand Down Expand Up @@ -876,7 +876,7 @@
end

add_index "spree_shipments", ["number"], :name => "index_shipments_on_number"
add_index "spree_shipments", ["order_id"], :name => "index_spree_shipments_on_order_id"
add_index "spree_shipments", ["order_id"], :name => "index_spree_shipments_on_order_id", :unique => true

create_table "spree_shipping_categories", :force => true do |t|
t.string "name"
Expand Down
10 changes: 0 additions & 10 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,6 @@
after(:create) { |c| c.set_preference(:per_kg, 0.5); c.save! }
end

factory :shipping_method_with_flat_rate, parent: :shipping_method do
calculator { Spree::Calculator::FlatRate.new(preferred_amount: 50.0) }
end

factory :shipment_with_flat_rate, parent: :shipment do
after(:create) do |shipment|
shipment.add_shipping_method(create(:shipping_method_with_flat_rate), true)
end
end

factory :order_with_totals_and_distribution, :parent => :order do #possibly called :order_with_line_items in newer Spree
distributor { create(:distributor_enterprise) }
order_cycle { create(:simple_order_cycle) }
Expand Down
22 changes: 22 additions & 0 deletions spec/models/concerns/order_shipping_method_spec.rb
Original file line number Diff line number Diff line change
@@ -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