Predict month of harvest on perennials

This commit is contained in:
Brenda Wallace
2019-11-23 22:21:36 +13:00
parent ab1b77ede8
commit 09a70c4742
5 changed files with 62 additions and 20 deletions

View File

@@ -54,6 +54,28 @@ module PredictHarvest
first_harvest_predicted_at > Time.zone.today
end
def harvest_months
Rails.cache.fetch("#{cache_key_with_version}/harvest_months", expires_in: 5.minutes) do
# use this planting's harvest if any, otherwise use nearby plantings_count
harvests_to_predict_from = harvests.size.positive? ? harvests : Harvest.where(planting: nearby_same_crop)
harvests_to_predict_from.where.not(harvested_at: nil)
.group("extract(MONTH from harvested_at)")
.count
end
end
def nearby_same_crop
return unless location
# nearby_members = Member.nearest_to(location)
Planting.joins(:garden)
.where(crop: crop)
.located
.where('gardens.latitude < ? AND gardens.latitude > ?',
latitude + 10, latitude - 10)
.where("plantings.harvests_count> 0")
end
private
def harvests_with_dates

View File

@@ -34,6 +34,11 @@ class Garden < ApplicationRecord
numericality: { only_integer: false, greater_than_or_equal_to: 0 },
allow_nil: true
scope :located, lambda {
where.not(gardens: { location: '' })
.where.not(gardens: { latitude: nil })
.where.not(gardens: { longitude: nil })
}
AREA_UNITS_VALUES = {
"square metres" => "square metre",
"square feet" => "square foot",

View File

@@ -32,6 +32,12 @@ class Planting < ApplicationRecord
##
## Scopes
scope :located, lambda {
joins(:garden)
.where.not(gardens: { location: '' })
.where.not(gardens: { latitude: nil })
.where.not(gardens: { longitude: nil })
}
scope :active, -> { where('finished <> true').where('finished_at IS NULL OR finished_at < ?', Time.zone.now) }
scope :annual, -> { joins(:crop).where(crops: { perennial: false }) }
scope :perennial, -> { joins(:crop).where(crops: { perennial: true }) }
@@ -48,7 +54,7 @@ class Planting < ApplicationRecord
delegate :name, :slug, :en_wikipedia_url, :default_scientific_name, :plantings_count,
to: :crop, prefix: true
delegate :annual?, :svg_icon, to: :crop
delegate :annual?, :perennial?, :svg_icon, to: :crop
delegate :location, :longitude, :latitude, to: :garden
##

View File

@@ -10,9 +10,9 @@
%p
Planted in
= link_to @planting.garden, @planting.garden
- if @planting.owner.location
- if @planting.garden.location
%p
%small
View other plantings, members and more near
= link_to @planting.owner.location, place_path(@planting.owner.location, anchor: "plantings")
= link_to @planting.garden.location, place_path(@planting.garden.location, anchor: "plantings")
.col= render "members/avatar", member: @planting.owner

View File

@@ -1,23 +1,32 @@
- if @planting.crop.annual?
- if planting.annual?
.d-flex.justify-content-between
- if @planting.planted_at.present?
%p.small #{ image_icon 'planting-hand'} Planted #{I18n.l @planting.planted_at}
- if @planting.first_harvest_date.present?
%p.small #{harvest_icon} Harvest started #{I18n.l @planting.first_harvest_date}
- elsif @planting.first_harvest_predicted_at.present?
%p.small #{harvest_icon} First harvest expected #{I18n.l @planting.first_harvest_predicted_at}
- if @planting.finished_at.present?
%p.small #{finished_icon} Finished #{I18n.l @planting.finished_at}
- elsif @planting.finish_predicted_at.present?
%p.small #{finished_icon} Finish expected #{I18n.l @planting.finish_predicted_at}
- if @planting.planted_at.present? && @planting.expected_lifespan.present?
- if planting.planted_at.present?
%p.small #{ image_icon 'planting-hand'} Planted #{I18n.l planting.planted_at}
- if planting.first_harvest_date.present?
%p.small #{harvest_icon} Harvest started #{I18n.l planting.first_harvest_date}
- elsif planting.first_harvest_predicted_at.present?
%p.small #{harvest_icon} First harvest expected #{I18n.l planting.first_harvest_predicted_at}
- if planting.finished_at.present?
%p.small #{finished_icon} Finished #{I18n.l planting.finished_at}
- elsif planting.finish_predicted_at.present?
%p.small #{finished_icon} Finish expected #{I18n.l planting.finish_predicted_at}
- if planting.planted_at.present? && planting.expected_lifespan.present?
.progress
.progress-bar{"aria-valuemax" => "100", "aria-valuemin" => "0", "aria-valuenow" => @planting.percentage_grown, role: "progressbar", style: "width: #{@planting.percentage_grown}%"}
.progress-bar{"aria-valuemax" => "100", "aria-valuemin" => "0", "aria-valuenow" => planting.percentage_grown, role: "progressbar", style: "width: #{planting.percentage_grown}%"}
%ul.list-unstyled.d-flex.justify-content-between
- in_weeks(@planting.expected_lifespan).times do |week_number|
%li{class: @planting.planted_at + week_number.weeks > Time.zone.today ? 'text-muted progress-fade' : '', 'data-toggle': "tooltip", 'data-placement': "top", title: I18n.l(@planting.planted_at + week_number.weeks)}
- in_weeks(planting.expected_lifespan).times do |week_number|
%li{class: planting.planted_at + week_number.weeks > Time.zone.today ? 'text-muted progress-fade' : '', 'data-toggle': "tooltip", 'data-placement': "top", title: I18n.l(planting.planted_at + week_number.weeks)}
= render 'timeline_icon',
planting: @planting,
planting: planting,
week_number: week_number,
date_this_week: @planting.planted_at + week_number.weeks
date_this_week: planting.planted_at + week_number.weeks
(One emojii = 1 week)
- if planting.perennial?
Harvest months:
- (1..12).each do |month|
- if planting.harvest_months.keys().include?(month.to_f)
.badge.badge-info
= I18n.t('date.abbr_month_names')[month]
- else
.badge.text-muted
= I18n.t('date.abbr_month_names')[month]