Account for finished planting when counting days of life

This commit is contained in:
Martina Simicic
2020-02-03 20:00:52 +01:00
committed by Brenda Wallace
parent 10008ca30d
commit b0a5223d97
2 changed files with 8 additions and 5 deletions

View File

@@ -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

View File

@@ -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