From d1603a6e7fe08a647c0de82b8ed8408fd6c4a0e4 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 16 Apr 2018 07:18:15 +1200 Subject: [PATCH 01/41] Badges showing finished and harvest status --- app/assets/stylesheets/overrides.sass | 3 ++ app/helpers/plantings_helper.rb | 10 +++++ app/models/planting.rb | 47 ++++++++++++++++++++++++ app/views/gardens/_overview.html.haml | 7 +--- app/views/plantings/_badges.html.haml | 15 ++++++++ app/views/plantings/_thumbnail.html.haml | 13 ++++--- 6 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 app/views/plantings/_badges.html.haml diff --git a/app/assets/stylesheets/overrides.sass b/app/assets/stylesheets/overrides.sass index 71dc96630..293e8aea7 100644 --- a/app/assets/stylesheets/overrides.sass +++ b/app/assets/stylesheets/overrides.sass @@ -100,6 +100,9 @@ p.stats dd margin-left: auto +.planting-badges + position: absolute + #placesmap, #cropmap height: 500px diff --git a/app/helpers/plantings_helper.rb b/app/helpers/plantings_helper.rb index f71dc8158..b5dac192a 100644 --- a/app/helpers/plantings_helper.rb +++ b/app/helpers/plantings_helper.rb @@ -32,4 +32,14 @@ module PlantingsHelper def plantings_active_tickbox_path(owner, show_all) show_inactive_tickbox_path('plantings', owner, show_all) end + + def days_from_now_to_finished(planting) + return unless planting.finish_predicted_at.present? && planting.finish_predicted_at > Time.zone.today + (planting.finish_predicted_at - Time.zone.today).to_i + end + + def days_from_now_to_first_harvest(planting) + return unless planting.planted_at.present? && planting.first_harvest_predicted_at.present? + (planting.first_harvest_predicted_at - Time.zone.today).to_i + end end diff --git a/app/models/planting.rb b/app/models/planting.rb index 3dff4c006..0ac677abf 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -89,6 +89,16 @@ class Planting < ActiveRecord::Base planted_at + crop.median_lifespan.days if crop.median_lifespan.present? && planted_at.present? end + def first_harvest_predicted_at + return unless crop.median_days_to_first_harvest.present? && planted_at.present? + planted_at + crop.median_days_to_first_harvest.days + end + + def last_harvest_predicted_at + return unless crop.median_days_to_last_harvest.present? && planted_at.present? + planted_at + crop.median_days_to_last_harvest.days + end + def calculate_lifespan self.lifespan = (planted_at.present? && finished_at.present? ? finished_at - planted_at : nil) end @@ -130,6 +140,43 @@ class Planting < ActiveRecord::Base harvests_with_dates.maximum(:harvested_at) end + def zombie? + crop.annual? && !finished && + planted_at.present? && + finish_predicted_at.present? && + (finish_predicted_at + 60.days) < Time.zone.today + end + + def living_past_predicted_finished? + crop.annual? && !finished && + planted_at.present? && + finish_predicted_at.present? && + finish_predicted_at > Time.zone.today + end + + def harvest_time? + return false if crop.perennial || finished + + # We have harvests but haven't finished + harvests.size.positive? || + + # or, we don't have harvests, but we predict we should by now + (first_harvest_predicted_at.present? && + harvests.empty? && + first_harvest_predicted_at < Time.zone.today) + end + + def before_harvest_time? + first_harvest_predicted_at.present? && + harvests.empty? && + first_harvest_predicted_at.present? && + first_harvest_predicted_at > Time.zone.today + end + + def age_in_days + (Time.zone.today - planted_at).to_i if planted_at.present? + end + private def harvests_with_dates diff --git a/app/views/gardens/_overview.html.haml b/app/views/gardens/_overview.html.haml index b8f9ce54a..978a6abbf 100644 --- a/app/views/gardens/_overview.html.haml +++ b/app/views/gardens/_overview.html.haml @@ -13,12 +13,9 @@ .col-md-10 .row - if garden.plantings.current.size.positive? - - garden.plantings.current.includes(:crop).each do |planting| + - garden.plantings.current.includes(:crop, :photos).each do |planting| .col-md-2.col-sm-6.col-xs-6 - .hover-wrapper - .text - = render 'plantings/actions', planting: planting - = render partial: "plantings/thumbnail", locals: { planting: planting } + = render "plantings/thumbnail", planting: planting - else .col-md-2.col-sm-6.col-xs-6 no plantings - if can?(:edit, garden) diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml new file mode 100644 index 000000000..2e0c078b7 --- /dev/null +++ b/app/views/plantings/_badges.html.haml @@ -0,0 +1,15 @@ +// Finish times +- if planting.zombie? + %span.badge.badge-danger zombie +- elsif planting.living_past_predicted_finished? + %span.badge.badge-light + = days_from_now_to_finished(planting) + days until finished + +// Harvest times +- if planting.harvest_time? + %span.badge.badge-light harvest time +- elsif planting.before_harvest_time? + %span.badge.badge-light + = days_from_now_to_first_harvest(planting) + days until harvest diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index e34c653da..6d52308cd 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -1,9 +1,10 @@ -.thumbnail - .planting-thumbnail - - if planting - = link_to image_tag(planting_image_path(planting), - alt: planting.crop.name, class: 'img'), - planting +.planting-badges + = render 'plantings/badges', planting: planting +.hover-wrapper + .text= render 'plantings/actions', planting: planting + .thumbnail + .planting-thumbnail + = link_to image_tag(planting_image_path(planting), alt: planting.crop.name, class: 'img'), planting_path(planting) .plantinginfo .planting-name = render 'plantings/progress', planting: planting, show_explanation: false From d043613bde97bfdc7ef86f0d5803fb4c4b4c9e11 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 16 Apr 2018 21:14:47 +1200 Subject: [PATCH 02/41] Model specs for harvest time and zombies --- spec/models/planting_spec.rb | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 6948caa75..404d50e69 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -97,8 +97,10 @@ describe Planting do it { expect(planting.expected_lifespan).to eq(nil) } end context 'lots of data' do + let(:crop) { FactoryBot.create :crop } + # this is a method so it creates a new one each time def one_hundred_day_old_planting - FactoryBot.create(:planting, crop: planting.crop, planted_at: 100.days.ago) + FactoryBot.create(:planting, crop: crop, planted_at: 100.days.ago) end before do # 50 days to harvest @@ -110,19 +112,35 @@ describe Planting do # 10 days to harvest FactoryBot.create(:harvest, harvested_at: 90.days.ago, crop: planting.crop, planting: one_hundred_day_old_planting) + planting.crop.plantings.each(&:update_harvest_days) planting.crop.update_lifespan_medians planting.crop.update_harvest_medians end - it { expect(planting.crop.median_days_to_first_harvest).to eq(20) } + it { expect(crop.median_days_to_first_harvest).to eq(20) } + describe 'sets median time to harvest' do + let(:planting) { FactoryBot.create :planting, crop: crop, planted_at: Time.zone.today } + it { expect(planting.first_harvest_predicted_at).to eq(Time.zone.today + 20.days) } + end + + describe 'harvest still growing' do + let(:planting) { FactoryBot.create :planting, crop: crop, planted_at: Time.zone.today } + it { expect(planting.before_harvest_time?).to eq true } + it { expect(planting.harvest_time?).to eq false } + end + describe 'harvesting ready now' do + let(:planting) { FactoryBot.create :planting, crop: crop, planted_at: 21.days.ago } + it { expect(planting.first_harvest_predicted_at).to eq(1.day.ago.to_date) } + it { expect(planting.before_harvest_time?).to eq false } + it { expect(planting.harvest_time?).to eq true } + end end describe 'planting has no harvests' do + let(:planting) { FactoryBot.create :planting } before do planting.update_harvest_days planting.crop.update_harvest_medians end - let(:planting) { FactoryBot.create :planting } - it { expect(planting.days_to_first_harvest).to eq(nil) } it { expect(planting.days_to_last_harvest).to eq(nil) } end From 934bd9adb8fd79a020af5d56e4ceb1e4d8e8e659 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 16 Apr 2018 22:11:36 +1200 Subject: [PATCH 03/41] Fixed bug in spec, wasn't really on member's garden#index --- spec/features/gardens/gardens_index_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/features/gardens/gardens_index_spec.rb b/spec/features/gardens/gardens_index_spec.rb index 39161d5c8..da8eeeac2 100644 --- a/spec/features/gardens/gardens_index_spec.rb +++ b/spec/features/gardens/gardens_index_spec.rb @@ -10,7 +10,7 @@ feature "Gardens#index", :js do context "with 10 gardens" do before do FactoryBot.create_list :garden, 10, owner: member - visit gardens_path(member: member) + visit gardens_path(owner: member.login_name) end it "displays each of the gardens" do From 3ee253743826be788b599bfae2a81ebd054e5a4c Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Tue, 17 Apr 2018 10:03:59 +1200 Subject: [PATCH 04/41] Only show time to finished if not finished --- app/views/plantings/_badges.html.haml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml index 2e0c078b7..a665b2a75 100644 --- a/app/views/plantings/_badges.html.haml +++ b/app/views/plantings/_badges.html.haml @@ -2,6 +2,8 @@ - if planting.zombie? %span.badge.badge-danger zombie - elsif planting.living_past_predicted_finished? + +- else %span.badge.badge-light = days_from_now_to_finished(planting) days until finished From 2a8c4d4829780f34d0fb5c5f8166931b1dfd05f1 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Tue, 17 Apr 2018 10:06:40 +1200 Subject: [PATCH 05/41] Add finished and finished_at to plantings factory --- spec/factories/planting.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/factories/planting.rb b/spec/factories/planting.rb index 5b7311025..f19d80145 100644 --- a/spec/factories/planting.rb +++ b/spec/factories/planting.rb @@ -6,6 +6,8 @@ FactoryBot.define do planted_at { Time.zone.local(2014, 7, 30) } quantity 33 description "This is a *really* good plant." + finished nil + finished_at nil factory :seed_planting do planted_from 'seed' From 3ed90b8d046ea56f97d17619ed22af7a37f05fe0 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Thu, 19 Apr 2018 13:44:39 +1200 Subject: [PATCH 06/41] Moving predictions to own file --- app/models/concerns/predictable.rb | 109 +++++++++++++++++++++++++++++ app/models/planting.rb | 97 +------------------------ 2 files changed, 110 insertions(+), 96 deletions(-) create mode 100644 app/models/concerns/predictable.rb diff --git a/app/models/concerns/predictable.rb b/app/models/concerns/predictable.rb new file mode 100644 index 000000000..992b63d3b --- /dev/null +++ b/app/models/concerns/predictable.rb @@ -0,0 +1,109 @@ +module Predictable + extend ActiveSupport::Concern + + included do + ## Triggers + before_save :calculate_lifespan + + def calculate_lifespan + self.lifespan = (planted_at.present? && finished_at.present? ? finished_at - planted_at : nil) + end + + # dates + def first_harvest_date + harvests_with_dates.minimum(:harvested_at) + end + + def last_harvest_date + harvests_with_dates.maximum(:harvested_at) + end + + def finish_predicted_at + planted_at + crop.median_lifespan.days if crop.median_lifespan.present? && planted_at.present? + end + + def first_harvest_predicted_at + return unless crop.median_days_to_first_harvest.present? && planted_at.present? + planted_at + crop.median_days_to_first_harvest.days + end + + def last_harvest_predicted_at + return unless crop.median_days_to_last_harvest.present? && planted_at.present? + planted_at + crop.median_days_to_last_harvest.days + end + + # days + def age_in_days + (Time.zone.today - planted_at).to_i if planted_at.present? + end + + def expected_lifespan + if planted_at.present? && finished_at.present? + return (finished_at - planted_at).to_i + end + crop.median_lifespan + end + + def days_since_planted + (Time.zone.today - planted_at).to_i if planted_at.present? + end + + # progress + def percentage_grown + return 100 if finished + return if planted_at.blank? || expected_lifespan.blank? + p = (days_since_planted / expected_lifespan.to_f) * 100 + return p if p <= 100 + 100 + end + + # actions + def update_harvest_days! + days_to_first_harvest = nil + days_to_last_harvest = nil + if planted_at.present? && harvests_with_dates.size.positive? + days_to_first_harvest = (first_harvest_date - planted_at).to_i + days_to_last_harvest = (last_harvest_date - planted_at).to_i if finished? + end + update(days_to_first_harvest: days_to_first_harvest, days_to_last_harvest: days_to_last_harvest) + end + + # states + def finished_is_predicatable? + crop.annual? && planted_at.present? && finish_predicted_at.present? + end + + def zombie? + crop.annual? && !finished && + planted_at.present? && + finish_predicted_at.present? && + (finish_predicted_at + 60.days) < Time.zone.today + end + + def should_be_finished? + crop.annual? && !finished && + planted_at.present? && + finish_predicted_at.present? && + finish_predicted_at < Time.zone.today + end + + def harvest_time? + return false if crop.perennial || finished + + # We have harvests but haven't finished + harvests.size.positive? || + + # or, we don't have harvests, but we predict we should by now + (first_harvest_predicted_at.present? && + harvests.empty? && + first_harvest_predicted_at < Time.zone.today) + end + + def before_harvest_time? + first_harvest_predicted_at.present? && + harvests.empty? && + first_harvest_predicted_at.present? && + first_harvest_predicted_at > Time.zone.today + end + end +end diff --git a/app/models/planting.rb b/app/models/planting.rb index 0ac677abf..2aa50916e 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -3,6 +3,7 @@ class Planting < ActiveRecord::Base include PhotoCapable include Finishable include Ownable + include Predictable friendly_id :planting_slug, use: %i(slugged finders) # Constants @@ -13,10 +14,6 @@ class Planting < ActiveRecord::Base 'graft', 'layering' ].freeze - ## - ## Triggers - before_save :calculate_lifespan - belongs_to :garden belongs_to :crop, counter_cache: true has_many :harvests, dependent: :destroy @@ -85,98 +82,6 @@ class Planting < ActiveRecord::Base planted_at.present? && planted_at <= Date.current end - def finish_predicted_at - planted_at + crop.median_lifespan.days if crop.median_lifespan.present? && planted_at.present? - end - - def first_harvest_predicted_at - return unless crop.median_days_to_first_harvest.present? && planted_at.present? - planted_at + crop.median_days_to_first_harvest.days - end - - def last_harvest_predicted_at - return unless crop.median_days_to_last_harvest.present? && planted_at.present? - planted_at + crop.median_days_to_last_harvest.days - end - - def calculate_lifespan - self.lifespan = (planted_at.present? && finished_at.present? ? finished_at - planted_at : nil) - end - - def expected_lifespan - if planted_at.present? && finished_at.present? - return (finished_at - planted_at).to_i - end - crop.median_lifespan - end - - def days_since_planted - (Time.zone.today - planted_at).to_i if planted_at.present? - end - - def percentage_grown - return 100 if finished - return if planted_at.blank? || expected_lifespan.blank? - p = (days_since_planted / expected_lifespan.to_f) * 100 - return p if p <= 100 - 100 - end - - def update_harvest_days - days_to_first_harvest = nil - days_to_last_harvest = nil - if planted_at.present? && harvests_with_dates.size.positive? - days_to_first_harvest = (first_harvest_date - planted_at).to_i - days_to_last_harvest = (last_harvest_date - planted_at).to_i if finished? - end - update(days_to_first_harvest: days_to_first_harvest, days_to_last_harvest: days_to_last_harvest) - end - - def first_harvest_date - harvests_with_dates.minimum(:harvested_at) - end - - def last_harvest_date - harvests_with_dates.maximum(:harvested_at) - end - - def zombie? - crop.annual? && !finished && - planted_at.present? && - finish_predicted_at.present? && - (finish_predicted_at + 60.days) < Time.zone.today - end - - def living_past_predicted_finished? - crop.annual? && !finished && - planted_at.present? && - finish_predicted_at.present? && - finish_predicted_at > Time.zone.today - end - - def harvest_time? - return false if crop.perennial || finished - - # We have harvests but haven't finished - harvests.size.positive? || - - # or, we don't have harvests, but we predict we should by now - (first_harvest_predicted_at.present? && - harvests.empty? && - first_harvest_predicted_at < Time.zone.today) - end - - def before_harvest_time? - first_harvest_predicted_at.present? && - harvests.empty? && - first_harvest_predicted_at.present? && - first_harvest_predicted_at > Time.zone.today - end - - def age_in_days - (Time.zone.today - planted_at).to_i if planted_at.present? - end - private def harvests_with_dates From 279c922b34a3568b2fb6b7224b9e5c3cad712c3a Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Thu, 19 Apr 2018 13:45:29 +1200 Subject: [PATCH 07/41] Rename update predictions methods to have ! --- app/controllers/harvests_controller.rb | 2 +- app/controllers/plantings_controller.rb | 2 +- app/models/crop.rb | 2 +- db/migrate/20171105011017_set_prediction_data.rb | 2 +- spec/models/planting_spec.rb | 8 ++++---- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/harvests_controller.rb b/app/controllers/harvests_controller.rb index 2e7fbef3e..73aaafd86 100644 --- a/app/controllers/harvests_controller.rb +++ b/app/controllers/harvests_controller.rb @@ -92,7 +92,7 @@ class HarvestsController < ApplicationController # if this harvest is not linked to a planting, then do nothing return if @harvest.planting.nil? - @harvest.planting.update_harvest_days + @harvest.planting.update_harvest_days! @harvest.crop.update_harvest_medians end end diff --git a/app/controllers/plantings_controller.rb b/app/controllers/plantings_controller.rb index f6da9d076..5ee31ea2c 100644 --- a/app/controllers/plantings_controller.rb +++ b/app/controllers/plantings_controller.rb @@ -77,7 +77,7 @@ class PlantingsController < ApplicationController end def update_planting_medians - @planting.update_harvest_days + @planting.update_harvest_days! end def planting_params diff --git a/app/models/crop.rb b/app/models/crop.rb index bead8e89b..b1d492353 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -185,7 +185,7 @@ class Crop < ActiveRecord::Base end def update_medians - plantings.each(&:update_harvest_days) + plantings.each(&:update_harvest_days!) update_lifespan_medians update_harvest_medians end diff --git a/db/migrate/20171105011017_set_prediction_data.rb b/db/migrate/20171105011017_set_prediction_data.rb index 149001927..5f5590d37 100644 --- a/db/migrate/20171105011017_set_prediction_data.rb +++ b/db/migrate/20171105011017_set_prediction_data.rb @@ -1,7 +1,7 @@ class SetPredictionData < ActiveRecord::Migration def up say "Updating all plantings time to first harvest" - Planting.all.each(&:update_harvest_days) + Planting.all.each(&:update_harvest_days!) say "Updating crop median time to first harvest, and lifespan" Crop.all.each do |crop| crop.update_lifespan_medians diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 404d50e69..146548f51 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -113,7 +113,7 @@ describe Planting do FactoryBot.create(:harvest, harvested_at: 90.days.ago, crop: planting.crop, planting: one_hundred_day_old_planting) - planting.crop.plantings.each(&:update_harvest_days) + planting.crop.plantings.each(&:update_harvest_days!) planting.crop.update_lifespan_medians planting.crop.update_harvest_medians end @@ -138,7 +138,7 @@ describe Planting do describe 'planting has no harvests' do let(:planting) { FactoryBot.create :planting } before do - planting.update_harvest_days + planting.update_harvest_days! planting.crop.update_harvest_medians end it { expect(planting.days_to_first_harvest).to eq(nil) } @@ -152,7 +152,7 @@ describe Planting do planting: planting, crop: planting.crop, harvested_at: 10.days.ago) - planting.update_harvest_days + planting.update_harvest_days! planting.crop.update_harvest_medians end it { expect(planting.days_to_first_harvest).to eq(90) } @@ -166,7 +166,7 @@ describe Planting do before do FactoryBot.create :harvest, planting: planting, crop: planting.crop, harvested_at: 90.days.ago FactoryBot.create :harvest, planting: planting, crop: planting.crop, harvested_at: 10.days.ago - planting.update_harvest_days + planting.update_harvest_days! planting.crop.update_harvest_medians end it { expect(planting.days_to_first_harvest).to eq(10) } From 5bf75fa36ff8f2dea14cb421333b994497b503ea Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Thu, 19 Apr 2018 13:45:47 +1200 Subject: [PATCH 08/41] Only show finish prediction badges if we have enough data --- app/views/plantings/_badges.html.haml | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml index a665b2a75..a2ab4afec 100644 --- a/app/views/plantings/_badges.html.haml +++ b/app/views/plantings/_badges.html.haml @@ -1,12 +1,13 @@ // Finish times -- if planting.zombie? - %span.badge.badge-danger zombie -- elsif planting.living_past_predicted_finished? - -- else - %span.badge.badge-light - = days_from_now_to_finished(planting) - days until finished +- if planting.finished_is_predicatable? + - if planting.zombie? + %span.badge.badge-danger zombie + - elsif planting.should_be_finished? + %span.badge.badge-light days past finished + - else + %span.badge.badge-light + = days_from_now_to_finished(planting) + days until finished // Harvest times - if planting.harvest_time? From 7a112aeecf2245b77e8f27bf262a5725f3293a98 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Thu, 19 Apr 2018 13:49:54 +1200 Subject: [PATCH 09/41] Grammar correction in method name --- app/models/concerns/predictable.rb | 2 +- app/views/plantings/_badges.html.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/predictable.rb b/app/models/concerns/predictable.rb index 992b63d3b..0a0aa5a73 100644 --- a/app/models/concerns/predictable.rb +++ b/app/models/concerns/predictable.rb @@ -69,7 +69,7 @@ module Predictable end # states - def finished_is_predicatable? + def finish_is_predicatable? crop.annual? && planted_at.present? && finish_predicted_at.present? end diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml index a2ab4afec..16e83a075 100644 --- a/app/views/plantings/_badges.html.haml +++ b/app/views/plantings/_badges.html.haml @@ -1,5 +1,5 @@ // Finish times -- if planting.finished_is_predicatable? +- if planting.finish_is_predicatable? - if planting.zombie? %span.badge.badge-danger zombie - elsif planting.should_be_finished? From c2ff3790dbaece2cf2d62f81c8b8c988de67f2dd Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 09:22:35 +1200 Subject: [PATCH 10/41] set finished false on planting factory some code+tests doesn't cope with nil --- spec/factories/planting.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/planting.rb b/spec/factories/planting.rb index f19d80145..27a2b179d 100644 --- a/spec/factories/planting.rb +++ b/spec/factories/planting.rb @@ -6,7 +6,7 @@ FactoryBot.define do planted_at { Time.zone.local(2014, 7, 30) } quantity 33 description "This is a *really* good plant." - finished nil + finished false finished_at nil factory :seed_planting do From b7d4781aefb5f359e8cbbfb380041bd292d6e0fb Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 10:18:18 +1200 Subject: [PATCH 11/41] Split the planting&harvest predictions into files --- .../{predictable.rb => predict_harvest.rb} | 66 +++---------------- app/models/concerns/predict_planting.rb | 57 ++++++++++++++++ app/models/planting.rb | 11 ++-- 3 files changed, 72 insertions(+), 62 deletions(-) rename app/models/concerns/{predictable.rb => predict_harvest.rb} (52%) create mode 100644 app/models/concerns/predict_planting.rb diff --git a/app/models/concerns/predictable.rb b/app/models/concerns/predict_harvest.rb similarity index 52% rename from app/models/concerns/predictable.rb rename to app/models/concerns/predict_harvest.rb index 0a0aa5a73..d5422ee35 100644 --- a/app/models/concerns/predictable.rb +++ b/app/models/concerns/predict_harvest.rb @@ -1,14 +1,7 @@ -module Predictable +module PredictHarvest extend ActiveSupport::Concern - included do - ## Triggers - before_save :calculate_lifespan - - def calculate_lifespan - self.lifespan = (planted_at.present? && finished_at.present? ? finished_at - planted_at : nil) - end - + included do # rubocop:disable Metrics/BlockLength # dates def first_harvest_date harvests_with_dates.minimum(:harvested_at) @@ -18,10 +11,6 @@ module Predictable harvests_with_dates.maximum(:harvested_at) end - def finish_predicted_at - planted_at + crop.median_lifespan.days if crop.median_lifespan.present? && planted_at.present? - end - def first_harvest_predicted_at return unless crop.median_days_to_first_harvest.present? && planted_at.present? planted_at + crop.median_days_to_first_harvest.days @@ -32,31 +21,6 @@ module Predictable planted_at + crop.median_days_to_last_harvest.days end - # days - def age_in_days - (Time.zone.today - planted_at).to_i if planted_at.present? - end - - def expected_lifespan - if planted_at.present? && finished_at.present? - return (finished_at - planted_at).to_i - end - crop.median_lifespan - end - - def days_since_planted - (Time.zone.today - planted_at).to_i if planted_at.present? - end - - # progress - def percentage_grown - return 100 if finished - return if planted_at.blank? || expected_lifespan.blank? - p = (days_since_planted / expected_lifespan.to_f) * 100 - return p if p <= 100 - 100 - end - # actions def update_harvest_days! days_to_first_harvest = nil @@ -68,25 +32,7 @@ module Predictable update(days_to_first_harvest: days_to_first_harvest, days_to_last_harvest: days_to_last_harvest) end - # states - def finish_is_predicatable? - crop.annual? && planted_at.present? && finish_predicted_at.present? - end - - def zombie? - crop.annual? && !finished && - planted_at.present? && - finish_predicted_at.present? && - (finish_predicted_at + 60.days) < Time.zone.today - end - - def should_be_finished? - crop.annual? && !finished && - planted_at.present? && - finish_predicted_at.present? && - finish_predicted_at < Time.zone.today - end - + # status def harvest_time? return false if crop.perennial || finished @@ -105,5 +51,11 @@ module Predictable first_harvest_predicted_at.present? && first_harvest_predicted_at > Time.zone.today end + + private + + def harvests_with_dates + harvests.where.not(harvested_at: nil) + end end end diff --git a/app/models/concerns/predict_planting.rb b/app/models/concerns/predict_planting.rb new file mode 100644 index 000000000..ca33a1d59 --- /dev/null +++ b/app/models/concerns/predict_planting.rb @@ -0,0 +1,57 @@ +module PredictPlanting + extend ActiveSupport::Concern + + included do # rubocop:disable Metrics/BlockLength + ## Triggers + before_save :calculate_lifespan + + def calculate_lifespan + self.lifespan = (planted_at.present? && finished_at.present? ? finished_at - planted_at : nil) + end + + # dates + def finish_predicted_at + planted_at + crop.median_lifespan.days if crop.median_lifespan.present? && planted_at.present? + end + + # days + def expected_lifespan + if planted_at.present? && finished_at.present? + return (finished_at - planted_at).to_i + end + crop.median_lifespan + end + + def days_since_planted + (Time.zone.today - planted_at).to_i if planted_at.present? + end + + # progress + def percentage_grown + return 100 if finished + return if planted_at.blank? || expected_lifespan.blank? + p = (days_since_planted / expected_lifespan.to_f) * 100 + return p if p <= 100 + 100 + end + + # states + def finish_is_predicatable? + crop.annual? && planted_at.present? && finish_predicted_at.present? + end + + def zombie? + crop.annual? && !finished && + planted_at.present? && + finish_predicted_at.present? && + (finish_predicted_at + 60.days) < Time.zone.today + end + + def should_be_finished? + crop.annual? && !finished && + planted_at.present? && + finish_predicted_at.present? && + finish_predicted_at < Time.zone.today + end + end +end diff --git a/app/models/planting.rb b/app/models/planting.rb index 2aa50916e..02dd7cff2 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -3,7 +3,8 @@ class Planting < ActiveRecord::Base include PhotoCapable include Finishable include Ownable - include Predictable + include PredictPlanting + include PredictHarvest friendly_id :planting_slug, use: %i(slugged finders) # Constants @@ -56,6 +57,10 @@ class Planting < ActiveRecord::Base in: PLANTED_FROM_VALUES, message: "%s is not a valid planting method" } + def age_in_days + (Time.zone.today - planted_at).to_i if planted_at.present? + end + def planting_slug [ owner.login_name, @@ -84,10 +89,6 @@ class Planting < ActiveRecord::Base private - def harvests_with_dates - harvests.where.not(harvested_at: nil) - end - # check that any finished_at date occurs after planted_at def finished_must_be_after_planted return unless planted_at && finished_at # only check if we have both From f8b76e9c846acabdb914268736594823de466f01 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 11:12:25 +1200 Subject: [PATCH 12/41] Reducing complexity in planting predictions --- app/models/concerns/predict_planting.rb | 29 ++++++++++++++----------- app/models/planting.rb | 10 ++++++++- spec/models/planting_spec.rb | 3 +-- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/models/concerns/predict_planting.rb b/app/models/concerns/predict_planting.rb index ca33a1d59..65d101569 100644 --- a/app/models/concerns/predict_planting.rb +++ b/app/models/concerns/predict_planting.rb @@ -16,10 +16,12 @@ module PredictPlanting # days def expected_lifespan - if planted_at.present? && finished_at.present? - return (finished_at - planted_at).to_i - end - crop.median_lifespan + actual_lifespan.present? ? actual_lifespan : crop.median_lifespan + end + + def actual_lifespan + return unless planted_at.present? && finished_at.present? + (finished_at - planted_at).to_i end def days_since_planted @@ -28,11 +30,14 @@ module PredictPlanting # progress def percentage_grown - return 100 if finished - return if planted_at.blank? || expected_lifespan.blank? - p = (days_since_planted / expected_lifespan.to_f) * 100 - return p if p <= 100 - 100 + 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? end # states @@ -41,14 +46,12 @@ module PredictPlanting end def zombie? - crop.annual? && !finished && - planted_at.present? && - finish_predicted_at.present? && + should_be_finished? && (finish_predicted_at + 60.days) < Time.zone.today end def should_be_finished? - crop.annual? && !finished && + crop.annual? && !finished? && planted_at.present? && finish_predicted_at.present? && finish_predicted_at < Time.zone.today diff --git a/app/models/planting.rb b/app/models/planting.rb index 02dd7cff2..bea6b9a8b 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -83,8 +83,16 @@ class Planting < ActiveRecord::Base photos.order(created_at: :desc).first end + def finished? + finished || (finished_at.present? && finished_at <= Time.zone.today) + end + def planted? - planted_at.present? && planted_at <= Date.current + planted_at.present? && planted_at <= Time.zone.today + end + + def growing? + planted? && !finished? end private diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 146548f51..0bd36f19f 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -73,8 +73,7 @@ describe Planting do describe 'planting finished 10 days, but was never planted' do let(:planting) { FactoryBot.create :planting, planted_at: nil, finished_at: 10.days.ago } - - it { expect(planting.percentage_grown).to eq nil } + it { expect(planting.percentage_grown).to eq 100 } end describe 'planted 30 days ago, finished 10 days ago' do From 8ce0717c0f6ff49c234767de380979c280a889ce Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 11:36:02 +1200 Subject: [PATCH 13/41] Fixing up planting helper predictions --- app/helpers/plantings_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/plantings_helper.rb b/app/helpers/plantings_helper.rb index b5dac192a..1bf216330 100644 --- a/app/helpers/plantings_helper.rb +++ b/app/helpers/plantings_helper.rb @@ -34,7 +34,7 @@ module PlantingsHelper end def days_from_now_to_finished(planting) - return unless planting.finish_predicted_at.present? && planting.finish_predicted_at > Time.zone.today + return unless planting.finish_is_predicatable? (planting.finish_predicted_at - Time.zone.today).to_i end From 9188bd384decac0fa8dd60640f766278c137c167 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 11:36:48 +1200 Subject: [PATCH 14/41] Futher simplifying planting badges --- app/views/plantings/_badges.html.haml | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml index 16e83a075..103cf156a 100644 --- a/app/views/plantings/_badges.html.haml +++ b/app/views/plantings/_badges.html.haml @@ -2,17 +2,16 @@ - if planting.finish_is_predicatable? - if planting.zombie? %span.badge.badge-danger zombie - - elsif planting.should_be_finished? - %span.badge.badge-light days past finished - - else + - elsif !planting.should_be_finished? %span.badge.badge-light = days_from_now_to_finished(planting) days until finished // Harvest times -- if planting.harvest_time? - %span.badge.badge-light harvest time -- elsif planting.before_harvest_time? - %span.badge.badge-light - = days_from_now_to_first_harvest(planting) - days until harvest +- unless planting.zombie? + - if planting.harvest_time? + %span.badge.badge-light harvest time + - elsif planting.before_harvest_time? + %span.badge.badge-light + = days_from_now_to_first_harvest(planting) + days until harvest From 4fe8cd566b1168d5f2937e9621951839516d2bed Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 12:37:00 +1200 Subject: [PATCH 15/41] USe finished not finished? because some plants have finished_at in the past but haven't had their .finished set to true yet --- app/models/concerns/predict_planting.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/predict_planting.rb b/app/models/concerns/predict_planting.rb index 65d101569..d670d78a2 100644 --- a/app/models/concerns/predict_planting.rb +++ b/app/models/concerns/predict_planting.rb @@ -51,10 +51,10 @@ module PredictPlanting end def should_be_finished? - crop.annual? && !finished? && + crop.annual? && !finished && planted_at.present? && finish_predicted_at.present? && - finish_predicted_at < Time.zone.today + finish_predicted_at <= Time.zone.today end end end From 935aebcb0d150bcacd7c67909b9680a07ee80df0 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 12:38:32 +1200 Subject: [PATCH 16/41] Tidy up visual link between planting thumbnail and crop name --- app/assets/stylesheets/overrides.sass | 20 ++++++++++++++++---- app/views/plantings/_progress.html.haml | 3 ++- app/views/plantings/_thumbnail.html.haml | 19 ++++++++++--------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/assets/stylesheets/overrides.sass b/app/assets/stylesheets/overrides.sass index 9651b2956..b5e9e06bd 100644 --- a/app/assets/stylesheets/overrides.sass +++ b/app/assets/stylesheets/overrides.sass @@ -93,17 +93,28 @@ p.stats padding-left: 1em width: 15em +.progress + border-radius: 0 + .planting + .planting-badges + position: absolute + + .planting-thumbnail + padding: 0 + border: 1px solid darken($beige, 10%) + border-radius: 4px + + .planting-name + position: relative + top: -1em + dl.planting-attributes dt text-align: left dd margin-left: auto -.planting-badges - position: absolute - - #placesmap, #cropmap height: 500px @@ -156,6 +167,7 @@ p.stats text-align: center margin-bottom: 1.5em max-width: 160px + max-height: 200px .member-thumbnail text-align: left diff --git a/app/views/plantings/_progress.html.haml b/app/views/plantings/_progress.html.haml index a594de75d..819cfe7af 100644 --- a/app/views/plantings/_progress.html.haml +++ b/app/views/plantings/_progress.html.haml @@ -1,5 +1,6 @@ - if planting.crop.perennial - %p Perennial + %p + %i perennial - elsif !planting.planted? - if show_explanation %p Progress: 0% - not planted yet diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index 6d52308cd..96bfeecbf 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -1,11 +1,12 @@ -.planting-badges - = render 'plantings/badges', planting: planting -.hover-wrapper - .text= render 'plantings/actions', planting: planting - .thumbnail - .planting-thumbnail - = link_to image_tag(planting_image_path(planting), alt: planting.crop.name, class: 'img'), planting_path(planting) - .plantinginfo +.planting + .planting-badges + = render 'plantings/badges', planting: planting + .hover-wrapper + .text + = render 'plantings/actions', planting: planting + .thumbnail + .planting-thumbnail + = link_to image_tag(planting_image_path(planting), alt: planting.crop.name, class: 'img'), planting_path(planting) + = render 'plantings/progress', planting: planting, show_explanation: false .planting-name - = render 'plantings/progress', planting: planting, show_explanation: false = link_to planting.crop.name, planting From 1b3e00fba0807019c23a64106f66849baa3c2728 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 13:00:24 +1200 Subject: [PATCH 17/41] Colours for badges on plantings --- app/assets/stylesheets/custom_bootstrap/variables.sass | 4 ++-- app/assets/stylesheets/overrides.sass | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/assets/stylesheets/custom_bootstrap/variables.sass b/app/assets/stylesheets/custom_bootstrap/variables.sass index f94f04106..f2fc82dda 100644 --- a/app/assets/stylesheets/custom_bootstrap/variables.sass +++ b/app/assets/stylesheets/custom_bootstrap/variables.sass @@ -1,7 +1,7 @@ // Use this file to override Twitter Bootstrap variables or define own variables. // Import original variables so they can be used in overrides -//@import 'bootstrap/variables.scss' +@import 'bootstrap/variables.scss' // Base colours @@ -10,7 +10,7 @@ $brown: #413f3b $green: #5f8e43 $blue: #2f4365 -$red: #8e4d43 +$red: #ff4d43 $orange: #ffa500 $yellow: #b2935c $white: #ffffff diff --git a/app/assets/stylesheets/overrides.sass b/app/assets/stylesheets/overrides.sass index b5e9e06bd..3f7bbdebb 100644 --- a/app/assets/stylesheets/overrides.sass +++ b/app/assets/stylesheets/overrides.sass @@ -96,6 +96,11 @@ p.stats .progress border-radius: 0 +.badge-zombie + background-color: $red +.badge-harvest + background-color: $blue + .planting .planting-badges position: absolute @@ -104,7 +109,8 @@ p.stats padding: 0 border: 1px solid darken($beige, 10%) border-radius: 4px - + .planting-actions + top: -8em .planting-name position: relative top: -1em From ca8dae4b78ec3e0b0d851b1e71f02987309580d6 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 13:02:09 +1200 Subject: [PATCH 18/41] Moving the planting buttons lower --- app/views/plantings/_thumbnail.html.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index 96bfeecbf..9d35f5c0f 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -2,11 +2,11 @@ .planting-badges = render 'plantings/badges', planting: planting .hover-wrapper - .text - = render 'plantings/actions', planting: planting .thumbnail .planting-thumbnail = link_to image_tag(planting_image_path(planting), alt: planting.crop.name, class: 'img'), planting_path(planting) = render 'plantings/progress', planting: planting, show_explanation: false .planting-name = link_to planting.crop.name, planting + .text + .planting-actions= render 'plantings/actions', planting: planting From a8509acd490f5cd6a8a6444a01a1490fa4a1bea9 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 13:02:51 +1200 Subject: [PATCH 19/41] Zombie status needs 90 days --- app/models/concerns/predict_planting.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/predict_planting.rb b/app/models/concerns/predict_planting.rb index d670d78a2..e0086d959 100644 --- a/app/models/concerns/predict_planting.rb +++ b/app/models/concerns/predict_planting.rb @@ -47,7 +47,7 @@ module PredictPlanting def zombie? should_be_finished? && - (finish_predicted_at + 60.days) < Time.zone.today + (finish_predicted_at + 90.days) < Time.zone.today end def should_be_finished? From 87b04f60f7a1af430fd38be8a32d647fa7a778fb Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 13:03:46 +1200 Subject: [PATCH 20/41] Order garden's plantings by planted_at --- app/views/gardens/_overview.html.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/gardens/_overview.html.haml b/app/views/gardens/_overview.html.haml index 978a6abbf..ed12d0cf5 100644 --- a/app/views/gardens/_overview.html.haml +++ b/app/views/gardens/_overview.html.haml @@ -13,8 +13,8 @@ .col-md-10 .row - if garden.plantings.current.size.positive? - - garden.plantings.current.includes(:crop, :photos).each do |planting| - .col-md-2.col-sm-6.col-xs-6 + - garden.plantings.current.order(created_at: :desc).includes(:crop, :photos).each do |planting| + .col-md-2.col-sm-4.col-xs-6 = render "plantings/thumbnail", planting: planting - else .col-md-2.col-sm-6.col-xs-6 no plantings From 9b16abd373f5d88cb1ec1cb56c2f6822d504ca80 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 13:04:31 +1200 Subject: [PATCH 21/41] Render perenial progress bar the same as others --- app/views/plantings/_progress.html.haml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/views/plantings/_progress.html.haml b/app/views/plantings/_progress.html.haml index 819cfe7af..caa42bf2a 100644 --- a/app/views/plantings/_progress.html.haml +++ b/app/views/plantings/_progress.html.haml @@ -1,6 +1,5 @@ - if planting.crop.perennial - %p - %i perennial + = render "plantings/progress_bar", status: "perennial", progress: nil - elsif !planting.planted? - if show_explanation %p Progress: 0% - not planted yet From aae7688a0ee05d2090325c2c2e44dea809db63f0 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 13:05:15 +1200 Subject: [PATCH 22/41] Classes for planting badge colours --- app/views/plantings/_badges.html.haml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml index 103cf156a..7ec837484 100644 --- a/app/views/plantings/_badges.html.haml +++ b/app/views/plantings/_badges.html.haml @@ -1,17 +1,18 @@ // Finish times - if planting.finish_is_predicatable? - if planting.zombie? - %span.badge.badge-danger zombie + %span.badge.badge-zombie zombie + = render 'shared/buttons/finish_planting', planting: planting - elsif !planting.should_be_finished? - %span.badge.badge-light + %span.badge = days_from_now_to_finished(planting) days until finished // Harvest times - unless planting.zombie? - if planting.harvest_time? - %span.badge.badge-light harvest time + %span.badge.badge-harvest harvesting now - elsif planting.before_harvest_time? - %span.badge.badge-light + %span.badge = days_from_now_to_first_harvest(planting) days until harvest From 9966eddd376fc49bc9223cf4da9bfeead0151b4f Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 13:52:49 +1200 Subject: [PATCH 23/41] Giving gardens unique names to stop test collisions --- spec/factories/garden.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/garden.rb b/spec/factories/garden.rb index 1f3d7461d..f93107361 100644 --- a/spec/factories/garden.rb +++ b/spec/factories/garden.rb @@ -1,6 +1,6 @@ FactoryBot.define do factory :garden do - name 'Springfield Community Garden' + name { Faker::Vehicle.vin } description "This is a **totally** cool garden" owner active true From 548fb3d3fd5a10049c37e8cc5ccea44f8467ae41 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 14:00:00 +1200 Subject: [PATCH 24/41] Badges feature specs --- spec/features/gardens/gardens_index_spec.rb | 69 ++++++++++++++++++++- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/spec/features/gardens/gardens_index_spec.rb b/spec/features/gardens/gardens_index_spec.rb index da8eeeac2..516796641 100644 --- a/spec/features/gardens/gardens_index_spec.rb +++ b/spec/features/gardens/gardens_index_spec.rb @@ -3,8 +3,7 @@ require 'custom_matchers' feature "Gardens#index", :js do context "Logged in as member" do - let(:member) { FactoryBot.create :member } - + let(:member) { FactoryBot.create :member, login_name: 'shadow' } background { login_as member } context "with 10 gardens" do @@ -67,4 +66,70 @@ feature "Gardens#index", :js do end end end + + describe 'badges' do + let(:garden) { member.gardens.first } + let(:member) { FactoryBot.create :member, login_name: 'robbieburns' } + let(:crop) { FactoryBot.create :crop } + before(:each) do + # time to harvest = 50 day + # time to finished = 90 days + FactoryBot.create(:harvest, + harvested_at: 50.days.ago, + crop: crop, + planting: FactoryBot.create(:planting, + crop: crop, + planted_at: 100.days.ago, + finished_at: 10.days.ago)) + crop.plantings.each(&:update_harvest_days!) + crop.update_lifespan_medians + crop.update_harvest_medians + + garden.update! name: 'super awesome garden' + assert planting + visit gardens_path(owner: member.login_name) + end + + describe 'harvest still growing' do + let!(:planting) do + FactoryBot.create :planting, + crop: crop, + owner: member, + garden: garden, + planted_at: Time.zone.today + end + it { expect(page).to have_link href: planting_path(planting) } + it { expect(page).to have_link href: garden_path(planting.garden) } + it { expect(page).to have_text '50 days until harvest' } + it { expect(page).to have_text '90 days until finished' } + it { expect(page).not_to have_text 'harvesting now' } + end + + describe 'harvesting now' do + let!(:planting) do + FactoryBot.create :planting, + crop: crop, + owner: member, garden: garden, + planted_at: 51.days.ago + end + it { expect(crop.median_days_to_first_harvest).to eq 50 } + it { expect(crop.median_lifespan).to eq 90 } + + it { expect(page).to have_text 'harvesting now' } + it { expect(page).to have_text '39 days until finished' } + it { expect(page).not_to have_text 'days until harvest' } + end + + describe 'zombie' do + let!(:planting) do + FactoryBot.create :planting, + crop: crop, owner: member, + garden: garden, planted_at: 260.days.ago + end + it { expect(page).to have_text 'zombie' } + it { expect(page).not_to have_text 'harvesting now' } + it { expect(page).not_to have_text 'days until harvest' } + it { expect(page).not_to have_text 'days until finished' } + end + end end From 2e4378f025c3bb6cefb8b97182a47572bc084ace Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 14:17:50 +1200 Subject: [PATCH 25/41] Add css class to show finished/zombie/harvesting etc --- app/assets/stylesheets/overrides.sass | 4 ++++ app/helpers/plantings_helper.rb | 10 ++++++++++ app/views/plantings/_thumbnail.html.haml | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/assets/stylesheets/overrides.sass b/app/assets/stylesheets/overrides.sass index 3f7bbdebb..5bde11d05 100644 --- a/app/assets/stylesheets/overrides.sass +++ b/app/assets/stylesheets/overrides.sass @@ -101,6 +101,10 @@ p.stats .badge-harvest background-color: $blue +.planting-zombie +.planting-predicted-finished + background-color: $beige + .planting .planting-badges position: absolute diff --git a/app/helpers/plantings_helper.rb b/app/helpers/plantings_helper.rb index 1bf216330..b57a1dc4a 100644 --- a/app/helpers/plantings_helper.rb +++ b/app/helpers/plantings_helper.rb @@ -42,4 +42,14 @@ module PlantingsHelper return unless planting.planted_at.present? && planting.first_harvest_predicted_at.present? (planting.first_harvest_predicted_at - Time.zone.today).to_i end + + def planting_classes(planting) + classes = [] + classes << 'planting-growing' if planting.growing? + classes << 'planting-finished' if planting.finished? + classes << 'planting-harvest-time' if planting.harvest_time? + classes << 'planting-predicted-finished' if planting.should_be_finished? + classes << 'planting-zombie' if planting.zombie? + classes.join(' ') + end end diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index 9d35f5c0f..6b8400b46 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -3,7 +3,7 @@ = render 'plantings/badges', planting: planting .hover-wrapper .thumbnail - .planting-thumbnail + .planting-thumbnail{class: planting_classes(planting)} = link_to image_tag(planting_image_path(planting), alt: planting.crop.name, class: 'img'), planting_path(planting) = render 'plantings/progress', planting: planting, show_explanation: false .planting-name From 948c32d1d5f2682dbdc1a8532069b1e36e0969d4 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 14:19:40 +1200 Subject: [PATCH 26/41] test that expects a garden name, set to that name --- spec/models/planting_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 0bd36f19f..86da90d9f 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -135,7 +135,7 @@ describe Planting do end end describe 'planting has no harvests' do - let(:planting) { FactoryBot.create :planting } + let(:planting) { FactoryBot.create :planting, name: 'Springfield Community Garden' } before do planting.update_harvest_days! planting.crop.update_harvest_medians From 5cc647ee9b2830e75e8eb340f4ac0ed45d48819b Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 14:22:20 +1200 Subject: [PATCH 27/41] test that expects a garden name, set to that name --- spec/models/garden_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/garden_spec.rb b/spec/models/garden_spec.rb index f8e5a3311..839ef3445 100644 --- a/spec/models/garden_spec.rb +++ b/spec/models/garden_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' describe Garden do let(:owner) { FactoryBot.create(:member) } - let(:garden) { FactoryBot.create(:garden, owner: owner) } + let(:garden) { FactoryBot.create(:garden, owner: owner, name: 'Springfield Community Garden') } it "should have a slug" do garden.slug.should match(/member\d+-springfield-community-garden/) From 87d43f1eb23884ac4723d2199c67863537890474 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 14:26:27 +1200 Subject: [PATCH 28/41] Move planting delete action into the bar --- app/views/plantings/_actions.html.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/plantings/_actions.html.haml b/app/views/plantings/_actions.html.haml index 87eddc8cf..6f5b1b93d 100644 --- a/app/views/plantings/_actions.html.haml +++ b/app/views/plantings/_actions.html.haml @@ -8,5 +8,5 @@ = render 'shared/buttons/harvest_planting', planting: planting = render 'shared/buttons/save_seeds', planting: planting - - if can? :destroy, planting - = render 'shared/buttons/delete', path: planting + - if can? :destroy, planting + = render 'shared/buttons/delete', path: planting From 34b6b546160736621687eb7eb975dfe65bfcc616 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 14:33:39 +1200 Subject: [PATCH 29/41] Name goes on garden, not planting --- spec/models/planting_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 86da90d9f..809ac845d 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' describe Planting do let(:crop) { FactoryBot.create(:tomato) } let(:garden_owner) { FactoryBot.create(:member) } - let(:garden) { FactoryBot.create(:garden, owner: garden_owner) } + let(:garden) { FactoryBot.create(:garden, owner: garden_owner, name: 'Springfield Community Garden') } let(:planting) { FactoryBot.create(:planting, crop: crop, garden: garden, owner: garden.owner) } let(:finished_planting) do FactoryBot.create :planting, planted_at: 4.days.ago, finished_at: 2.days.ago, finished: true @@ -135,7 +135,7 @@ describe Planting do end end describe 'planting has no harvests' do - let(:planting) { FactoryBot.create :planting, name: 'Springfield Community Garden' } + let(:planting) { FactoryBot.create :planting } before do planting.update_harvest_days! planting.crop.update_harvest_medians From 6077d4da3472030d24b03147bd48fa2d6e7974fd Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 15:01:33 +1200 Subject: [PATCH 30/41] Fixed bug in garden spec --- spec/features/harvests/harvesting_a_crop_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/features/harvests/harvesting_a_crop_spec.rb b/spec/features/harvests/harvesting_a_crop_spec.rb index e99c5e814..5f6d85fa2 100644 --- a/spec/features/harvests/harvesting_a_crop_spec.rb +++ b/spec/features/harvests/harvesting_a_crop_spec.rb @@ -130,7 +130,7 @@ feature "Harvesting a crop", :js, :elasticsearch do end scenario "linking to a planting" do - expect(page).to have_content planting.to_s + expect(page).to have_content existing_planting.to_s choose("harvest_planting_id_#{existing_planting.id}") click_button "save" expect(page).to have_link(href: planting_path(existing_planting)) From 4df3354020adf9b4819b8d417e78ae8ce0e62b2d Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 15:17:17 +1200 Subject: [PATCH 31/41] Style fix up --- app/views/plantings/_thumbnail.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index 6b8400b46..ccb3bb4b0 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -3,7 +3,7 @@ = render 'plantings/badges', planting: planting .hover-wrapper .thumbnail - .planting-thumbnail{class: planting_classes(planting)} + .planting-thumbnail{ class: planting_classes(planting) } = link_to image_tag(planting_image_path(planting), alt: planting.crop.name, class: 'img'), planting_path(planting) = render 'plantings/progress', planting: planting, show_explanation: false .planting-name From 4ad4dd0c7fb8bf3f522e4ddd87957f26092b4791 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 15:17:51 +1200 Subject: [PATCH 32/41] Style fix up --- app/models/concerns/predict_planting.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/predict_planting.rb b/app/models/concerns/predict_planting.rb index e0086d959..8298455d7 100644 --- a/app/models/concerns/predict_planting.rb +++ b/app/models/concerns/predict_planting.rb @@ -16,7 +16,7 @@ module PredictPlanting # days def expected_lifespan - actual_lifespan.present? ? actual_lifespan : crop.median_lifespan + actual_lifespan.presence || crop.median_lifespan end def actual_lifespan From e103e6be35e1cb5fbdbe56da753d296793500e77 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 15:32:04 +1200 Subject: [PATCH 33/41] Reducing complexity in planting predictions code --- app/models/concerns/predict_planting.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/predict_planting.rb b/app/models/concerns/predict_planting.rb index 8298455d7..52be89be6 100644 --- a/app/models/concerns/predict_planting.rb +++ b/app/models/concerns/predict_planting.rb @@ -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 From ef9bb01fc984c6e2ee5f12baf1707d38fd28d3bb Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 15:34:18 +1200 Subject: [PATCH 34/41] Wrapped a long line --- app/views/plantings/_thumbnail.html.haml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index ccb3bb4b0..a6b8349b8 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -4,7 +4,8 @@ .hover-wrapper .thumbnail .planting-thumbnail{ class: planting_classes(planting) } - = link_to image_tag(planting_image_path(planting), alt: planting.crop.name, class: 'img'), planting_path(planting) + = link_to image_tag(planting_image_path(planting), + alt: planting.crop.name, class: 'img'), planting_path(planting) = render 'plantings/progress', planting: planting, show_explanation: false .planting-name = link_to planting.crop.name, planting From 34145f7b640924d5d6b60d9aaf55d42d657b5a21 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 16:19:51 +1200 Subject: [PATCH 35/41] Translation of string in haml --- app/views/plantings/_badges.html.haml | 8 ++++---- config/locales/en.yml | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml index 7ec837484..b9e228e20 100644 --- a/app/views/plantings/_badges.html.haml +++ b/app/views/plantings/_badges.html.haml @@ -1,18 +1,18 @@ // Finish times - if planting.finish_is_predicatable? - if planting.zombie? - %span.badge.badge-zombie zombie + %span.badge.badge-zombie=t('.zombie') = render 'shared/buttons/finish_planting', planting: planting - elsif !planting.should_be_finished? %span.badge = days_from_now_to_finished(planting) - days until finished + = t('.days_until_finished') // Harvest times - unless planting.zombie? - if planting.harvest_time? - %span.badge.badge-harvest harvesting now + %span.badge.badge-harvest= t('.harvesting_now') - elsif planting.before_harvest_time? %span.badge = days_from_now_to_first_harvest(planting) - days until harvest + = t('.days_until_harvest') diff --git a/config/locales/en.yml b/config/locales/en.yml index d5d3364a6..d5a22d196 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -52,6 +52,11 @@ en: location_helper: If you have a location set in your profile, it will be used when you create a new garden. location: "%{owner}'s %{garden}" updated: Garden was successfully updated. + overview: + gardensphoto: gardens/photo + plantingsthumbnail: plantings/thumbnail + no_plantings: no plantings + gardensactions: gardens/actions harvests: created: Harvest was successfully created. index: @@ -189,6 +194,12 @@ en: default: Everyone's plantings owner_plantings: "%{owner} plantings" string: "%{crop} planting in %{garden} by %{owner}" + badges: + zombie: zombie + sharedbuttonsfinish_planting: shared/buttons/finish_planting + days_until_finished: days until finished + harvesting_now: harvesting now + days_until_harvest: days until harvest posts: index: title: From 78f021c2f28980ec51c68d3a50ae26931415ed66 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 16:26:40 +1200 Subject: [PATCH 36/41] style fix up --- app/views/plantings/_badges.html.haml | 2 +- app/views/plantings/_progress.html.haml | 2 +- config/locales/en.yml | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml index b9e228e20..a75ccb3ee 100644 --- a/app/views/plantings/_badges.html.haml +++ b/app/views/plantings/_badges.html.haml @@ -1,7 +1,7 @@ // Finish times - if planting.finish_is_predicatable? - if planting.zombie? - %span.badge.badge-zombie=t('.zombie') + %span.badge.badge-zombie= t('.zombie') = render 'shared/buttons/finish_planting', planting: planting - elsif !planting.should_be_finished? %span.badge diff --git a/app/views/plantings/_progress.html.haml b/app/views/plantings/_progress.html.haml index caa42bf2a..5d94de1c3 100644 --- a/app/views/plantings/_progress.html.haml +++ b/app/views/plantings/_progress.html.haml @@ -2,7 +2,7 @@ = render "plantings/progress_bar", status: "perennial", progress: nil - elsif !planting.planted? - if show_explanation - %p Progress: 0% - not planted yet + %p= t('.progress_0_not_planted_yet') = render "plantings/progress_bar", status: "not planted", progress: 0 - elsif planting.finished? = render "plantings/progress_bar", status: 'finished', progress: 100 diff --git a/config/locales/en.yml b/config/locales/en.yml index d5a22d196..be173d985 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -200,6 +200,8 @@ en: days_until_finished: days until finished harvesting_now: harvesting now days_until_harvest: days until harvest + progress: + progress_0_not_planted_yet: 'Progress: 0% - not planted yet' posts: index: title: From e5cf8b6091e1c29624f8601e992f16f0f8e61fcc Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 17:25:04 +1200 Subject: [PATCH 37/41] added spec for finished_predicted_at --- spec/models/planting_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 809ac845d..6d8e90605 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -60,9 +60,12 @@ describe Planting do describe 'planting 30 days ago, not finished' do let(:planting) { FactoryBot.create :planting, planted_at: 30.days.ago } - # 30 / 50 + # 30 / 50 = 60% it { expect(planting.percentage_grown).to eq 60.0 } + # planted 30 days ago it { expect(planting.days_since_planted).to eq 30 } + # means 20 days to go + it { expect(planting.finish_predicted_at).to eq Time.zone.today + 20.days } end describe 'planting not planted yet' do From 6677f8641f7643384d5f485af1035c0ad41024c7 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 17:40:22 +1200 Subject: [PATCH 38/41] If not crop lifespan data, use parent data --- app/models/concerns/predict_planting.rb | 29 ++++++++++++++++++++----- spec/models/planting_spec.rb | 14 ++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/predict_planting.rb b/app/models/concerns/predict_planting.rb index 52be89be6..006f51ed6 100644 --- a/app/models/concerns/predict_planting.rb +++ b/app/models/concerns/predict_planting.rb @@ -11,12 +11,24 @@ module PredictPlanting # dates def finish_predicted_at - planted_at + crop.median_lifespan.days if crop.median_lifespan.present? && planted_at.present? + if planted_at.blank? + nil + elsif crop.median_lifespan.present? + planted_at + crop.median_lifespan.days + elsif crop.parent.present? && crop.parent.median_lifespan.present? + planted_at + crop.parent.median_lifespan.days + end end # days def expected_lifespan - actual_lifespan.presence || crop.median_lifespan + if actual_lifespan.present? + actual_lifespan + elsif crop.median_lifespan.present? + crop.median_lifespan + elsif crop.parent.present? && crop.parent.median_lifespan.present? + crop.parent.median_lifespan + end end def actual_lifespan @@ -30,10 +42,15 @@ module PredictPlanting # progress def percentage_grown - return 100 if finished? - return unless finish_is_predicatable? - return calculate_percentage_grown if growing? - 0 if planted? + if finished? + 100 + elsif !finish_is_predicatable? + nil + elsif growing? + calculate_percentage_grown + elsif planted? + 0 + end end # states diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 6d8e90605..6b49f1c0a 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -68,6 +68,20 @@ describe Planting do it { expect(planting.finish_predicted_at).to eq Time.zone.today + 20.days } end + describe 'child crop uses parent data' do + let(:child_crop) { FactoryBot.create :crop, parent: crop, name: 'child' } + let(:child_planting) { FactoryBot.create :planting, crop: child_crop, planted_at: 30.days.ago } + + # not data for this crop + it { expect(child_crop.median_lifespan).to eq nil } + # 30 / 50 = 60% + it { expect(child_planting.percentage_grown).to eq 60.0 } + # planted 30 days ago + it { expect(child_planting.days_since_planted).to eq 30 } + # means 20 days to go + it { expect(child_planting.finish_predicted_at).to eq Time.zone.today + 20.days } + end + describe 'planting not planted yet' do let(:planting) { FactoryBot.create :planting, planted_at: nil, finished_at: nil } From caa5bcec4b44a21b7fcadfea9ad8ca5ee9b90f90 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 21 Apr 2018 21:55:33 +1200 Subject: [PATCH 39/41] Only show finish button if can edit planting --- app/views/plantings/_badges.html.haml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml index a75ccb3ee..cb2019374 100644 --- a/app/views/plantings/_badges.html.haml +++ b/app/views/plantings/_badges.html.haml @@ -2,7 +2,8 @@ - if planting.finish_is_predicatable? - if planting.zombie? %span.badge.badge-zombie= t('.zombie') - = render 'shared/buttons/finish_planting', planting: planting + - if can? :edit, planting + = render 'shared/buttons/finish_planting', planting: planting - elsif !planting.should_be_finished? %span.badge = days_from_now_to_finished(planting) From db0c60be0df9c42486232cb4095eecdd229772b5 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 23 Apr 2018 13:33:38 +1200 Subject: [PATCH 40/41] Rename zombie to super_late --- app/assets/stylesheets/overrides.sass | 4 ++-- app/helpers/plantings_helper.rb | 2 +- app/models/concerns/predict_planting.rb | 3 ++- app/views/plantings/_badges.html.haml | 6 +++--- config/locales/en.yml | 2 +- spec/features/gardens/gardens_index_spec.rb | 4 ++-- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/assets/stylesheets/overrides.sass b/app/assets/stylesheets/overrides.sass index 920dbd5d4..3c44ffa62 100644 --- a/app/assets/stylesheets/overrides.sass +++ b/app/assets/stylesheets/overrides.sass @@ -115,12 +115,12 @@ p.stats .progress border-radius: 0 -.badge-zombie +.badge-super-late background-color: $red .badge-harvest background-color: $blue -.planting-zombie +.planting-super-late .planting-predicted-finished background-color: $beige diff --git a/app/helpers/plantings_helper.rb b/app/helpers/plantings_helper.rb index b57a1dc4a..dd96ce5fa 100644 --- a/app/helpers/plantings_helper.rb +++ b/app/helpers/plantings_helper.rb @@ -49,7 +49,7 @@ module PlantingsHelper classes << 'planting-finished' if planting.finished? classes << 'planting-harvest-time' if planting.harvest_time? classes << 'planting-predicted-finished' if planting.should_be_finished? - classes << 'planting-zombie' if planting.zombie? + classes << 'planting-super-late' if planting.super_late? classes.join(' ') end end diff --git a/app/models/concerns/predict_planting.rb b/app/models/concerns/predict_planting.rb index 006f51ed6..9cc0e5521 100644 --- a/app/models/concerns/predict_planting.rb +++ b/app/models/concerns/predict_planting.rb @@ -58,7 +58,8 @@ module PredictPlanting crop.annual? && planted_at.present? && finish_predicted_at.present? end - def zombie? + # Planting has live more then 90 days past predicted finish + def super_late? should_be_finished? && (finish_predicted_at + 90.days) < Time.zone.today end diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml index cb2019374..561a22538 100644 --- a/app/views/plantings/_badges.html.haml +++ b/app/views/plantings/_badges.html.haml @@ -1,7 +1,7 @@ // Finish times - if planting.finish_is_predicatable? - - if planting.zombie? - %span.badge.badge-zombie= t('.zombie') + - if planting.super_late? + %span.badge.badge-super late= t('.super late') - if can? :edit, planting = render 'shared/buttons/finish_planting', planting: planting - elsif !planting.should_be_finished? @@ -10,7 +10,7 @@ = t('.days_until_finished') // Harvest times -- unless planting.zombie? +- unless planting.super_late? - if planting.harvest_time? %span.badge.badge-harvest= t('.harvesting_now') - elsif planting.before_harvest_time? diff --git a/config/locales/en.yml b/config/locales/en.yml index be173d985..3cc1a947a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -195,7 +195,7 @@ en: owner_plantings: "%{owner} plantings" string: "%{crop} planting in %{garden} by %{owner}" badges: - zombie: zombie + super late: super late sharedbuttonsfinish_planting: shared/buttons/finish_planting days_until_finished: days until finished harvesting_now: harvesting now diff --git a/spec/features/gardens/gardens_index_spec.rb b/spec/features/gardens/gardens_index_spec.rb index 516796641..0a973e305 100644 --- a/spec/features/gardens/gardens_index_spec.rb +++ b/spec/features/gardens/gardens_index_spec.rb @@ -120,13 +120,13 @@ feature "Gardens#index", :js do it { expect(page).not_to have_text 'days until harvest' } end - describe 'zombie' do + describe 'super late' do let!(:planting) do FactoryBot.create :planting, crop: crop, owner: member, garden: garden, planted_at: 260.days.ago end - it { expect(page).to have_text 'zombie' } + it { expect(page).to have_text 'super late' } it { expect(page).not_to have_text 'harvesting now' } it { expect(page).not_to have_text 'days until harvest' } it { expect(page).not_to have_text 'days until finished' } From fc46685ab97828930f6a96dc04132a871f71e4db Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 23 Apr 2018 13:44:53 +1200 Subject: [PATCH 41/41] Renamed should_be_finshed? to late? --- app/assets/stylesheets/overrides.sass | 2 +- app/helpers/plantings_helper.rb | 2 +- app/models/concerns/predict_planting.rb | 5 ++--- app/views/plantings/_badges.html.haml | 9 +++++---- app/views/shared/buttons/_finish_planting.html.haml | 2 +- config/locales/en.yml | 3 ++- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/app/assets/stylesheets/overrides.sass b/app/assets/stylesheets/overrides.sass index 3c44ffa62..b255536ea 100644 --- a/app/assets/stylesheets/overrides.sass +++ b/app/assets/stylesheets/overrides.sass @@ -121,7 +121,7 @@ p.stats background-color: $blue .planting-super-late -.planting-predicted-finished +.planting-late background-color: $beige .planting diff --git a/app/helpers/plantings_helper.rb b/app/helpers/plantings_helper.rb index dd96ce5fa..4ea42f43e 100644 --- a/app/helpers/plantings_helper.rb +++ b/app/helpers/plantings_helper.rb @@ -48,7 +48,7 @@ module PlantingsHelper classes << 'planting-growing' if planting.growing? classes << 'planting-finished' if planting.finished? classes << 'planting-harvest-time' if planting.harvest_time? - classes << 'planting-predicted-finished' if planting.should_be_finished? + classes << 'planting-late' if planting.late? classes << 'planting-super-late' if planting.super_late? classes.join(' ') end diff --git a/app/models/concerns/predict_planting.rb b/app/models/concerns/predict_planting.rb index 9cc0e5521..ff66168d8 100644 --- a/app/models/concerns/predict_planting.rb +++ b/app/models/concerns/predict_planting.rb @@ -60,11 +60,10 @@ module PredictPlanting # Planting has live more then 90 days past predicted finish def super_late? - should_be_finished? && - (finish_predicted_at + 90.days) < Time.zone.today + late? && (finish_predicted_at + 90.days) < Time.zone.today end - def should_be_finished? + def late? crop.annual? && !finished && planted_at.present? && finish_predicted_at.present? && diff --git a/app/views/plantings/_badges.html.haml b/app/views/plantings/_badges.html.haml index 561a22538..2d690b914 100644 --- a/app/views/plantings/_badges.html.haml +++ b/app/views/plantings/_badges.html.haml @@ -1,10 +1,11 @@ // Finish times - if planting.finish_is_predicatable? - if planting.super_late? - %span.badge.badge-super late= t('.super late') - - if can? :edit, planting - = render 'shared/buttons/finish_planting', planting: planting - - elsif !planting.should_be_finished? + %span.badge.badge-super-late= t('.super_late') + = render 'shared/buttons/finish_planting', planting: planting + - elsif planting.late? + %span.badge.badge-late= t('.late_finishing') + - else %span.badge = days_from_now_to_finished(planting) = t('.days_until_finished') diff --git a/app/views/shared/buttons/_finish_planting.html.haml b/app/views/shared/buttons/_finish_planting.html.haml index 9a3b6ce75..7b1ba8319 100644 --- a/app/views/shared/buttons/_finish_planting.html.haml +++ b/app/views/shared/buttons/_finish_planting.html.haml @@ -1,4 +1,4 @@ -- unless planting.finished +- if can?(:edit, planting) && !planting.finished = link_to planting_path(planting, planting: { finished: 1 }), method: :put, class: 'btn btn-default btn-xs append-date' do %span.glyphicon.glyphicon-ok{ title: "Finished" } diff --git a/config/locales/en.yml b/config/locales/en.yml index 3cc1a947a..09d5687f8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -195,7 +195,8 @@ en: owner_plantings: "%{owner} plantings" string: "%{crop} planting in %{garden} by %{owner}" badges: - super late: super late + late_finishing: late finishing + super_late: super late sharedbuttonsfinish_planting: shared/buttons/finish_planting days_until_finished: days until finished harvesting_now: harvesting now