mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-08 07:43:49 -04:00
"Changed last_harvest" to "days_to_last_harvest"
This commit is contained in:
@@ -222,7 +222,7 @@ class Crop < ActiveRecord::Base
|
||||
|
||||
def update_harvest_medians
|
||||
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))
|
||||
update(median_days_to_last_harvest: Planting.where(crop: self).median(:days_to_last_harvest))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -116,14 +116,14 @@ class Planting < ActiveRecord::Base
|
||||
def update_harvest_days
|
||||
harvests_with_dates = harvests.where.not(harvested_at: nil)
|
||||
days_to_first_harvest = nil
|
||||
last_harvest = nil
|
||||
days_to_last_harvest = nil
|
||||
if planted_at.present? && harvests_with_dates.size.positive?
|
||||
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).first.harvested_at - planted_at).to_i
|
||||
days_to_last_harvest = (harvests_with_dates.order(harvested_at: :desc).first.harvested_at - planted_at).to_i
|
||||
end
|
||||
end
|
||||
update(days_to_first_harvest: days_to_first_harvest, last_harvest: last_harvest)
|
||||
update(days_to_first_harvest: days_to_first_harvest, days_to_last_harvest: days_to_last_harvest)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -24,10 +24,10 @@
|
||||
%b= crop.median_days_to_first_harvest
|
||||
days after planting
|
||||
|
||||
- if crop.annual? && crop.median_last_harvest.present?
|
||||
- if crop.annual? && crop.median_days_to_last_harvest.present?
|
||||
%p
|
||||
Last harvest expected
|
||||
%b= crop.median_last_harvest
|
||||
%b= crop.median_days_to_last_harvest
|
||||
days after planting
|
||||
|
||||
- if can? :create, Planting
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
%b= crop.median_days_to_first_harvest
|
||||
days after planting
|
||||
|
||||
- if crop.perennial == false && crop.median_last_harvest.present?
|
||||
- if crop.perennial == false && crop.median_days_to_last_harvest.present?
|
||||
%p
|
||||
Last harvest expected
|
||||
%b= crop.median_last_harvest
|
||||
%b= crop.median_days_to_last_harvest
|
||||
days after planting
|
||||
|
||||
@@ -59,9 +59,9 @@
|
||||
- if @planting.days_to_first_harvest.present?
|
||||
%dt First Harvest:
|
||||
%dd #{@planting.days_to_first_harvest} days after planting
|
||||
- if @planting.last_harvest.present?
|
||||
- if @planting.days_to_last_harvest.present?
|
||||
%dt Last Harvest:
|
||||
%dd #{@planting.last_harvest} days after planting
|
||||
%dd #{@planting.days_to_last_harvest} days after planting
|
||||
|
||||
%p= render 'plantings/harvests', planting: @planting
|
||||
%p= render 'plantings/progress', planting: @planting, show_explanation: true
|
||||
|
||||
@@ -7,12 +7,12 @@ class AllThePredictions < ActiveRecord::Migration
|
||||
|
||||
# how old was planting at first harvest
|
||||
add_column :plantings, :days_to_first_harvest, :integer
|
||||
add_column :plantings, :last_harvest, :integer
|
||||
add_column :plantings, :days_to_last_harvest, :integer
|
||||
|
||||
# Keep the median values for the crop
|
||||
add_column :crops, :median_lifespan, :integer
|
||||
add_column :crops, :median_days_to_first_harvest, :integer
|
||||
add_column :crops, :median_last_harvest, :integer
|
||||
add_column :crops, :median_days_to_last_harvest, :integer
|
||||
|
||||
create_table :median_functions do |t|
|
||||
end
|
||||
|
||||
18
db/schema.rb
18
db/schema.rb
@@ -181,23 +181,23 @@ ActiveRecord::Schema.define(version: 20171105011017) do
|
||||
end
|
||||
|
||||
create_table "crops", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "name", null: false
|
||||
t.string "en_wikipedia_url"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "slug"
|
||||
t.integer "parent_id"
|
||||
t.integer "plantings_count", default: 0
|
||||
t.integer "plantings_count", default: 0
|
||||
t.integer "creator_id"
|
||||
t.integer "requester_id"
|
||||
t.string "approval_status", default: "approved"
|
||||
t.string "approval_status", default: "approved"
|
||||
t.text "reason_for_rejection"
|
||||
t.text "request_notes"
|
||||
t.text "rejection_notes"
|
||||
t.boolean "perennial", default: false
|
||||
t.boolean "perennial", default: false
|
||||
t.integer "median_lifespan"
|
||||
t.integer "median_days_to_first_harvest"
|
||||
t.integer "median_last_harvest"
|
||||
t.integer "median_days_to_last_harvest"
|
||||
end
|
||||
|
||||
add_index "crops", ["name"], name: "index_crops_on_name", using: :btree
|
||||
@@ -416,8 +416,8 @@ ActiveRecord::Schema.define(version: 20171105011017) do
|
||||
end
|
||||
|
||||
create_table "plantings", force: :cascade do |t|
|
||||
t.integer "garden_id", null: false
|
||||
t.integer "crop_id", null: false
|
||||
t.integer "garden_id", null: false
|
||||
t.integer "crop_id", null: false
|
||||
t.date "planted_at"
|
||||
t.integer "quantity"
|
||||
t.text "description"
|
||||
@@ -427,12 +427,12 @@ ActiveRecord::Schema.define(version: 20171105011017) do
|
||||
t.string "sunniness"
|
||||
t.string "planted_from"
|
||||
t.integer "owner_id"
|
||||
t.boolean "finished", default: false
|
||||
t.boolean "finished", default: false
|
||||
t.date "finished_at"
|
||||
t.integer "days_before_maturity"
|
||||
t.integer "lifespan"
|
||||
t.integer "days_to_first_harvest"
|
||||
t.integer "last_harvest"
|
||||
t.integer "days_to_last_harvest"
|
||||
end
|
||||
|
||||
add_index "plantings", ["slug"], name: "index_plantings_on_slug", unique: true, using: :btree
|
||||
|
||||
@@ -83,9 +83,9 @@ describe Planting do
|
||||
context 'no data' do
|
||||
let(:planting) { FactoryBot.create :planting }
|
||||
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.crop.median_days_to_last_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.days_to_last_harvest).to eq(nil) }
|
||||
it { expect(planting.expected_lifespan).to eq(nil) }
|
||||
end
|
||||
context 'lots of data' do
|
||||
@@ -115,7 +115,7 @@ describe Planting do
|
||||
end
|
||||
let(:planting) { FactoryBot.create :planting }
|
||||
it { expect(planting.days_to_first_harvest).to eq(nil) }
|
||||
it { expect(planting.last_harvest).to eq(nil) }
|
||||
it { expect(planting.days_to_last_harvest).to eq(nil) }
|
||||
end
|
||||
describe 'planting has first harvest' do
|
||||
let(:planting) { FactoryBot.create :planting, planted_at: 100.days.ago }
|
||||
@@ -125,9 +125,9 @@ describe Planting do
|
||||
planting.crop.update_harvest_medians
|
||||
end
|
||||
it { expect(planting.days_to_first_harvest).to eq(90) }
|
||||
it { expect(planting.last_harvest).to eq(nil) }
|
||||
it { expect(planting.days_to_last_harvest).to eq(nil) }
|
||||
it { expect(planting.crop.median_days_to_first_harvest).to eq(90) }
|
||||
it { expect(planting.crop.median_last_harvest).to eq(nil) }
|
||||
it { expect(planting.crop.median_days_to_last_harvest).to eq(nil) }
|
||||
end
|
||||
describe 'planting has last harvest' do
|
||||
let(:planting) { FactoryBot.create :planting, planted_at: 100.days.ago, finished_at: 1.day.ago, finished: true }
|
||||
@@ -138,9 +138,9 @@ describe Planting do
|
||||
planting.crop.update_harvest_medians
|
||||
end
|
||||
it { expect(planting.days_to_first_harvest).to eq(10) }
|
||||
it { expect(planting.last_harvest).to eq(90) }
|
||||
it { expect(planting.days_to_last_harvest).to eq(90) }
|
||||
it { expect(planting.crop.median_days_to_first_harvest).to eq(10) }
|
||||
it { expect(planting.crop.median_last_harvest).to eq(90) }
|
||||
it { expect(planting.crop.median_days_to_last_harvest).to eq(90) }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user