Skip to content

Commit

Permalink
Fix bug on calculator_decorator line_items_for where input is line_it…
Browse files Browse the repository at this point in the history
…em with an nil order.

Adapted line_items_for so that weight_calculator.line_items_for could be removed.
  • Loading branch information
luisramos0 committed Nov 17, 2018
1 parent 5ecea06 commit b71b505
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 20 deletions.
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)
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
Expand Up @@ -5,12 +5,14 @@ module Spree
# 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.respond_to?(:variant) && object.respond_to?(:quantity)
[object]
elsif object.respond_to? :line_items
object.line_items
elsif object.respond_to? :order
elsif object.order.present?
object.order.line_items
else
[object]
raise "Could not fetch line_items for #{object.inspect}"
end
end
end
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) }
let(:line_item) { build(:line_item) }

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

0 comments on commit b71b505

Please sign in to comment.