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

Fix bug on calculator_decorator line_items_for where input is line_item with a nil order #3072

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 0 additions & 14 deletions app/models/calculator/weight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,5 @@ def compute(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)
luisramos0 marked this conversation as resolved.
Show resolved Hide resolved
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
8 changes: 5 additions & 3 deletions app/models/spree/calculator_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
module Spree
Calculator.class_eval do


private

# Given an object which might be an Order or a LineItem (amongst
# others), return a collection of line items.
def line_items_for(object)
if object.respond_to? :line_items
if object.is_a?(Spree::LineItem)
[object]
elsif object.respond_to? :line_items
object.line_items
elsif object.order.present?
object.order.line_items
else
[object]
end
end

end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'spec_helper'
require 'open_food_network/enterprise_fee_calculator'

module OpenFoodNetwork
Expand Down
24 changes: 12 additions & 12 deletions spec/models/calculator/weight_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

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)
variant3 = double(:variant, weight: nil)
variant1 = build(:variant, weight: 10)
variant2 = build(:variant, weight: 20)
variant3 = build(:variant, weight: nil)

line_item1 = double(:line_item, variant: variant1, quantity: 1)
line_item2 = double(:line_item, variant: variant2, quantity: 3)
line_item3 = double(:line_item, variant: variant3, quantity: 5)
line_item1 = build(:line_item, variant: variant1, quantity: 1)
line_item2 = build(:line_item, variant: variant2, quantity: 3)
line_item3 = build(:line_item, variant: variant3, quantity: 5)
luisramos0 marked this conversation as resolved.
Show resolved Hide resolved

order = double(:order, line_items: [line_item1, line_item2, line_item3])

Expand All @@ -17,20 +17,20 @@
end

it "computes shipping cost for a line item" do
variant = double(:variant, weight: 10)
variant = build(:variant, weight: 10)

line_item = double(:line_item, variant: variant, quantity: 2)
line_item = build(:line_item, variant: variant, quantity: 2)

subject.set_preference(:per_kg, 10)
expect(subject.compute(line_item)).to eq(10 * 2 * 10)
end

it "computes shipping cost for an object with an order" do
variant1 = double(:variant, weight: 10)
variant2 = double(:variant, weight: 5)
variant1 = build(:variant, weight: 10)
variant2 = build(:variant, weight: 5)

line_item1 = double(:line_item, variant: variant1, quantity: 1)
line_item2 = double(:line_item, variant: variant2, quantity: 2)
line_item1 = build(:line_item, variant: variant1, quantity: 1)
line_item2 = build(:line_item, variant: variant2, quantity: 2)

order = double(:order, line_items: [line_item1, line_item2])
object_with_order = double(:object_with_order, order: order)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Spree::Calculator::FlatPercentItemTotal do
let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new }
let(:line_item) { instance_double(Spree::LineItem, amount: 10) }
luisramos0 marked this conversation as resolved.
Show resolved Hide resolved
let(:line_item) { build(:line_item, price: 10, quantity: 1) }

before { calculator.stub :preferred_flat_percent => 10 }

Expand Down
2 changes: 1 addition & 1 deletion spec/models/spree/calculator/flexi_rate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe Spree::Calculator::FlexiRate do
let(:line_item) { instance_double(Spree::LineItem, amount: 10, quantity: quantity) }
let(:line_item) { build(:line_item, quantity: quantity) }
let(:calculator) do
Spree::Calculator::FlexiRate.new(
preferred_first_item: 2,
Expand Down
2 changes: 1 addition & 1 deletion spec/models/spree/calculator/per_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Spree::Calculator::PerItem do
let(:calculator) { Spree::Calculator::PerItem.new(preferred_amount: 10) }
let(:shipping_calculable) { double(:calculable) }
let(:line_item) { double(:line_item, quantity: 5, product: double(:product)) }
let(:line_item) { build(:line_item, quantity: 5) }

it "correctly calculates on a single line item object" do
calculator.stub(calculable: shipping_calculable)
Expand Down