From 9cc31e125e1d2327636aa88f11dea607fae982b6 Mon Sep 17 00:00:00 2001 From: puppe1990 Date: Sat, 19 Oct 2024 16:19:58 -0300 Subject: [PATCH] fix error --- app/models/production.rb | 4 +++- spec/models/production_spec.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/models/production.rb b/app/models/production.rb index e62dc22d..86613888 100644 --- a/app/models/production.rb +++ b/app/models/production.rb @@ -126,6 +126,8 @@ def calendar_date end def total_value_delivered - production_products.sum { |pp| pp.pieces_delivered * pp.unit_price } + production_products.sum do |pp| + (pp.pieces_delivered || 0) * (pp.unit_price || 0) + end end end diff --git a/spec/models/production_spec.rb b/spec/models/production_spec.rb index ef09f590..270c99da 100644 --- a/spec/models/production_spec.rb +++ b/spec/models/production_spec.rb @@ -216,6 +216,20 @@ expected_total = production.production_products.first.pieces_delivered * production.production_products.first.unit_price expect(production.total_value_delivered).to be_within(0.01).of(expected_total) end + + it 'handles nil unit_price correctly' do + production.production_products.first.update( + pieces_delivered: 5, + unit_price: nil + ) + production.production_products.last.update( + pieces_delivered: 3, + unit_price: 10.0 + ) + + expected_total = 0 + (3 * 10.0) + expect(production.total_value_delivered).to be_within(0.01).of(expected_total) + end end end end