From 62beec23a104ce1548db00dbbea7577ced09e2ab Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Wed, 8 Nov 2017 17:17:15 +1300 Subject: [PATCH] "Changed first_harvest" to "days_to_first_harvest" --- app/models/crop.rb | 2 +- app/models/planting.rb | 9 ++++----- app/views/crops/_index_card.html.haml | 4 ++-- app/views/crops/_predictions.html.haml | 4 ++-- app/views/plantings/show.html.haml | 4 ++-- db/migrate/20171022032108_all_the_predictions.rb | 4 ++-- db/schema.rb | 4 ++-- spec/models/planting_spec.rb | 16 ++++++++-------- 8 files changed, 23 insertions(+), 24 deletions(-) diff --git a/app/models/crop.rb b/app/models/crop.rb index 1b76b8af3..d0c6044c5 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -221,7 +221,7 @@ class Crop < ActiveRecord::Base end def update_harvest_medians - update(median_first_harvest: Planting.where(crop: self).median(:first_harvest)) + update(median_days_to_first_harvest: Planting.where(crop: self).median(:days_to_first_harvest)) update(median_last_harvest: Planting.where(crop: self).median(:last_harvest)) end diff --git a/app/models/planting.rb b/app/models/planting.rb index 9923845c3..2f43818fb 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -115,16 +115,15 @@ class Planting < ActiveRecord::Base def update_harvest_days harvests_with_dates = harvests.where.not(harvested_at: nil) - first_harvest = nil + days_to_first_harvest = nil last_harvest = nil if planted_at.present? && harvests_with_dates.size.positive? - # how long ago was this planted - first_harvest = (harvests_with_dates.order(harvested_at: :asc).limit(1).first.harvested_at - planted_at).to_i + days_to_first_harvest = (harvests_with_dates.order(harvested_at: :asc).first.harvested_at - planted_at).to_i if finished? - last_harvest = (harvests_with_dates.order(harvested_at: :desc).limit(1).first.harvested_at - planted_at).to_i + last_harvest = (harvests_with_dates.order(harvested_at: :desc).first.harvested_at - planted_at).to_i end end - update(first_harvest: first_harvest, last_harvest: last_harvest) + update(days_to_first_harvest: days_to_first_harvest, last_harvest: last_harvest) end private diff --git a/app/views/crops/_index_card.html.haml b/app/views/crops/_index_card.html.haml index 92da22965..ae2146ae2 100644 --- a/app/views/crops/_index_card.html.haml +++ b/app/views/crops/_index_card.html.haml @@ -18,10 +18,10 @@ Median Lifespan %b= crop.median_lifespan days - - unless crop.median_first_harvest.nil? + - unless crop.median_days_to_first_harvest.nil? %p First harvest expected - %b= crop.median_first_harvest + %b= crop.median_days_to_first_harvest days after planting - if crop.annual? && crop.median_last_harvest.present? diff --git a/app/views/crops/_predictions.html.haml b/app/views/crops/_predictions.html.haml index 47f69e9b5..982b18898 100644 --- a/app/views/crops/_predictions.html.haml +++ b/app/views/crops/_predictions.html.haml @@ -16,10 +16,10 @@ %b= crop.median_lifespan days -- unless crop.median_first_harvest.nil? +- unless crop.median_days_to_first_harvest.nil? %p First harvest expected - %b= crop.median_first_harvest + %b= crop.median_days_to_first_harvest days after planting - if crop.perennial == false && crop.median_last_harvest.present? diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 59aa52417..54e8a9abe 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -56,9 +56,9 @@ - if @planting.lifespan.present? %dt Actual Lifespan: %dd #{@planting.lifespan} days - - if @planting.first_harvest.present? + - if @planting.days_to_first_harvest.present? %dt First Harvest: - %dd #{@planting.first_harvest} days after planting + %dd #{@planting.days_to_first_harvest} days after planting - if @planting.last_harvest.present? %dt Last Harvest: %dd #{@planting.last_harvest} days after planting diff --git a/db/migrate/20171022032108_all_the_predictions.rb b/db/migrate/20171022032108_all_the_predictions.rb index 4b7b22a88..70b28db28 100644 --- a/db/migrate/20171022032108_all_the_predictions.rb +++ b/db/migrate/20171022032108_all_the_predictions.rb @@ -6,12 +6,12 @@ class AllThePredictions < ActiveRecord::Migration add_column :plantings, :lifespan, :integer # how old was planting at first harvest - add_column :plantings, :first_harvest, :integer + add_column :plantings, :days_to_first_harvest, :integer add_column :plantings, :last_harvest, :integer # Keep the median values for the crop add_column :crops, :median_lifespan, :integer - add_column :crops, :median_first_harvest, :integer + add_column :crops, :median_days_to_first_harvest, :integer add_column :crops, :median_last_harvest, :integer create_table :median_functions do |t| diff --git a/db/schema.rb b/db/schema.rb index 945ce107e..6427edbd7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -196,7 +196,7 @@ ActiveRecord::Schema.define(version: 20171105011017) do t.text "rejection_notes" t.boolean "perennial", default: false t.integer "median_lifespan" - t.integer "median_first_harvest" + t.integer "median_days_to_first_harvest" t.integer "median_last_harvest" end @@ -431,7 +431,7 @@ ActiveRecord::Schema.define(version: 20171105011017) do t.date "finished_at" t.integer "days_before_maturity" t.integer "lifespan" - t.integer "first_harvest" + t.integer "days_to_first_harvest" t.integer "last_harvest" end diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index 866030e8c..8f477d6f4 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -82,9 +82,9 @@ describe Planting do describe 'planting first harvest preductions' do context 'no data' do let(:planting) { FactoryBot.create :planting } - it { expect(planting.crop.median_first_harvest).to eq(nil) } + it { expect(planting.crop.median_days_to_first_harvest).to eq(nil) } it { expect(planting.crop.median_last_harvest).to eq(nil) } - it { expect(planting.first_harvest).to eq(nil) } + it { expect(planting.days_to_first_harvest).to eq(nil) } it { expect(planting.last_harvest).to eq(nil) } it { expect(planting.expected_lifespan).to eq(nil) } end @@ -106,7 +106,7 @@ describe Planting do planting.crop.update_lifespan_medians planting.crop.update_harvest_medians end - it { expect(planting.crop.median_first_harvest).to eq(20) } + it { expect(planting.crop.median_days_to_first_harvest).to eq(20) } end describe 'planting has no harvests' do before do @@ -114,7 +114,7 @@ describe Planting do planting.crop.update_harvest_medians end let(:planting) { FactoryBot.create :planting } - it { expect(planting.first_harvest).to eq(nil) } + it { expect(planting.days_to_first_harvest).to eq(nil) } it { expect(planting.last_harvest).to eq(nil) } end describe 'planting has first harvest' do @@ -124,9 +124,9 @@ describe Planting do planting.update_harvest_days planting.crop.update_harvest_medians end - it { expect(planting.first_harvest).to eq(90) } + it { expect(planting.days_to_first_harvest).to eq(90) } it { expect(planting.last_harvest).to eq(nil) } - it { expect(planting.crop.median_first_harvest).to eq(90) } + it { expect(planting.crop.median_days_to_first_harvest).to eq(90) } it { expect(planting.crop.median_last_harvest).to eq(nil) } end describe 'planting has last harvest' do @@ -137,9 +137,9 @@ describe Planting do planting.update_harvest_days planting.crop.update_harvest_medians end - it { expect(planting.first_harvest).to eq(10) } + it { expect(planting.days_to_first_harvest).to eq(10) } it { expect(planting.last_harvest).to eq(90) } - it { expect(planting.crop.median_first_harvest).to eq(10) } + it { expect(planting.crop.median_days_to_first_harvest).to eq(10) } it { expect(planting.crop.median_last_harvest).to eq(90) } end end