mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-18 13:38:24 -04:00
Merge pull request #1178 from Br3nda/bw/plantingshelper-simplify
PlantingsHelper simplification
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
module PlantingsHelper
|
||||
def display_days_before_maturity(planting)
|
||||
if planting.finished?
|
||||
"0"
|
||||
elsif !planting.finished_at.nil?
|
||||
((p = planting.finished_at - Date.current).to_i) <= 0 ? "0" : p.to_i.to_s
|
||||
elsif planting.planted_at.nil? || planting.days_before_maturity.nil?
|
||||
"unknown"
|
||||
# First try to calc from finished/finished_at
|
||||
if planting.finished? || planting.finished_at.present?
|
||||
planting.days_until_finished.to_s
|
||||
# then try to calc from planted at + maturity
|
||||
elsif planting.planted_at.present? && planting.days_before_maturity.present?
|
||||
planting.days_until_mature.to_s
|
||||
else
|
||||
((p = (planting.planted_at + planting.days_before_maturity) - Date.current).to_i <= 0) ? "0" : p.to_i.to_s
|
||||
"unknown"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -112,6 +112,17 @@ class Planting < ActiveRecord::Base
|
||||
planted_at.present? && current_date.to_date >= planted_at
|
||||
end
|
||||
|
||||
def days_until_finished
|
||||
return 0 if finished?
|
||||
days = (finished_at - Date.current).to_i
|
||||
days.positive? ? days : 0
|
||||
end
|
||||
|
||||
def days_until_mature
|
||||
days = ((planted_at + days_before_maturity) - Date.current).to_i
|
||||
days.positive? ? days : 0
|
||||
end
|
||||
|
||||
def percentage_grown(current_date = Date.current)
|
||||
return nil unless days_before_maturity && planted?(current_date)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user