Fix calculate_price when order_item without product is provided
This commit is contained in:
parent
0581a7238d
commit
e9aa52736a
|
@ -36,7 +36,7 @@ class Order < ActiveRecord::Base
|
||||||
private
|
private
|
||||||
|
|
||||||
def calculate_price
|
def calculate_price
|
||||||
self.price_cents = self.order_items.map{ |oi| oi.count * oi.product.price_cents }.sum
|
self.price_cents = self.order_items.map{ |oi| oi.count * (oi.product.try(:price_cents) || 0) }.sum
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_api_job
|
def create_api_job
|
||||||
|
|
|
@ -48,7 +48,7 @@ describe Order do
|
||||||
|
|
||||||
describe 'order_items' do
|
describe 'order_items' do
|
||||||
it 'should be validated' do
|
it 'should be validated' do
|
||||||
@order.order_items << OrderItem.new(count: -5)
|
@order.order_items.build(count: -5)
|
||||||
expect(@order).to_not be_valid
|
expect(@order).to_not be_valid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue