Reducing complexity in planting predictions code

This commit is contained in:
Brenda Wallace
2018-04-21 15:32:04 +12:00
parent 4ad4dd0c7f
commit e103e6be35

View File

@@ -32,12 +32,8 @@ module PredictPlanting
def percentage_grown
return 100 if finished?
return unless finish_is_predicatable?
if growing?
percent = (days_since_planted / expected_lifespan.to_f) * 100
return 100 if percent > 100
return percent
end
return 0 if planted?
return calculate_percentage_grown if growing?
0 if planted?
end
# states
@@ -56,5 +52,12 @@ module PredictPlanting
finish_predicted_at.present? &&
finish_predicted_at <= Time.zone.today
end
private
def calculate_percentage_grown
percent = (days_since_planted / expected_lifespan.to_f) * 100
(percent > 100 ? 100 : percent)
end
end
end