From 09a70c4742dec49b8382b1a542900a236f119cdb Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 23 Nov 2019 22:21:36 +1300 Subject: [PATCH 01/22] Predict month of harvest on perennials --- app/models/concerns/predict_harvest.rb | 22 +++++++++++++ app/models/garden.rb | 5 +++ app/models/planting.rb | 8 ++++- app/views/plantings/_owner.haml | 4 +-- app/views/plantings/_timeline.html.haml | 43 +++++++++++++++---------- 5 files changed, 62 insertions(+), 20 deletions(-) diff --git a/app/models/concerns/predict_harvest.rb b/app/models/concerns/predict_harvest.rb index bc377f369..b5f3e7bef 100644 --- a/app/models/concerns/predict_harvest.rb +++ b/app/models/concerns/predict_harvest.rb @@ -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 diff --git a/app/models/garden.rb b/app/models/garden.rb index b30418486..7aee416fc 100644 --- a/app/models/garden.rb +++ b/app/models/garden.rb @@ -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", diff --git a/app/models/planting.rb b/app/models/planting.rb index 686153b64..34f82daa6 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -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 ## diff --git a/app/views/plantings/_owner.haml b/app/views/plantings/_owner.haml index 89bfcae4d..3b0b931d8 100644 --- a/app/views/plantings/_owner.haml +++ b/app/views/plantings/_owner.haml @@ -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 \ No newline at end of file diff --git a/app/views/plantings/_timeline.html.haml b/app/views/plantings/_timeline.html.haml index 925456c9b..b64cd5831 100644 --- a/app/views/plantings/_timeline.html.haml +++ b/app/views/plantings/_timeline.html.haml @@ -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] From 5793f26b6f0addfa1f0f433bac172d3075168eb9 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 08:41:12 +1300 Subject: [PATCH 02/22] stub tests --- spec/models/planting_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 3bbd0a255..d9c03aff9 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -207,6 +207,11 @@ describe Planting do end end + describe 'planting perrennial' do + pending 'no harvest to predict from' + pending 'harvests used to predict' + pending 'nearby plantings used to predict' + end it 'has an owner' do planting.owner.should be_an_instance_of Member end From 70565c7202038b39280f841a25db5c6513083b10 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 09:43:25 +1300 Subject: [PATCH 03/22] show neighbouring plantings same crop also helps see where the prediction came from --- app/models/concerns/predict_harvest.rb | 14 +------------- app/models/planting.rb | 11 +++++++++++ app/views/plantings/_timeline.html.haml | 19 ++++++++++++------- app/views/plantings/show.html.haml | 7 +++++++ 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/app/models/concerns/predict_harvest.rb b/app/models/concerns/predict_harvest.rb index b5f3e7bef..76a6c179a 100644 --- a/app/models/concerns/predict_harvest.rb +++ b/app/models/concerns/predict_harvest.rb @@ -57,25 +57,13 @@ module PredictHarvest 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 = harvests.size.positive? ? harvests : Harvest.where.not(planting_id: nil).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 diff --git a/app/models/planting.rb b/app/models/planting.rb index 34f82daa6..ff21f295a 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -102,6 +102,17 @@ class Planting < ApplicationRecord planted? && !finished? end + def nearby_same_crop + return if location.empty? + # latitude, longitude = Geocoder.coordinates(location, params: { limit: 1 }) + Planting.joins(:garden) + .where(crop: crop) + .located + .where('gardens.latitude < ? AND gardens.latitude > ?', + latitude + 10, latitude - 10) + .where("plantings.harvests_count> 0") + end + private # check that any finished_at date occurs after planted_at diff --git a/app/views/plantings/_timeline.html.haml b/app/views/plantings/_timeline.html.haml index b64cd5831..05aef5c5d 100644 --- a/app/views/plantings/_timeline.html.haml +++ b/app/views/plantings/_timeline.html.haml @@ -23,10 +23,15 @@ (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] + - if planting.harvest_months.empty? + %span.text-muted + We need more data on this crop in your latitude. There's not enough + info yet to predict harvests. + - else + - (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] diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 0ff106ccc..8edddbe82 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -56,6 +56,13 @@ = render 'plantings/actions', planting: @planting %hr/ = render @planting.crop + - unless @planting.nearby_same_crop.nil? + %section.neighbours + %h2 Neighbours + %p Other #{@planting.crop.name} plantings at the same latitude + %ul + - @planting.nearby_same_crop.each do |neighbour| + %li= link_to neighbour, neighbour .row .col-md-6 %section.harvests From fd744cf67ac531b854b8a094bd238b7e88f8e3df Mon Sep 17 00:00:00 2001 From: codefactor-io Date: Sat, 23 Nov 2019 20:44:19 +0000 Subject: [PATCH 04/22] [CodeFactor] Apply fixes --- app/models/planting.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/planting.rb b/app/models/planting.rb index ff21f295a..a043c7eb7 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -104,6 +104,7 @@ class Planting < ApplicationRecord def nearby_same_crop return if location.empty? + # latitude, longitude = Geocoder.coordinates(location, params: { limit: 1 }) Planting.joins(:garden) .where(crop: crop) From e40cca806c3489b350650a928f3fa8bb87d279a4 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 09:44:35 +1300 Subject: [PATCH 05/22] World neighbours --- app/views/plantings/show.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 8edddbe82..10cfe9086 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -58,7 +58,7 @@ = render @planting.crop - unless @planting.nearby_same_crop.nil? %section.neighbours - %h2 Neighbours + %h2 World Neighbours %p Other #{@planting.crop.name} plantings at the same latitude %ul - @planting.nearby_same_crop.each do |neighbour| From 0bb4dbec10c16d32149417b9565a30569fdb79b6 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 10:15:11 +1300 Subject: [PATCH 06/22] Display of planting neighbours --- app/controllers/plantings_controller.rb | 3 +++ app/models/concerns/predict_harvest.rb | 14 +++++++++++--- app/models/planting.rb | 3 ++- app/views/plantings/show.html.haml | 18 ++++++++++++++---- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/controllers/plantings_controller.rb b/app/controllers/plantings_controller.rb index d96c7f266..f36a94e39 100644 --- a/app/controllers/plantings_controller.rb +++ b/app/controllers/plantings_controller.rb @@ -33,6 +33,9 @@ class PlantingsController < ApplicationController def show @photos = @planting.photos.includes(:owner).order(date_taken: :desc) @matching_seeds = matching_seeds + @neighbours = @planting.nearby_same_crop + .where.not(id: @planting.id) + .limit(6) respond_with @planting end diff --git a/app/models/concerns/predict_harvest.rb b/app/models/concerns/predict_harvest.rb index 76a6c179a..f5dbb1a7e 100644 --- a/app/models/concerns/predict_harvest.rb +++ b/app/models/concerns/predict_harvest.rb @@ -56,14 +56,22 @@ module PredictHarvest 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.not(planting_id: nil).where(planting: nearby_same_crop) - harvests_to_predict_from.where.not(harvested_at: nil) + neighbours_for_harvest_predictions.where.not(harvested_at: nil) .group("extract(MONTH from harvested_at)") .count end end + def neighbours_for_harvest_predictions + # use this planting's harvest if any + return harvests if harvests.size.positive? + + # otherwise use nearby plantings + return Harvest.where(planting: nearby_same_crop.has_harvests) + .where.not(planting_id: nil) if location + [] + end + private def harvests_with_dates diff --git a/app/models/planting.rb b/app/models/planting.rb index ff21f295a..bb29758ce 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -43,6 +43,7 @@ class Planting < ApplicationRecord scope :perennial, -> { joins(:crop).where(crops: { perennial: true }) } scope :interesting, -> { has_photos.one_per_owner.order(planted_at: :desc) } scope :recent, -> { order(created_at: :desc) } + scope :has_harvests, -> { where('plantings.harvests_count > 0') } scope :one_per_owner, lambda { joins("JOIN members m ON (m.id=plantings.owner_id) LEFT OUTER JOIN plantings p2 @@ -104,13 +105,13 @@ class Planting < ApplicationRecord def nearby_same_crop return if location.empty? + # latitude, longitude = Geocoder.coordinates(location, params: { limit: 1 }) Planting.joins(:garden) .where(crop: crop) .located .where('gardens.latitude < ? AND gardens.latitude > ?', latitude + 10, latitude - 10) - .where("plantings.harvests_count> 0") end private diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 10cfe9086..add37d4f5 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -56,13 +56,23 @@ = render 'plantings/actions', planting: @planting %hr/ = render @planting.crop - - unless @planting.nearby_same_crop.nil? + + - if @planting.location %section.neighbours %h2 World Neighbours + - @neighbours.each do |planting| + = link_to planting, class: 'list-group-item list-group-item-action flex-column align-items-start' do + .d-flex.w-100.justify-content-between + %p.mb-2 + = image_tag planting_image_path(planting), width: 75, class: 'rounded shadow' + .text-right + %h5= planting.crop.name + - if planting.planted_from.present? + %span.badge.badge-success= planting.planted_from.pluralize + %small.text-muted + planted by #{planting.owner} + in #{planting.garden.location} %p Other #{@planting.crop.name} plantings at the same latitude - %ul - - @planting.nearby_same_crop.each do |neighbour| - %li= link_to neighbour, neighbour .row .col-md-6 %section.harvests From a8a522d09ea60ef905004062e2bdd6255077716f Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 10:17:55 +1300 Subject: [PATCH 07/22] rubocop style fix --- app/models/concerns/predict_harvest.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/predict_harvest.rb b/app/models/concerns/predict_harvest.rb index f5dbb1a7e..63b9885e3 100644 --- a/app/models/concerns/predict_harvest.rb +++ b/app/models/concerns/predict_harvest.rb @@ -67,8 +67,11 @@ module PredictHarvest return harvests if harvests.size.positive? # otherwise use nearby plantings - return Harvest.where(planting: nearby_same_crop.has_harvests) - .where.not(planting_id: nil) if location + if location + return Harvest.where(planting: nearby_same_crop.has_harvests) + .where.not(planting_id: nil) + end + [] end From 554adf234ea65e282ea3da3c8dd897f2e6147a1f Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 10:30:56 +1300 Subject: [PATCH 08/22] Allow chaining on Harvest predictions method returns --- app/models/concerns/predict_harvest.rb | 2 +- app/models/planting.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/predict_harvest.rb b/app/models/concerns/predict_harvest.rb index 63b9885e3..a5a0956a1 100644 --- a/app/models/concerns/predict_harvest.rb +++ b/app/models/concerns/predict_harvest.rb @@ -72,7 +72,7 @@ module PredictHarvest .where.not(planting_id: nil) end - [] + Harvest.none end private diff --git a/app/models/planting.rb b/app/models/planting.rb index bb29758ce..5539b6268 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -104,7 +104,7 @@ class Planting < ApplicationRecord end def nearby_same_crop - return if location.empty? + return Planting.none if location.blank? # latitude, longitude = Geocoder.coordinates(location, params: { limit: 1 }) Planting.joins(:garden) From 5b7de1ae7a60984dc1d3213da1b13af4224dcdf0 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 10:39:06 +1300 Subject: [PATCH 09/22] Added location --- app/views/plantings/_timeline.html.haml | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/plantings/_timeline.html.haml b/app/views/plantings/_timeline.html.haml index 05aef5c5d..eb4853212 100644 --- a/app/views/plantings/_timeline.html.haml +++ b/app/views/plantings/_timeline.html.haml @@ -35,3 +35,4 @@ - else .badge.text-muted = I18n.t('date.abbr_month_names')[month] + in #{link_to planting.garden.location, place_path(planting.garden.location)} From 83f018255188f943e838cabf7061b118a918afac Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 10:41:20 +1300 Subject: [PATCH 10/22] rubocop lint --- app/models/concerns/predict_harvest.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/predict_harvest.rb b/app/models/concerns/predict_harvest.rb index a5a0956a1..a552735b3 100644 --- a/app/models/concerns/predict_harvest.rb +++ b/app/models/concerns/predict_harvest.rb @@ -69,7 +69,7 @@ module PredictHarvest # otherwise use nearby plantings if location return Harvest.where(planting: nearby_same_crop.has_harvests) - .where.not(planting_id: nil) + .where.not(planting_id: nil) end Harvest.none From 6325df25b87e00a5d2ebdab8dde94f10d17cc975 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 11:36:55 +1300 Subject: [PATCH 11/22] Make the month an int --- app/models/concerns/predict_harvest.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/predict_harvest.rb b/app/models/concerns/predict_harvest.rb index a552735b3..51224a2a6 100644 --- a/app/models/concerns/predict_harvest.rb +++ b/app/models/concerns/predict_harvest.rb @@ -57,7 +57,7 @@ module PredictHarvest def harvest_months Rails.cache.fetch("#{cache_key_with_version}/harvest_months", expires_in: 5.minutes) do neighbours_for_harvest_predictions.where.not(harvested_at: nil) - .group("extract(MONTH from harvested_at)") + .group("extract(MONTH from harvested_at)::int") .count end end From 9aae07b8dff730926cdd9ee025d0bb521272998f Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 11:37:07 +1300 Subject: [PATCH 12/22] Screen reader niceness --- app/views/plantings/_timeline.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/plantings/_timeline.html.haml b/app/views/plantings/_timeline.html.haml index eb4853212..8ff8be310 100644 --- a/app/views/plantings/_timeline.html.haml +++ b/app/views/plantings/_timeline.html.haml @@ -33,6 +33,6 @@ .badge.badge-info = I18n.t('date.abbr_month_names')[month] - else - .badge.text-muted + .badge.text-muted{'aria-hidden': "true"} = I18n.t('date.abbr_month_names')[month] in #{link_to planting.garden.location, place_path(planting.garden.location)} From fd1a608e4c65aa5f167a4a5736c64588b25df98b Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 11:37:18 +1300 Subject: [PATCH 13/22] Elaborated comment --- spec/factories/member.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/member.rb b/spec/factories/member.rb index 37ed2c185..87b5326f3 100644 --- a/spec/factories/member.rb +++ b/spec/factories/member.rb @@ -39,7 +39,7 @@ FactoryBot.define do factory :london_member do sequence(:login_name) { |n| "JohnH#{n}" } # for the astronomer who figured out longitude location { 'Greenwich, UK' } - # including lat/long explicitly because geocoder doesn't work with FG + # including lat/long explicitly because geocoder doesn't work with FactoryBot latitude { 51.483 } longitude { 0.004 } end From 2538b712258d1e966e68f7d8174f7b342a0ab7bc Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 11:37:33 +1300 Subject: [PATCH 14/22] Planting model spec for perenial harvests --- spec/models/planting_spec.rb | 40 ++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index d9c03aff9..f32d81f8a 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -207,10 +207,42 @@ describe Planting do end end - describe 'planting perrennial' do - pending 'no harvest to predict from' - pending 'harvests used to predict' - pending 'nearby plantings used to predict' + describe 'planting perennial' do + let(:crop) { FactoryBot.create(:crop, name: 'feijoa', perennial: true) } + it { expect(planting.perennial?).to eq true } + describe 'no harvest to predict from' do + it { expect(planting.harvest_months).to eq({}) } + end + + describe 'harvests used to predict' do + before do + FactoryBot.create :harvest, planting: planting, crop: crop, harvested_at: '1 May 2019' + FactoryBot.create :harvest, planting: planting, crop: crop, harvested_at: '18 June 2019' + FactoryBot.create_list :harvest, 4, planting: planting, crop: crop, harvested_at: '18 August 2019' + end + it { expect(planting.harvest_months).to eq(5 => 1, 6 => 1, 8 => 4) } + end + + describe 'nearby plantings used to predict' do + # Note the locations used need to be stubbed in geocoder + let(:garden) { FactoryBot.create :garden, location: 'Edinburgh', owner: garden_owner } + + before do + # Near by planting with harvests + nearby_garden = FactoryBot.create :garden, location: 'Greenwich, UK' + nearby_planting = FactoryBot.create :planting, crop: crop, garden: nearby_garden, owner: nearby_garden.owner, planted_at: '1 January 2000' + FactoryBot.create :harvest, planting: nearby_planting, crop: crop, harvested_at: '1 May 2019' + FactoryBot.create :harvest, planting: nearby_planting, crop: crop, harvested_at: '18 June 2019' + FactoryBot.create_list :harvest, 4, planting: nearby_planting, crop: crop, harvested_at: '18 August 2008' + + # far away planting harvests + faraway_garden = FactoryBot.create :garden, location: 'Amundsen-Scott Base, Antarctica' + faraway_planting = FactoryBot.create :planting, garden: faraway_garden, crop: crop, owner: faraway_garden.owner, planted_at: '16 May 2001' + + FactoryBot.create_list :harvest, 4, planting: faraway_planting, crop: crop, harvested_at: '18 December 2006' + end + it { expect(planting.harvest_months).to eq(5 => 1, 6 => 1, 8 => 4) } + end end it 'has an owner' do planting.owner.should be_an_instance_of Member From bcbee5831fa6265e777c55d88bb6e1cc082164cb Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 11:42:10 +1300 Subject: [PATCH 15/22] Everyone gets harvest months, not just perennials --- app/views/plantings/_timeline.html.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/plantings/_timeline.html.haml b/app/views/plantings/_timeline.html.haml index 8ff8be310..2baf4eebe 100644 --- a/app/views/plantings/_timeline.html.haml +++ b/app/views/plantings/_timeline.html.haml @@ -20,8 +20,8 @@ planting: planting, week_number: week_number, date_this_week: planting.planted_at + week_number.weeks - (One emojii = 1 week) -- if planting.perennial? + %small (One emojii = 1 week) +.harvest-months Harvest months: - if planting.harvest_months.empty? %span.text-muted From 1e663fc3df894924aa630cd82471ac8d8bf08f6a Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 12:13:48 +1300 Subject: [PATCH 16/22] Layout tidy to cope with neighbours on page --- app/views/plantings/show.html.haml | 34 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index add37d4f5..4be8a3f9e 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -14,6 +14,15 @@ %li.breadcrumb-item= link_to @planting.owner, member_plantings_path(@planting.owner) %li.breadcrumb-item.active= link_to @planting.crop.name, @planting + +- if @planting.parent_seed.nil? && @matching_seeds && @matching_seeds.any? && @planting.owner == current_member + .alert.alert-info{role: "alert"} + = bootstrap_form_for(@planting) do |f| + Is this from one of these plantings? + - @matching_seeds.each do |seed| + = f.radio_button :parent_seed_id, seed.id, label: seed + = f.submit "save", class: 'btn btn-sm' + .planting .row .col-md-8.col-xs-12 @@ -30,13 +39,6 @@ = render 'timeline', planting: @planting - - if @planting.parent_seed.nil? && @matching_seeds && @matching_seeds.any? && @planting.owner == current_member - .alert.alert-info{role: "alert"} - = bootstrap_form_for(@planting) do |f| - Is this from one of these plantings? - - @matching_seeds.each do |seed| - = f.radio_button :parent_seed_id, seed.id, label: seed - = f.submit "save", class: 'btn btn-sm' .col-md-4.col-xs-12 = render 'plantings/owner', planting: @planting @@ -52,6 +54,15 @@ :growstuff_markdown #{strip_tags(@planting.description)} %section= render 'plantings/photos', photos: @photos, planting: @planting + + %section.harvests + %a{name: 'harvests'} + = render 'plantings/harvests', planting: @planting + + %section.descendants + %a{name: 'seeds'} + = render 'plantings/descendants', planting: @planting + .col-md-4.col-xs-12 = render 'plantings/actions', planting: @planting %hr/ @@ -73,12 +84,3 @@ planted by #{planting.owner} in #{planting.garden.location} %p Other #{@planting.crop.name} plantings at the same latitude - .row - .col-md-6 - %section.harvests - %a{name: 'harvests'} - = render 'plantings/harvests', planting: @planting - .col-md-6 - %section.descendants - %a{name: 'seeds'} - = render 'plantings/descendants', planting: @planting From e7c30ef448b8c729fe9165c47c490acd4f309567 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 12:17:40 +1300 Subject: [PATCH 17/22] Wrapped long lines --- spec/models/planting_spec.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index f32d81f8a..565fb5801 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -230,16 +230,22 @@ describe Planting do before do # Near by planting with harvests nearby_garden = FactoryBot.create :garden, location: 'Greenwich, UK' - nearby_planting = FactoryBot.create :planting, crop: crop, garden: nearby_garden, owner: nearby_garden.owner, planted_at: '1 January 2000' - FactoryBot.create :harvest, planting: nearby_planting, crop: crop, harvested_at: '1 May 2019' - FactoryBot.create :harvest, planting: nearby_planting, crop: crop, harvested_at: '18 June 2019' - FactoryBot.create_list :harvest, 4, planting: nearby_planting, crop: crop, harvested_at: '18 August 2008' + nearby_planting = FactoryBot.create :planting, crop: crop, + garden: nearby_garden, owner: nearby_garden.owner, planted_at: '1 January 2000' + FactoryBot.create :harvest, planting: nearby_planting, crop: crop, + harvested_at: '1 May 2019' + FactoryBot.create :harvest, planting: nearby_planting, crop: crop, + harvested_at: '18 June 2019' + FactoryBot.create_list :harvest, 4, planting: nearby_planting, crop: crop, + harvested_at: '18 August 2008' # far away planting harvests faraway_garden = FactoryBot.create :garden, location: 'Amundsen-Scott Base, Antarctica' - faraway_planting = FactoryBot.create :planting, garden: faraway_garden, crop: crop, owner: faraway_garden.owner, planted_at: '16 May 2001' + faraway_planting = FactoryBot.create :planting, garden: faraway_garden, crop: crop, + owner: faraway_garden.owner, planted_at: '16 May 2001' - FactoryBot.create_list :harvest, 4, planting: faraway_planting, crop: crop, harvested_at: '18 December 2006' + FactoryBot.create_list :harvest, 4, planting: faraway_planting, crop: crop, + harvested_at: '18 December 2006' end it { expect(planting.harvest_months).to eq(5 => 1, 6 => 1, 8 => 4) } end From cf2c42a7e05079d7d75b599755f0df7d09faa47c Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 12:21:37 +1300 Subject: [PATCH 18/22] Only display location if we have a location --- app/views/plantings/_timeline.html.haml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/views/plantings/_timeline.html.haml b/app/views/plantings/_timeline.html.haml index 2baf4eebe..05a29da3a 100644 --- a/app/views/plantings/_timeline.html.haml +++ b/app/views/plantings/_timeline.html.haml @@ -35,4 +35,5 @@ - else .badge.text-muted{'aria-hidden': "true"} = I18n.t('date.abbr_month_names')[month] - in #{link_to planting.garden.location, place_path(planting.garden.location)} + - unless planting.garden.location.blank? + in #{link_to planting.garden.location, place_path(planting.garden.location)} From ace0f0ac16ec5f55c0a8e14d7ae33f9aa59d789f Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 24 Nov 2019 12:46:03 +1300 Subject: [PATCH 19/22] stub the neighbours variable on planting views spec --- spec/views/plantings/show.html.haml_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/views/plantings/show.html.haml_spec.rb b/spec/views/plantings/show.html.haml_spec.rb index efb3482ed..c774c5d48 100644 --- a/spec/views/plantings/show.html.haml_spec.rb +++ b/spec/views/plantings/show.html.haml_spec.rb @@ -13,6 +13,7 @@ describe "plantings/show" do before do assign(:planting, planting) assign(:photos, planting.photos.paginate(page: 1)) + assign(:neighbours, planting.nearby_same_crop) controller.stub(:current_user) { member } end From faf375c932106faa6ae9c9ab97aa647f5d541d91 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 25 Nov 2019 13:25:05 +1300 Subject: [PATCH 20/22] Feature spec for month by month harvest --- app/views/plantings/_timeline.html.haml | 4 +- spec/features/plantings/prediction_spec.rb | 60 ++++++++++++++++++++++ spec/features/plantings/show_spec.rb | 1 + 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 spec/features/plantings/prediction_spec.rb diff --git a/app/views/plantings/_timeline.html.haml b/app/views/plantings/_timeline.html.haml index 05a29da3a..ee317f299 100644 --- a/app/views/plantings/_timeline.html.haml +++ b/app/views/plantings/_timeline.html.haml @@ -30,10 +30,10 @@ - else - (1..12).each do |month| - if planting.harvest_months.keys().include?(month.to_f) - .badge.badge-info + .badge.badge-info.badge-harvesting{id: "month-#{month}"} = I18n.t('date.abbr_month_names')[month] - else - .badge.text-muted{'aria-hidden': "true"} + .badge.text-muted{'aria-hidden': "true", id: "month-#{month}"} = I18n.t('date.abbr_month_names')[month] - unless planting.garden.location.blank? in #{link_to planting.garden.location, place_path(planting.garden.location)} diff --git a/spec/features/plantings/prediction_spec.rb b/spec/features/plantings/prediction_spec.rb new file mode 100644 index 000000000..b775fd8fd --- /dev/null +++ b/spec/features/plantings/prediction_spec.rb @@ -0,0 +1,60 @@ +require "rails_helper" +require 'custom_matchers' +describe "Display a planting", :js, :elasticsearch do + describe 'planting perennial' do + let(:garden) { FactoryBot.create :garden, location: 'Edinburgh' } + let(:crop) { FactoryBot.create(:crop, name: 'feijoa', perennial: true) } + let(:planting) { FactoryBot.create :planting, crop: crop, garden: garden, owner: garden.owner } + + describe 'no harvest to predict from' do + before { visit planting_path(planting) } + it { expect(planting.harvest_months).to eq({}) } + it { expect(page).to have_content 'We need more data on this crop in your latitude.' } + end + + describe 'harvests used to predict' do + before do + FactoryBot.create :harvest, planting: planting, crop: crop, harvested_at: '1 May 2019' + FactoryBot.create :harvest, planting: planting, crop: crop, harvested_at: '18 June 2019' + FactoryBot.create_list :harvest, 4, planting: planting, crop: crop, harvested_at: '18 August 2019' + end + before { visit planting_path(planting) } + it { expect(page.find("#month-1")[:class]).not_to include("badge-harvesting") } + it { expect(page.find("#month-2")[:class]).not_to include("badge-harvesting") } + it { expect(page.find("#month-5")[:class]).to include("badge-harvesting") } + it { expect(page.find("#month-6")[:class]).to include("badge-harvesting") } + it { expect(page.find("#month-8")[:class]).to include("badge-harvesting") } + end + + describe 'nearby plantings used to predict' do + # Note the locations used need to be stubbed in geocoder + + before do + # Near by planting with harvests + nearby_garden = FactoryBot.create :garden, location: 'Greenwich, UK' + nearby_planting = FactoryBot.create :planting, crop: crop, + garden: nearby_garden, owner: nearby_garden.owner, planted_at: '1 January 2000' + FactoryBot.create :harvest, planting: nearby_planting, crop: crop, + harvested_at: '1 May 2019' + FactoryBot.create :harvest, planting: nearby_planting, crop: crop, + harvested_at: '18 June 2019' + FactoryBot.create_list :harvest, 4, planting: nearby_planting, crop: crop, + harvested_at: '18 August 2008' + + # far away planting harvests + faraway_garden = FactoryBot.create :garden, location: 'Amundsen-Scott Base, Antarctica' + faraway_planting = FactoryBot.create :planting, garden: faraway_garden, crop: crop, + owner: faraway_garden.owner, planted_at: '16 May 2001' + + FactoryBot.create_list :harvest, 4, planting: faraway_planting, crop: crop, + harvested_at: '18 December 2006' + end + before { visit planting_path(planting) } + it { expect(page.find("#month-1")[:class]).not_to include("badge-harvesting") } + it { expect(page.find("#month-2")[:class]).not_to include("badge-harvesting") } + it { expect(page.find("#month-5")[:class]).to include("badge-harvesting") } + it { expect(page.find("#month-6")[:class]).to include("badge-harvesting") } + it { expect(page.find("#month-8")[:class]).to include("badge-harvesting") } + end + end +end diff --git a/spec/features/plantings/show_spec.rb b/spec/features/plantings/show_spec.rb index 9d8450834..f67abc6e2 100644 --- a/spec/features/plantings/show_spec.rb +++ b/spec/features/plantings/show_spec.rb @@ -61,6 +61,7 @@ describe "Display a planting", :js, :elasticsearch do it { expect(find('.plantingfact--quantity')).to have_text '100' } end end + context 'signed in' do include_context 'signed in member' before { visit planting_path(planting) } From 5ae4dbaa7336873f95ac8991061b682b0972bc66 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 25 Nov 2019 13:33:47 +1300 Subject: [PATCH 21/22] Fix coverage reporter --- .travis.yml | 9 ++------- script/install_codeclimate.sh | 7 ------- 2 files changed, 2 insertions(+), 14 deletions(-) delete mode 100755 script/install_codeclimate.sh diff --git a/.travis.yml b/.travis.yml index 63706e849..0cda2b910 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,14 +29,8 @@ before_install: # - sudo apt update - sudo apt install dpkg - sudo apt install google-chrome-stable - - ./script/install_codeclimate.sh - ./script/install_linters.sh - ./script/install_elasticsearch.sh -before_script: - - > - if [ "${COVERAGE}" = "true" ]; then - ./cc-test-reporter before-build - fi script: - set -e - > @@ -52,7 +46,8 @@ script: after_script: - > if [ "${COVERAGE}" = "true" ]; then - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT; + gem install codeclimate + codeclimate-test-reporter fi - > if [ "${PERCY}" = "true" ]; then diff --git a/script/install_codeclimate.sh b/script/install_codeclimate.sh deleted file mode 100755 index f48091a9a..000000000 --- a/script/install_codeclimate.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -if [ "${COVERAGE}" = "true" ]; then - set -euv - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter; - chmod +x ./cc-test-reporter; -fi From 82e2734caa62f68f3a6105e0daee2846b320ed99 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 25 Nov 2019 14:03:59 +1300 Subject: [PATCH 22/22] USe the codeclimate gem --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0cda2b910..f9aa1768d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,7 +46,7 @@ script: after_script: - > if [ "${COVERAGE}" = "true" ]; then - gem install codeclimate + gem install codeclimate-test-reporter codeclimate-test-reporter fi - >