From b0a5223d971376dab8d11ca37b0effd814e06ac8 Mon Sep 17 00:00:00 2001 From: Martina Simicic <922964+simicic@users.noreply.github.com> Date: Mon, 3 Feb 2020 20:00:52 +0100 Subject: [PATCH] Account for finished planting when counting days of life --- app/models/concerns/predict_planting.rb | 5 ++++- spec/models/planting_spec.rb | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/models/concerns/predict_planting.rb b/app/models/concerns/predict_planting.rb index 9f1aee587..da8b0e230 100644 --- a/app/models/concerns/predict_planting.rb +++ b/app/models/concerns/predict_planting.rb @@ -40,7 +40,10 @@ module PredictPlanting end def age_in_days - (Time.zone.today - planted_at).to_i if planted_at.present? + return if planted_at.blank? + + known_last_day ||= finished_at || Time.zone.today + (known_last_day - planted_at).to_i end def percentage_grown diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 37016b497..3b0bffd56 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -38,11 +38,11 @@ describe Planting do end describe 'planting all finished' do - let(:planting) { FactoryBot.create :planting, planted_at: 30.days.ago, finished_at: 1.day.ago, finished: true } + let(:planting) { FactoryBot.create :planting, planted_at: 30.days.ago, finished_at: 10.days.ago, finished: true } it { expect(planting.crop.median_lifespan).to eq(nil) } - it { expect(planting.expected_lifespan).to eq(29) } - it { expect(planting.age_in_days).to eq(30) } + it { expect(planting.expected_lifespan).to eq(20) } + it { expect(planting.age_in_days).to eq(20) } it { expect(planting.percentage_grown).to eq(100) } end end @@ -99,7 +99,7 @@ describe Planting do describe 'planted 30 days ago, finished 10 days ago' do let(:planting) { FactoryBot.create :planting, planted_at: 30.days.ago, finished_at: 10.days.ago } - it { expect(planting.age_in_days).to eq 30 } + it { expect(planting.age_in_days).to eq 20 } it { expect(planting.percentage_grown).to eq 100 } end end