mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-18 21:56:55 -04:00
@@ -37,9 +37,8 @@ Style/AlignParameters:
|
||||
Metrics/LineLength:
|
||||
Max: 120
|
||||
|
||||
|
||||
# See https://github.com/bbatsov/rubocop/issues/3629
|
||||
Rails/HttpPositionalArguments:
|
||||
# turn these back on in Rails 5
|
||||
Rails/HttpPositionalArguments: # See https://github.com/bbatsov/rubocop/issues/3629
|
||||
Enabled: false
|
||||
|
||||
Style/Documentation:
|
||||
@@ -65,9 +64,9 @@ Metrics/BlockLength:
|
||||
Metrics/MethodLength:
|
||||
Max: 34
|
||||
Metrics/AbcSize:
|
||||
Max: 32
|
||||
Max: 31
|
||||
Metrics/ClassLength:
|
||||
Max: 207
|
||||
Max: 179
|
||||
Metrics/CyclomaticComplexity:
|
||||
Max: 11
|
||||
Metrics/PerceivedComplexity:
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
# This configuration was generated by
|
||||
# `rubocop --auto-gen-config --no-offense-counts`
|
||||
# on 2017-05-28 10:57:55 +1200 using RuboCop version 0.47.1.
|
||||
# on 2017-11-05 20:41:45 +1300 using RuboCop version 0.47.1.
|
||||
# The point is for the user to remove these configuration records
|
||||
# one by one as the offenses are removed from the code base.
|
||||
# Note that changes in the inspected code, or installation of new
|
||||
# versions of RuboCop, may require this file to be generated again.
|
||||
|
||||
|
||||
Lint/HandleExceptions:
|
||||
Exclude:
|
||||
- 'lib/tasks/testing.rake'
|
||||
@@ -57,7 +56,6 @@ Rails/TimeZone:
|
||||
- 'spec/factories/member.rb'
|
||||
- 'spec/factories/post.rb'
|
||||
- 'spec/models/post_spec.rb'
|
||||
- 'spec/views/plantings/index.html.haml_spec.rb'
|
||||
|
||||
# Cop supports --auto-correct.
|
||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
||||
@@ -122,13 +120,6 @@ Style/EachForSimpleLoop:
|
||||
- 'spec/models/crop_spec.rb'
|
||||
- 'spec/views/home/_crops.html.haml_spec.rb'
|
||||
|
||||
# Cop supports --auto-correct.
|
||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
||||
# SupportedStyles: compact, expanded
|
||||
Style/EmptyMethod:
|
||||
Exclude:
|
||||
- 'app/controllers/roles_controller.rb'
|
||||
|
||||
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
||||
# SupportedStyles: format, sprintf, percent
|
||||
Style/FormatString:
|
||||
@@ -146,7 +137,6 @@ Style/IdenticalConditionalBranches:
|
||||
Style/IfUnlessModifier:
|
||||
Exclude:
|
||||
- 'app/helpers/crops_helper.rb'
|
||||
- 'app/models/planting.rb'
|
||||
- 'config/initializers/geocoder.rb'
|
||||
- 'lib/tasks/growstuff.rake'
|
||||
|
||||
@@ -165,7 +155,6 @@ Style/MultilineIfModifier:
|
||||
|
||||
Style/MultilineTernaryOperator:
|
||||
Exclude:
|
||||
- 'app/controllers/notifications_controller.rb'
|
||||
- 'app/controllers/order_items_controller.rb'
|
||||
|
||||
# Cop supports --auto-correct.
|
||||
@@ -223,7 +212,6 @@ Style/RegexpLiteral:
|
||||
- 'spec/views/devise/registrations/edit_spec.rb'
|
||||
- 'spec/views/members/index.html.haml_spec.rb'
|
||||
- 'spec/views/posts/index.html.haml_spec.rb'
|
||||
- 'spec/views/posts/show.html.haml_spec.rb'
|
||||
|
||||
# Cop supports --auto-correct.
|
||||
# Configuration parameters: SupportedStyles.
|
||||
@@ -243,4 +231,4 @@ Style/SymbolProc:
|
||||
# SupportedStyles: percent, brackets
|
||||
Style/WordArray:
|
||||
EnforcedStyle: percent
|
||||
MinSize: 5
|
||||
MinSize: 4
|
||||
|
||||
@@ -33,7 +33,7 @@ before_script:
|
||||
if [ "${STATIC_CHECKS}" = "true" ]; then
|
||||
./script/install_linters;
|
||||
else
|
||||
bundle exec rake db:create db:migrate db:test:prepare;
|
||||
RAILS_ENV=test bundle exec rake db:create db:migrate;
|
||||
bundle exec rake assets:precompile;
|
||||
fi
|
||||
- set +e
|
||||
|
||||
3
Gemfile
3
Gemfile
@@ -17,6 +17,9 @@ gem 'font-awesome-sass'
|
||||
|
||||
gem 'uglifier' # JavaScript compressor
|
||||
|
||||
# planting and harvest predictions
|
||||
gem 'active_median'
|
||||
|
||||
gem 'flickraw'
|
||||
gem 'jquery-rails'
|
||||
gem 'jquery-ui-rails', '~> 5.0.2' # needs careful upgrade with change of location
|
||||
|
||||
16
Gemfile.lock
16
Gemfile.lock
@@ -23,6 +23,8 @@ GEM
|
||||
active_link_to (1.0.5)
|
||||
actionpack
|
||||
addressable
|
||||
active_median (0.1.4)
|
||||
activerecord
|
||||
active_merchant-paypal-bogus-gateway (0.1.0)
|
||||
activemerchant
|
||||
active_utils (3.3.9)
|
||||
@@ -93,7 +95,7 @@ GEM
|
||||
capybara-email (2.5.0)
|
||||
capybara (~> 2.4)
|
||||
mail
|
||||
capybara-screenshot (1.0.17)
|
||||
capybara-screenshot (1.0.18)
|
||||
capybara (>= 1.0, < 3)
|
||||
launchy
|
||||
childprocess (0.8.0)
|
||||
@@ -247,7 +249,7 @@ GEM
|
||||
ruby_parser (~> 3.5)
|
||||
httparty (0.15.6)
|
||||
multi_xml (>= 0.5.2)
|
||||
i18n (0.9.0)
|
||||
i18n (0.9.1)
|
||||
concurrent-ruby (~> 1.0)
|
||||
i18n-tasks (0.9.12)
|
||||
activesupport (>= 4.0.2)
|
||||
@@ -305,15 +307,15 @@ GEM
|
||||
crass (~> 1.0.2)
|
||||
nokogiri (>= 1.5.9)
|
||||
lumberjack (1.0.12)
|
||||
mail (2.6.6)
|
||||
mime-types (>= 1.16, < 4)
|
||||
mail (2.7.0)
|
||||
mini_mime (>= 0.1.1)
|
||||
memcachier (0.0.2)
|
||||
method_source (0.9.0)
|
||||
mime-types (3.1)
|
||||
mime-types-data (~> 3.2015)
|
||||
mime-types-data (3.2016.0521)
|
||||
mimemagic (0.3.2)
|
||||
mini_mime (0.1.4)
|
||||
mini_mime (1.0.0)
|
||||
mini_portile2 (2.3.0)
|
||||
minitest (5.10.3)
|
||||
moneta (0.8.1)
|
||||
@@ -473,7 +475,7 @@ GEM
|
||||
sprockets (>= 2.8, < 4.0)
|
||||
sprockets-rails (>= 2.0, < 4.0)
|
||||
tilt (>= 1.1, < 3)
|
||||
selenium-webdriver (3.6.0)
|
||||
selenium-webdriver (3.7.0)
|
||||
childprocess (~> 0.5)
|
||||
rubyzip (~> 1.0)
|
||||
sexp_processor (4.10.0)
|
||||
@@ -537,6 +539,7 @@ PLATFORMS
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
active_median
|
||||
active_merchant-paypal-bogus-gateway
|
||||
active_utils
|
||||
activemerchant
|
||||
@@ -622,6 +625,7 @@ DEPENDENCIES
|
||||
will_paginate
|
||||
xmlrpc
|
||||
|
||||
|
||||
RUBY VERSION
|
||||
ruby 2.4.1p111
|
||||
|
||||
|
||||
@@ -137,7 +137,7 @@ class CropsController < ApplicationController
|
||||
return unless params[param_name].present?
|
||||
destroy_names(name_type)
|
||||
params[param_name].each do |_i, value|
|
||||
create_name!(name_type, value)
|
||||
create_name!(name_type, value) unless value.empty?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -154,6 +154,7 @@ class CropsController < ApplicationController
|
||||
:name,
|
||||
:parent_id,
|
||||
:creator_id,
|
||||
:perennial,
|
||||
:approval_status,
|
||||
:request_notes,
|
||||
:reason_for_rejection,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class HarvestsController < ApplicationController
|
||||
before_action :authenticate_member!, except: [:index, :show]
|
||||
before_action :authenticate_member!, except: %i(index show)
|
||||
after_action :update_crop_medians, only: %i(create update destroy)
|
||||
load_and_authorize_resource
|
||||
respond_to :html, :json
|
||||
respond_to :csv, only: :index
|
||||
@@ -33,6 +34,7 @@ class HarvestsController < ApplicationController
|
||||
|
||||
def create
|
||||
@harvest.crop_id = @harvest.planting.crop_id if @harvest.planting_id
|
||||
@harvest.harvested_at = Time.zone.now if @harvest.harvested_at.blank?
|
||||
@harvest.save
|
||||
respond_with(@harvest)
|
||||
end
|
||||
@@ -71,8 +73,8 @@ class HarvestsController < ApplicationController
|
||||
elsif @planting
|
||||
@planting.harvests
|
||||
else
|
||||
Harvest
|
||||
end.joins(:owner, :crop).paginate(page: params[:page])
|
||||
Harvest.all
|
||||
end.order(harvested_at: :desc).joins(:owner, :crop).paginate(page: params[:page])
|
||||
end
|
||||
|
||||
def csv_filename
|
||||
@@ -83,4 +85,13 @@ class HarvestsController < ApplicationController
|
||||
end
|
||||
"Growstuff-#{specifics}Harvests-#{Time.zone.now.to_s(:number)}.csv"
|
||||
end
|
||||
|
||||
def update_crop_medians
|
||||
# We only update medians to predict plantings
|
||||
# if this harvest is not linked to a planting, then do nothing
|
||||
return if @harvest.planting.nil?
|
||||
|
||||
@harvest.planting.update_harvest_days
|
||||
@harvest.crop.update_harvest_medians
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
class PlantingsController < ApplicationController
|
||||
before_action :authenticate_member!, except: [:index, :show]
|
||||
after_action :expire_homepage, only: [:create, :update, :destroy]
|
||||
before_action :authenticate_member!, except: %i(index show)
|
||||
after_action :expire_homepage, only: %i(create update destroy)
|
||||
after_action :update_crop_medians, only: %i(create update destroy)
|
||||
after_action :update_planting_medians, only: :update
|
||||
load_and_authorize_resource
|
||||
|
||||
respond_to :html, :json
|
||||
@@ -51,13 +53,11 @@ class PlantingsController < ApplicationController
|
||||
def create
|
||||
@planting = Planting.new(planting_params)
|
||||
@planting.owner = current_member
|
||||
@planting.calc_and_set_days_before_maturity
|
||||
@planting.save
|
||||
@planting.save!
|
||||
respond_with @planting
|
||||
end
|
||||
|
||||
def update
|
||||
@planting.calc_and_set_days_before_maturity
|
||||
@planting.update(planting_params)
|
||||
respond_with @planting
|
||||
end
|
||||
@@ -69,6 +69,14 @@ class PlantingsController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def update_crop_medians
|
||||
@planting.crop.update_lifespan_medians
|
||||
end
|
||||
|
||||
def update_planting_medians
|
||||
@planting.update_harvest_days
|
||||
end
|
||||
|
||||
def planting_params
|
||||
params[:planted_at] = parse_date(params[:planted_at]) if params[:planted_at]
|
||||
params.require(:planting).permit(
|
||||
@@ -87,6 +95,9 @@ class PlantingsController < ApplicationController
|
||||
Planting
|
||||
end
|
||||
p = p.current unless @show_all
|
||||
p.joins(:owner, :crop, :garden).order(:created_at).paginate(page: params[:page])
|
||||
p.joins(:owner, :crop, :garden)
|
||||
.includes(:crop, :owner, :garden)
|
||||
.order(:created_at)
|
||||
.paginate(page: params[:page])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,18 +1,6 @@
|
||||
module PlantingsHelper
|
||||
def display_days_before_maturity(planting)
|
||||
# First try to calc from finished/finished_at
|
||||
if planting.finished? || planting.finished_at.present?
|
||||
planting.days_until_finished.to_s
|
||||
# then try to calc from planted at + maturity
|
||||
elsif planting.planted_at.present? && planting.days_before_maturity.present?
|
||||
planting.days_until_mature.to_s
|
||||
else
|
||||
"unknown"
|
||||
end
|
||||
end
|
||||
|
||||
def display_finished(planting)
|
||||
if !planting.finished_at.nil?
|
||||
if planting.finished_at.present?
|
||||
planting.finished_at
|
||||
elsif planting.finished
|
||||
"Yes (no date specified)"
|
||||
|
||||
@@ -2,11 +2,14 @@ class Crop < ActiveRecord::Base
|
||||
extend FriendlyId
|
||||
friendly_id :name, use: [:slugged, :finders]
|
||||
|
||||
has_many :scientific_names, after_add: :update_index, after_remove: :update_index
|
||||
accepts_nested_attributes_for :scientific_names,
|
||||
allow_destroy: true,
|
||||
reject_if: :all_blank
|
||||
##
|
||||
## Triggers
|
||||
before_destroy { |crop| crop.posts.clear }
|
||||
|
||||
##
|
||||
## Relationships
|
||||
has_many :scientific_names, after_add: :update_index, after_remove: :update_index, dependent: :destroy
|
||||
accepts_nested_attributes_for :scientific_names, allow_destroy: true, reject_if: :all_blank
|
||||
has_many :alternate_names, after_add: :update_index, after_remove: :update_index, dependent: :destroy
|
||||
has_many :plantings
|
||||
has_many :photos, through: :plantings
|
||||
@@ -15,33 +18,32 @@ class Crop < ActiveRecord::Base
|
||||
has_many :plant_parts, -> { uniq.reorder("plant_parts.name") }, through: :harvests
|
||||
belongs_to :creator, class_name: 'Member'
|
||||
belongs_to :requester, class_name: 'Member'
|
||||
|
||||
belongs_to :parent, class_name: 'Crop'
|
||||
has_many :varieties, class_name: 'Crop', foreign_key: 'parent_id'
|
||||
has_and_belongs_to_many :posts # rubocop:disable Rails/HasAndBelongsToMany
|
||||
before_destroy { |crop| crop.posts.clear }
|
||||
|
||||
##
|
||||
## Scopes
|
||||
default_scope { order("lower(crops.name) asc") }
|
||||
scope :recent, lambda {
|
||||
approved.reorder("created_at desc")
|
||||
}
|
||||
scope :toplevel, lambda {
|
||||
approved.where(parent_id: nil)
|
||||
}
|
||||
scope :popular, lambda {
|
||||
approved.reorder("plantings_count desc, lower(name) asc")
|
||||
}
|
||||
scope :randomized, lambda {
|
||||
# ok on sqlite and psql, but not on mysql
|
||||
approved.reorder('random()')
|
||||
}
|
||||
scope :recent, -> { approved.reorder("created_at desc") }
|
||||
scope :toplevel, -> { approved.where(parent_id: nil) }
|
||||
scope :popular, -> { approved.reorder("plantings_count desc, lower(name) asc") }
|
||||
# ok on sqlite and psql, but not on mysql
|
||||
scope :randomized, -> { approved.reorder('random()') }
|
||||
scope :pending_approval, -> { where(approval_status: "pending") }
|
||||
scope :approved, -> { where(approval_status: "approved") }
|
||||
scope :rejected, -> { where(approval_status: "rejected") }
|
||||
|
||||
scope :interesting, -> { approved.has_photos.randomized }
|
||||
scope :has_photos, -> { includes(:photos).where.not(photos: { id: nil }) }
|
||||
|
||||
##
|
||||
## Validations
|
||||
# Reasons are only necessary when rejecting
|
||||
validates :reason_for_rejection, presence: true, if: :rejected?
|
||||
## This validation addresses a race condition
|
||||
validate :approval_status_cannot_be_changed_again
|
||||
validate :must_be_rejected_if_rejected_reasons_present
|
||||
validate :must_have_meaningful_reason_for_rejection
|
||||
## Wikipedia urls are only necessary when approving a crop
|
||||
validates :en_wikipedia_url,
|
||||
format: {
|
||||
@@ -50,16 +52,6 @@ class Crop < ActiveRecord::Base
|
||||
},
|
||||
if: :approved?
|
||||
|
||||
## Reasons are only necessary when rejecting
|
||||
validates :reason_for_rejection, presence: true, if: :rejected?
|
||||
|
||||
## This validation addresses a race condition
|
||||
validate :approval_status_cannot_be_changed_again
|
||||
|
||||
validate :must_be_rejected_if_rejected_reasons_present
|
||||
|
||||
validate :must_have_meaningful_reason_for_rejection
|
||||
|
||||
####################################
|
||||
# Elastic search configuration
|
||||
if ENV["GROWSTUFF_ELASTICSEARCH"] == "true"
|
||||
@@ -177,6 +169,10 @@ class Crop < ActiveRecord::Base
|
||||
.count("harvests.id")
|
||||
end
|
||||
|
||||
def annual?
|
||||
perennial != true
|
||||
end
|
||||
|
||||
def interesting?
|
||||
min_plantings = 3 # needs this many plantings to be interesting
|
||||
min_photos = 3 # needs this many photos to be interesting
|
||||
@@ -210,51 +206,25 @@ class Crop < ActiveRecord::Base
|
||||
reason_for_rejection
|
||||
end
|
||||
|
||||
# Crop.search(string)
|
||||
# # Crop.search(string)
|
||||
def self.search(query)
|
||||
if ENV['GROWSTUFF_ELASTICSEARCH'] == "true"
|
||||
search_str = query.nil? ? "" : query.downcase
|
||||
response = __elasticsearch__.search( # Finds documents which match any field, but uses the _score from
|
||||
# the best field insead of adding up _score from each field.
|
||||
query: {
|
||||
multi_match: {
|
||||
query: search_str.to_s,
|
||||
analyzer: "standard",
|
||||
fields: ["name",
|
||||
"scientific_names.scientific_name",
|
||||
"alternate_names.name"]
|
||||
}
|
||||
},
|
||||
filter: {
|
||||
term: { approval_status: "approved" }
|
||||
},
|
||||
size: 50
|
||||
)
|
||||
response.records.to_a
|
||||
else
|
||||
# if we don't have elasticsearch, just do a basic SQL query.
|
||||
# also, make sure it's an actual array not an activerecord
|
||||
# collection, so it matches what we get from elasticsearch and we can
|
||||
# manipulate it in the same ways (eg. deleting elements without deleting
|
||||
# the whole record from the db)
|
||||
matches = Crop.approved.where("name ILIKE ?", "%#{query}%").to_a
|
||||
|
||||
# we want to make sure that exact matches come first, even if not
|
||||
# using elasticsearch (eg. in development)
|
||||
exact_match = Crop.approved.find_by(name: query)
|
||||
if exact_match
|
||||
matches.delete(exact_match)
|
||||
matches.unshift(exact_match)
|
||||
end
|
||||
|
||||
matches
|
||||
end
|
||||
CropSearchService.search(query)
|
||||
end
|
||||
|
||||
def self.case_insensitive_name(name)
|
||||
where(["lower(crops.name) = :value", { value: name.downcase }])
|
||||
end
|
||||
|
||||
def update_lifespan_medians
|
||||
# Median lifespan of plantings
|
||||
update(median_lifespan: Planting.where(crop: self).median(:lifespan))
|
||||
end
|
||||
|
||||
def update_harvest_medians
|
||||
update(median_days_to_first_harvest: Planting.where(crop: self).median(:days_to_first_harvest))
|
||||
update(median_days_to_last_harvest: Planting.where(crop: self).median(:days_to_last_harvest))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def count_uses_of_property(col_name)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class Forum < ActiveRecord::Base
|
||||
extend FriendlyId
|
||||
validates :name, presence: true
|
||||
friendly_id :name, use: [:slugged, :finders]
|
||||
friendly_id :name, use: %i(slugged finders)
|
||||
|
||||
has_many :posts
|
||||
belongs_to :owner, class_name: "Member"
|
||||
|
||||
@@ -2,7 +2,7 @@ class Garden < ActiveRecord::Base
|
||||
extend FriendlyId
|
||||
include Geocodable
|
||||
include PhotoCapable
|
||||
friendly_id :garden_slug, use: [:slugged, :finders]
|
||||
friendly_id :garden_slug, use: %i(slugged finders)
|
||||
|
||||
belongs_to :owner, class_name: 'Member', foreign_key: 'owner_id', counter_cache: true
|
||||
has_many :plantings, dependent: :destroy
|
||||
@@ -41,7 +41,7 @@ class Garden < ActiveRecord::Base
|
||||
"acres" => "acre"
|
||||
}.freeze
|
||||
validates :area_unit, inclusion: { in: AREA_UNITS_VALUES.values,
|
||||
message: "%{value} is not a valid area unit" },
|
||||
message: "%<value>s is not a valid area unit" },
|
||||
allow_nil: true,
|
||||
allow_blank: true
|
||||
|
||||
|
||||
@@ -1,28 +1,11 @@
|
||||
class Harvest < ActiveRecord::Base
|
||||
extend FriendlyId
|
||||
include ActionView::Helpers::NumberHelper
|
||||
extend FriendlyId
|
||||
include PhotoCapable
|
||||
friendly_id :harvest_slug, use: [:slugged, :finders]
|
||||
|
||||
belongs_to :crop
|
||||
belongs_to :owner, class_name: 'Member', counter_cache: true
|
||||
belongs_to :plant_part
|
||||
belongs_to :planting
|
||||
|
||||
default_scope { joins(:owner).order(created_at: :desc) }
|
||||
validates :crop, approved: true
|
||||
|
||||
validates :crop, presence: { message: "must be present and exist in our database" }
|
||||
|
||||
validates :plant_part, presence: { message: "must be present and exist in our database" }
|
||||
|
||||
validates :quantity,
|
||||
numericality: {
|
||||
only_integer: false,
|
||||
greater_than_or_equal_to: 0
|
||||
},
|
||||
allow_nil: true
|
||||
friendly_id :harvest_slug, use: %i(slugged finders)
|
||||
|
||||
# Constants
|
||||
UNITS_VALUES = {
|
||||
"individual" => "individual",
|
||||
"bunches" => "bunch",
|
||||
@@ -35,29 +18,53 @@ class Harvest < ActiveRecord::Base
|
||||
"baskets" => "basket",
|
||||
"bushels" => "bushel"
|
||||
}.freeze
|
||||
validates :unit, inclusion: { in: UNITS_VALUES.values,
|
||||
message: "%{value} is not a valid unit" },
|
||||
allow_nil: true,
|
||||
allow_blank: true
|
||||
|
||||
validates :weight_quantity,
|
||||
numericality: { only_integer: false },
|
||||
allow_nil: true
|
||||
|
||||
WEIGHT_UNITS_VALUES = {
|
||||
"kg" => "kg",
|
||||
"lb" => "lb",
|
||||
"oz" => "oz"
|
||||
}.freeze
|
||||
validates :weight_unit, inclusion: { in: WEIGHT_UNITS_VALUES.values,
|
||||
message: "%{value} is not a valid unit" },
|
||||
allow_nil: true,
|
||||
allow_blank: true
|
||||
|
||||
##
|
||||
## Triggers
|
||||
after_validation :cleanup_quantities
|
||||
|
||||
before_save :set_si_weight
|
||||
|
||||
##
|
||||
## Relationships
|
||||
belongs_to :crop
|
||||
belongs_to :owner, class_name: 'Member', counter_cache: true
|
||||
belongs_to :plant_part
|
||||
belongs_to :planting
|
||||
|
||||
##
|
||||
## Scopes
|
||||
default_scope { joins(:owner) }
|
||||
|
||||
##
|
||||
## Validations
|
||||
validates :crop, approved: true
|
||||
validates :crop, presence: { message: "must be present and exist in our database" }
|
||||
validates :plant_part, presence: { message: "must be present and exist in our database" }
|
||||
validates :harvested_at, presence: true
|
||||
validates :quantity, allow_nil: true, numericality: {
|
||||
only_integer: false, greater_than_or_equal_to: 0
|
||||
}
|
||||
validates :unit, allow_nil: true, allow_blank: true, inclusion: {
|
||||
in: UNITS_VALUES.values, message: "%<value>s is not a valid unit"
|
||||
}
|
||||
validates :weight_quantity, allow_nil: true, numericality: { only_integer: false }
|
||||
validates :weight_unit, allow_nil: true, allow_blank: true, inclusion: {
|
||||
in: WEIGHT_UNITS_VALUES.values, message: "%<value>s is not a valid unit"
|
||||
}
|
||||
validate :crop_must_match_planting
|
||||
validate :harvest_must_be_after_planting
|
||||
|
||||
def time_from_planting_to_harvest
|
||||
return if planting.blank?
|
||||
harvested_at - planting.planted_at
|
||||
end
|
||||
|
||||
# we're storing the harvest weight in kilograms in the db too
|
||||
# to make data manipulation easier
|
||||
def set_si_weight
|
||||
@@ -118,4 +125,15 @@ class Harvest < ActiveRecord::Base
|
||||
def default_photo
|
||||
photos.first || crop.default_photo
|
||||
end
|
||||
|
||||
def crop_must_match_planting
|
||||
return if planting.blank? # only check if we are linked to a planting
|
||||
errors.add(:planting, "must be the same crop") unless crop == planting.crop
|
||||
end
|
||||
|
||||
def harvest_must_be_after_planting
|
||||
# only check if we are linked to a planting
|
||||
return unless harvested_at.present? && planting.present? && planting.planted_at.present?
|
||||
errors.add(:planting, "cannot be harvested before planting") unless harvested_at > planting.planted_at
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ class Member < ActiveRecord::Base
|
||||
include Geocodable
|
||||
extend FriendlyId
|
||||
|
||||
friendly_id :login_name, use: [:slugged, :finders]
|
||||
friendly_id :login_name, use: %i(slugged finders)
|
||||
|
||||
has_many :posts, foreign_key: 'author_id'
|
||||
has_many :comments, foreign_key: 'author_id'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class PlantPart < ActiveRecord::Base
|
||||
extend FriendlyId
|
||||
friendly_id :name, use: [:slugged, :finders]
|
||||
friendly_id :name, use: %i(slugged finders)
|
||||
|
||||
has_many :harvests
|
||||
has_many :crops, -> { uniq }, through: :harvests
|
||||
|
||||
@@ -1,48 +1,10 @@
|
||||
class Planting < ActiveRecord::Base
|
||||
extend FriendlyId
|
||||
include PhotoCapable
|
||||
friendly_id :planting_slug, use: [:slugged, :finders]
|
||||
|
||||
belongs_to :garden
|
||||
belongs_to :owner, class_name: 'Member', counter_cache: true
|
||||
belongs_to :crop, counter_cache: true
|
||||
has_many :harvests, dependent: :destroy
|
||||
|
||||
default_scope { joins(:owner).order(created_at: :desc) }
|
||||
scope :finished, -> { where(finished: true) }
|
||||
scope :current, -> { where(finished: false) }
|
||||
|
||||
scope :interesting, -> { has_photos.one_per_owner }
|
||||
scope :one_per_owner, lambda {
|
||||
joins("JOIN members m ON (m.id=plantings.owner_id)
|
||||
LEFT OUTER JOIN plantings p2
|
||||
ON (m.id=p2.owner_id AND plantings.id < p2.id)").where("p2 IS NULL")
|
||||
}
|
||||
|
||||
delegate :name,
|
||||
:en_wikipedia_url,
|
||||
:default_scientific_name,
|
||||
:plantings_count,
|
||||
to: :crop,
|
||||
prefix: true
|
||||
|
||||
validates :garden, presence: true
|
||||
validates :crop, presence: true
|
||||
validates :crop, approved: { message: "must be present and exist in our database" }
|
||||
|
||||
validates :quantity,
|
||||
numericality: {
|
||||
only_integer: true,
|
||||
greater_than_or_equal_to: 0
|
||||
},
|
||||
allow_nil: true
|
||||
friendly_id :planting_slug, use: %i(slugged finders)
|
||||
|
||||
# Constants
|
||||
SUNNINESS_VALUES = %w(sun semi-shade shade)
|
||||
validates :sunniness, inclusion: { in: SUNNINESS_VALUES,
|
||||
message: "%{value} is not a valid sunniness value" },
|
||||
allow_nil: true,
|
||||
allow_blank: true
|
||||
|
||||
PLANTED_FROM_VALUES = [
|
||||
'seed',
|
||||
'seedling',
|
||||
@@ -56,23 +18,47 @@ class Planting < ActiveRecord::Base
|
||||
'graft',
|
||||
'layering'
|
||||
]
|
||||
validates :planted_from, inclusion: { in: PLANTED_FROM_VALUES,
|
||||
message: "%{value} is not a valid planting method" },
|
||||
allow_nil: true,
|
||||
allow_blank: true
|
||||
|
||||
##
|
||||
## Triggers
|
||||
before_save :calculate_lifespan
|
||||
|
||||
belongs_to :garden
|
||||
belongs_to :owner, class_name: 'Member', counter_cache: true
|
||||
belongs_to :crop, counter_cache: true
|
||||
has_many :harvests, dependent: :destroy
|
||||
|
||||
##
|
||||
## Scopes
|
||||
default_scope { joins(:owner).order(created_at: :desc) }
|
||||
scope :finished, -> { where(finished: true) }
|
||||
scope :current, -> { where(finished: false) }
|
||||
scope :interesting, -> { has_photos.one_per_owner }
|
||||
scope :one_per_owner, lambda {
|
||||
joins("JOIN members m ON (m.id=plantings.owner_id)
|
||||
LEFT OUTER JOIN plantings p2
|
||||
ON (m.id=p2.owner_id AND plantings.id < p2.id)").where("p2 IS NULL")
|
||||
}
|
||||
|
||||
##
|
||||
## Delegations
|
||||
delegate :name, :en_wikipedia_url, :default_scientific_name, :plantings_count,
|
||||
to: :crop, prefix: true
|
||||
|
||||
##
|
||||
## Validations
|
||||
validates :garden, presence: true
|
||||
validates :crop, presence: true, approved: { message: "must be present and exist in our database" }
|
||||
validate :finished_must_be_after_planted
|
||||
|
||||
delegate :days_until_finished, to: :predict
|
||||
delegate :days_until_mature, to: :predict
|
||||
delegate :percentage_grown, to: :predict
|
||||
delegate :start_to_finish_diff, to: :predict
|
||||
|
||||
# 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
|
||||
errors.add(:finished_at, "must be after the planting date") unless planted_at < finished_at
|
||||
end
|
||||
validates :quantity, allow_nil: true, numericality: {
|
||||
only_integer: true, greater_than_or_equal_to: 0
|
||||
}
|
||||
validates :sunniness, allow_nil: true, allow_blank: true, inclusion: {
|
||||
in: SUNNINESS_VALUES, message: "%<value>s is not a valid sunniness value"
|
||||
}
|
||||
validates :planted_from, allow_nil: true, allow_blank: true, inclusion: {
|
||||
in: PLANTED_FROM_VALUES, message: "%<value>s is not a valid planting method"
|
||||
}
|
||||
|
||||
def planting_slug
|
||||
[
|
||||
@@ -100,13 +86,60 @@ class Planting < ActiveRecord::Base
|
||||
planted_at.present? && planted_at <= Date.current
|
||||
end
|
||||
|
||||
def calc_and_set_days_before_maturity
|
||||
self.days_before_maturity = predict.predict_days_before_maturity
|
||||
def finish_predicted_at
|
||||
planted_at + crop.median_lifespan.days if crop.median_lifespan.present? && planted_at.present?
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
def predict
|
||||
PlantingPredictions.new(self)
|
||||
def harvests_with_dates
|
||||
harvests.where.not(harvested_at: nil)
|
||||
end
|
||||
|
||||
def first_harvest_date
|
||||
harvests_with_dates.minimum(:harvested_at)
|
||||
end
|
||||
|
||||
def last_harvest_date
|
||||
harvests_with_dates.maximum(:harvested_at)
|
||||
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
|
||||
errors.add(:finished_at, "must be after the planting date") unless planted_at < finished_at
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class Post < ActiveRecord::Base
|
||||
extend FriendlyId
|
||||
include Likeable
|
||||
friendly_id :author_date_subject, use: [:slugged, :finders]
|
||||
friendly_id :author_date_subject, use: %i(slugged finders)
|
||||
belongs_to :author, class_name: 'Member'
|
||||
belongs_to :forum
|
||||
has_many :comments, dependent: :destroy
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
class Role < ActiveRecord::Base
|
||||
extend FriendlyId
|
||||
friendly_id :name, use: [:slugged, :finders]
|
||||
friendly_id :name, use: %i(slugged finders)
|
||||
|
||||
has_and_belongs_to_many :members # rubocop:disable Rails/HasAndBelongsToMany
|
||||
|
||||
class << self
|
||||
[:crop_wranglers, :admins].each do |method|
|
||||
%i(crop_wranglers admins).each do |method|
|
||||
define_method method do
|
||||
slug = method.to_s.singularize.dasherize
|
||||
Role.where(slug: slug).try(:first).try(:members)
|
||||
|
||||
42
app/services/crop_search_service.rb
Normal file
42
app/services/crop_search_service.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
class CropSearchService
|
||||
# Crop.search(string)
|
||||
def self.search(query)
|
||||
if ENV['GROWSTUFF_ELASTICSEARCH'] == "true"
|
||||
search_str = query.nil? ? "" : query.downcase
|
||||
response = Crop.__elasticsearch__.search( # Finds documents which match any field, but uses the _score from
|
||||
# the best field insead of adding up _score from each field.
|
||||
query: {
|
||||
multi_match: {
|
||||
query: search_str.to_s,
|
||||
analyzer: "standard",
|
||||
fields: ["name",
|
||||
"scientific_names.scientific_name",
|
||||
"alternate_names.name"]
|
||||
}
|
||||
},
|
||||
filter: {
|
||||
term: { approval_status: "approved" }
|
||||
},
|
||||
size: 50
|
||||
)
|
||||
response.records.to_a
|
||||
else
|
||||
# if we don't have elasticsearch, just do a basic SQL query.
|
||||
# also, make sure it's an actual array not an activerecord
|
||||
# collection, so it matches what we get from elasticsearch and we can
|
||||
# manipulate it in the same ways (eg. deleting elements without deleting
|
||||
# the whole record from the db)
|
||||
matches = Crop.approved.where("name ILIKE ?", "%#{query}%").to_a
|
||||
|
||||
# we want to make sure that exact matches come first, even if not
|
||||
# using elasticsearch (eg. in development)
|
||||
exact_match = Crop.approved.find_by(name: query)
|
||||
if exact_match
|
||||
matches.delete(exact_match)
|
||||
matches.unshift(exact_match)
|
||||
end
|
||||
|
||||
matches
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,58 +0,0 @@
|
||||
class PlantingPredictions
|
||||
def initialize(planting)
|
||||
@planting = planting
|
||||
end
|
||||
|
||||
def days_until_finished
|
||||
return 0 if @planting.finished?
|
||||
days = (@planting.finished_at - Date.current).to_i
|
||||
days.positive? ? days : 0
|
||||
end
|
||||
|
||||
def days_until_mature
|
||||
days = ((@planting.planted_at + @planting.days_before_maturity) - Date.current).to_i
|
||||
days.positive? ? days : 0
|
||||
end
|
||||
|
||||
def percentage_grown
|
||||
return nil unless @planting.days_before_maturity && @planting.planted?
|
||||
|
||||
days = (Date.current - @planting.planted_at.to_date).to_f
|
||||
|
||||
return 0 if Date.current < @planting.planted_at
|
||||
return 100 if days > @planting.days_before_maturity
|
||||
percent = (days / @planting.days_before_maturity * 100).to_i
|
||||
|
||||
percent = 100 if percent >= 100
|
||||
|
||||
percent
|
||||
end
|
||||
|
||||
def start_to_finish_diff
|
||||
(@planting.finished_at - @planting.planted_at).to_i if @planting.finished_at && @planting.planted_at
|
||||
end
|
||||
|
||||
def predict_days_before_maturity
|
||||
# calculate the number of days, from planted_at, until maturity
|
||||
if @planting.planted_at && @planting.finished_at
|
||||
start_to_finish_diff
|
||||
elsif @planting.crop_id
|
||||
plantings = other_finished_plantings_same_crop
|
||||
PlantingPredictions.mean_days_until_maturity(plantings)
|
||||
end
|
||||
end
|
||||
|
||||
def self.mean_days_until_maturity(plantings)
|
||||
## Given a set of finished plantings, calculate the average/mean time from start to finish
|
||||
differences = plantings.collect(&:start_to_finish_diff)
|
||||
differences.compact.sum / differences.compact.size unless differences.compact.empty?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def other_finished_plantings_same_crop
|
||||
Planting.where(crop_id: @planting.crop_id)
|
||||
.where.not(id: @planting.id)
|
||||
.where.not(finished_at: nil)
|
||||
end
|
||||
end
|
||||
@@ -29,6 +29,16 @@
|
||||
Wranglers: please ensure this is singular, and capitalize
|
||||
proper nouns only.
|
||||
|
||||
.form-group
|
||||
= f.label :perennial, 'Lifespan', class: 'control-label col-md-2'
|
||||
.col-md-8
|
||||
= f.radio_button(:perennial, false)
|
||||
= f.label(:perennial_false, "Annual")
|
||||
%span.help-block Living and reproducing in a single year or less
|
||||
= f.radio_button(:perennial, true)
|
||||
= f.label(:perennial_true, "Perennial")
|
||||
%span.help-block Living more than two years
|
||||
|
||||
.form-group
|
||||
= f.label :en_wikipedia_url, 'Wikipedia URL', class: 'control-label col-md-2'
|
||||
.col-md-8
|
||||
|
||||
@@ -12,11 +12,23 @@
|
||||
%p
|
||||
%b Scientific name:
|
||||
= crop.default_scientific_name
|
||||
%p
|
||||
%b
|
||||
Planted
|
||||
= pluralize(crop.plantings.size, "time")
|
||||
by #{ENV['GROWSTUFF_SITE_NAME']} members
|
||||
|
||||
- if crop.annual? && crop.median_lifespan.present?
|
||||
%p
|
||||
Median Lifespan
|
||||
%b= crop.median_lifespan
|
||||
days
|
||||
- unless crop.median_days_to_first_harvest.nil?
|
||||
%p
|
||||
First harvest expected
|
||||
%b= crop.median_days_to_first_harvest
|
||||
days after planting
|
||||
|
||||
- if crop.annual? && crop.median_days_to_last_harvest.present?
|
||||
%p
|
||||
Last harvest expected
|
||||
%b= crop.median_days_to_last_harvest
|
||||
days after planting
|
||||
|
||||
- if can? :create, Planting
|
||||
= link_to 'Plant this', new_planting_path(params: { crop_id: crop.id }), class: 'btn btn-primary'
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
%h2 Photos of #{crop.name} harvests
|
||||
.row
|
||||
- unless crop.harvest_photos.empty?
|
||||
- crop.harvest_photos.includes(:owner).first(3).each do |p|
|
||||
.col-md-4
|
||||
- unless crop.harvest_photos.empty?
|
||||
%h3 Photos of #{crop.name} harvests
|
||||
.row
|
||||
- crop.harvest_photos.first(6).each do |p|
|
||||
.col-xs-6.col-md-2
|
||||
= render "photos/thumbnail", photo: p
|
||||
|
||||
%h2 Photos of #{crop.name} plants
|
||||
.row
|
||||
- unless crop.photos.empty?
|
||||
- crop.photos.includes(:owner).first(3).each do |p|
|
||||
.col-md-4
|
||||
- unless crop.photos.empty?
|
||||
%h3 Photos of #{crop.name} plants
|
||||
.row
|
||||
- crop.photos.first(6).each do |p|
|
||||
.col-xs-6.col-md-2
|
||||
= render "photos/thumbnail", photo: p
|
||||
.row
|
||||
= link_to "more photos", crop_photos_path(crop_id: crop.id)
|
||||
|
||||
29
app/views/crops/_predictions.html.haml
Normal file
29
app/views/crops/_predictions.html.haml
Normal file
@@ -0,0 +1,29 @@
|
||||
- unless crop.perennial.nil?
|
||||
%p
|
||||
#{crop.name} is
|
||||
- if crop.perennial == true
|
||||
= link_to 'https://en.wikipedia.org/wiki/Annual_vs._perennial_plant_evolution' do
|
||||
a perennial crop
|
||||
(living more than two years)
|
||||
- elsif crop.perennial == false
|
||||
= link_to 'https://en.wikipedia.org/wiki/Annual_vs._perennial_plant_evolution' do
|
||||
an annual crop
|
||||
(living and reproducing in a single year or less)
|
||||
|
||||
- unless crop.median_lifespan.nil?
|
||||
%p
|
||||
Median lifespan of #{crop.name} plants is
|
||||
%b= crop.median_lifespan
|
||||
days
|
||||
|
||||
- unless crop.median_days_to_first_harvest.nil?
|
||||
%p
|
||||
First harvest expected
|
||||
%b= crop.median_days_to_first_harvest
|
||||
days after planting
|
||||
|
||||
- if crop.perennial == false && crop.median_days_to_last_harvest.present?
|
||||
%p
|
||||
Last harvest expected
|
||||
%b= crop.median_days_to_last_harvest
|
||||
days after planting
|
||||
@@ -20,19 +20,17 @@
|
||||
|
||||
- if can? :create, Seed
|
||||
= link_to 'Add seeds to stash', new_seed_path(params: { crop_id: @crop.id }), class: 'btn btn-default'
|
||||
- if member_signed_in?
|
||||
= display_seed_availability(@current_member, @crop)
|
||||
= link_to "View your seeds", seeds_by_owner_path(owner: current_member.slug)
|
||||
|
||||
.row
|
||||
.col-md-9
|
||||
- unless current_member
|
||||
Learn how to grow #{@crop.name.pluralize} from growers around the world.
|
||||
#{ENV['GROWSTUFF_SITE_NAME']} has tips and advice from real-life growers,
|
||||
including when to plant #{@crop.name.pluralize}, how to harvest
|
||||
#{@crop.name.pluralize}, and more.
|
||||
- if member_signed_in?
|
||||
= display_seed_availability(@current_member, @crop)
|
||||
= link_to "View your seeds", seeds_by_owner_path(owner: current_member.slug)
|
||||
|
||||
= render partial: 'photos', locals: { crop: @crop }
|
||||
%h2 Predictions
|
||||
= render 'predictions', crop: @crop
|
||||
|
||||
%p= render 'crops/photos', crop: @crop
|
||||
|
||||
%h2
|
||||
- if !@crop.plantings.empty?
|
||||
@@ -86,18 +84,18 @@
|
||||
|
||||
%h4 How to grow #{@crop.name.pluralize}
|
||||
|
||||
= render partial: 'grown_for', locals: { crop: @crop }
|
||||
= render partial: 'planting_advice', locals: { crop: @crop }
|
||||
= render 'grown_for', crop: @crop
|
||||
= render 'planting_advice', crop: @crop
|
||||
|
||||
= render partial: 'scientific_names', locals: { crop: @crop }
|
||||
= render partial: 'alternate_names', locals: { crop: @crop }
|
||||
= render 'scientific_names', crop: @crop
|
||||
= render 'alternate_names', crop: @crop
|
||||
|
||||
%h4 #{@crop.name.capitalize} varieties
|
||||
= render partial: 'varieties', locals: { crop: @crop }
|
||||
= render 'varieties', crop: @crop
|
||||
|
||||
= render partial: 'plantings', locals: { crop: @crop }
|
||||
= render partial: 'harvests', locals: { crop: @crop }
|
||||
= render partial: 'find_seeds', locals: { crop: @crop }
|
||||
= render 'plantings', crop: @crop
|
||||
= render 'harvests', crop: @crop
|
||||
= render 'find_seeds', crop: @crop
|
||||
|
||||
|
||||
%h4 Learn more about #{@crop.name.pluralize}
|
||||
|
||||
@@ -33,10 +33,8 @@
|
||||
%dt Planted from:
|
||||
%dd= display_planted_from(planting)
|
||||
|
||||
%dt Mature in:
|
||||
%dd
|
||||
= display_days_before_maturity(planting)
|
||||
days
|
||||
%dt Finish expected:
|
||||
%dd= planting.finish_predicted_at if planting.finish_predicted_at.present?
|
||||
|
||||
%p= render 'plantings/progress', planting: planting, show_explanation: true
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
- unless planting.harvests.empty?
|
||||
Harvests:
|
||||
%ul
|
||||
- planting.harvests.each do |harvest|
|
||||
- planting.harvests.includes(:crop).each do |harvest|
|
||||
%li
|
||||
= harvest.harvested_at ? harvest.harvested_at : "undated"
|
||||
= link_to harvest, harvest_path(harvest)
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
- if !planting.planted?
|
||||
- if planting.crop.perennial
|
||||
%p Perennial
|
||||
- elsif !planting.planted?
|
||||
- if show_explanation == true
|
||||
Progress: 0% - not planted yet
|
||||
= render "plantings/progress_bar", status: "not planted", progress: 0
|
||||
|
||||
- elsif planting.finished?
|
||||
- if show_explanation == true
|
||||
Progress: 100%
|
||||
= render "plantings/progress_bar", status: 'finished', progress: 100
|
||||
|
||||
- elsif planting.days_before_maturity.nil?
|
||||
- elsif planting.finish_predicted_at.nil?
|
||||
- if show_explanation == true
|
||||
Progress: Not calculated, days before maturity unknown
|
||||
Progress: Not enough data
|
||||
= render "plantings/progress_bar", status: "unknown", progress: nil
|
||||
|
||||
- else
|
||||
- if show_explanation == true
|
||||
Progress: #{planting.percentage_grown}%
|
||||
= render "plantings/progress_bar", status: 'growing', progress: planting.percentage_grown
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
-# haml-lint:disable InlineStyles
|
||||
%div{ class: "progress-bar progress-bar-#{status}", role: "progressbar", style: "width: #{progress}%" }
|
||||
- if progress >= 30
|
||||
#{progress}%
|
||||
#{sprintf '%.0f', progress}%
|
||||
- if progress < 30
|
||||
#{progress}%
|
||||
#{sprintf '%.0f', progress}%
|
||||
-# haml-lint:enable InlineStyles
|
||||
|
||||
@@ -28,13 +28,11 @@
|
||||
- unless @planting.owner.location.blank?
|
||||
(#{@planting.owner.location})
|
||||
%dt Quantity:
|
||||
%dd
|
||||
= display_planting_quantity(@planting)
|
||||
%dd= display_planting_quantity(@planting)
|
||||
|
||||
- unless @planting.planted_from.blank?
|
||||
%dt Planted from:
|
||||
%dd
|
||||
= display_planted_from(@planting)
|
||||
%dd= display_planted_from(@planting)
|
||||
|
||||
%dt Sun or shade?
|
||||
%dd
|
||||
@@ -42,11 +40,28 @@
|
||||
= image_tag("sunniness_#{sunniness}.png", size: "25x25", alt: sunniness, title: sunniness)
|
||||
= sunniness
|
||||
|
||||
%dt Days until maturity:
|
||||
%dd= display_days_before_maturity(@planting)
|
||||
- unless @planting.crop.perennial
|
||||
%dt Predicted finish:
|
||||
%dd
|
||||
- if @planting.finish_predicted_at.present?
|
||||
= @planting.finish_predicted_at
|
||||
- else
|
||||
%strong Set planted date to get predictions
|
||||
%dt Finished:
|
||||
%dd= display_finished(@planting)
|
||||
|
||||
%dt Finished:
|
||||
%dd= display_finished(@planting)
|
||||
- if @planting.crop.median_lifespan.present?
|
||||
%dt Expected Lifespan:
|
||||
%dd #{@planting.crop.median_lifespan} days
|
||||
- if @planting.lifespan.present?
|
||||
%dt Actual Lifespan:
|
||||
%dd #{@planting.lifespan} days
|
||||
- if @planting.days_to_first_harvest.present?
|
||||
%dt First Harvest:
|
||||
%dd #{@planting.days_to_first_harvest} days after planting
|
||||
- if @planting.days_to_last_harvest.present?
|
||||
%dt Last Harvest:
|
||||
%dd #{@planting.days_to_last_harvest} days after planting
|
||||
|
||||
%p= render 'plantings/harvests', planting: @planting
|
||||
%p= render 'plantings/progress', planting: @planting, show_explanation: true
|
||||
@@ -67,10 +82,10 @@
|
||||
#{ @planting.description != "" ? strip_tags(@planting.description) : "No description given." }
|
||||
|
||||
- if !@planting.photos.empty? || (can?(:edit, @planting) && can?(:create, Photo))
|
||||
%h2 Pictures
|
||||
%h2 Photos
|
||||
|
||||
.row
|
||||
- @planting.photos.each do |p|
|
||||
- @planting.photos.includes(:owner).each do |p|
|
||||
.col-md-2.six-across
|
||||
= render partial: 'photos/thumbnail', locals: { photo: p }
|
||||
- if can?(:create, Photo) && can?(:edit, @planting)
|
||||
|
||||
22
db/migrate/20171022032108_all_the_predictions.rb
Normal file
22
db/migrate/20171022032108_all_the_predictions.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
class AllThePredictions < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :crops, :perennial, :boolean, default: false
|
||||
|
||||
# time from planted, to finished
|
||||
add_column :plantings, :lifespan, :integer
|
||||
|
||||
# how old was planting at first harvest
|
||||
add_column :plantings, :days_to_first_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_days_to_last_harvest, :integer
|
||||
|
||||
remove_column :plantings, :days_before_maturity, :integer
|
||||
|
||||
create_table :median_functions do |t|
|
||||
end
|
||||
end
|
||||
end
|
||||
9
db/migrate/20171028230429_create_median_function.rb
Normal file
9
db/migrate/20171028230429_create_median_function.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
class CreateMedianFunction < ActiveRecord::Migration
|
||||
def up
|
||||
ActiveMedian.create_function
|
||||
end
|
||||
|
||||
def down
|
||||
ActiveMedian.drop_function
|
||||
end
|
||||
end
|
||||
13
db/migrate/20171105011017_set_prediction_data.rb
Normal file
13
db/migrate/20171105011017_set_prediction_data.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class SetPredictionData < ActiveRecord::Migration
|
||||
def up
|
||||
say "Updating all plantings time to first harvest"
|
||||
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
|
||||
crop.update_harvest_medians
|
||||
end
|
||||
end
|
||||
|
||||
def down; end
|
||||
end
|
||||
25
db/schema.rb
25
db/schema.rb
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20170520060252) do
|
||||
ActiveRecord::Schema.define(version: 20171105011017) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -181,19 +181,23 @@ ActiveRecord::Schema.define(version: 20170520060252) 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.integer "median_lifespan"
|
||||
t.integer "median_days_to_first_harvest"
|
||||
t.integer "median_days_to_last_harvest"
|
||||
end
|
||||
|
||||
add_index "crops", ["name"], name: "index_crops_on_name", using: :btree
|
||||
@@ -290,6 +294,9 @@ ActiveRecord::Schema.define(version: 20170520060252) do
|
||||
add_index "likes", ["likeable_type", "likeable_id"], name: "index_likes_on_likeable_type_and_likeable_id", using: :btree
|
||||
add_index "likes", ["member_id"], name: "index_likes_on_member_id", using: :btree
|
||||
|
||||
create_table "median_functions", force: :cascade do |t|
|
||||
end
|
||||
|
||||
create_table "members", force: :cascade do |t|
|
||||
t.string "email", default: "", null: false
|
||||
t.string "encrypted_password", default: "", null: false
|
||||
@@ -409,8 +416,8 @@ ActiveRecord::Schema.define(version: 20170520060252) 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"
|
||||
@@ -420,9 +427,11 @@ ActiveRecord::Schema.define(version: 20170520060252) 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 "days_to_last_harvest"
|
||||
end
|
||||
|
||||
add_index "plantings", ["slug"], name: "index_plantings_on_slug", unique: true, using: :btree
|
||||
|
||||
@@ -19,7 +19,8 @@ describe HarvestsController do
|
||||
{
|
||||
owner_id: subject.current_member.id,
|
||||
crop_id: FactoryBot.create(:crop).id,
|
||||
plant_part_id: FactoryBot.create(:plant_part).id
|
||||
plant_part_id: FactoryBot.create(:plant_part).id,
|
||||
harvested_at: '2017-01-01'
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@@ -5,6 +5,13 @@ FactoryBot.define do
|
||||
approval_status "approved"
|
||||
creator
|
||||
|
||||
factory :annual_crop, parent: :crop do
|
||||
perennial false
|
||||
end
|
||||
factory :perennial_crop, parent: :crop do
|
||||
perennial true
|
||||
end
|
||||
|
||||
factory :tomato do
|
||||
name "tomato"
|
||||
en_wikipedia_url "http://en.wikipedia.org/wiki/Tomato"
|
||||
|
||||
@@ -5,7 +5,7 @@ FactoryBot.define do
|
||||
crop
|
||||
plant_part
|
||||
owner
|
||||
harvested_at "2013-09-17"
|
||||
harvested_at Time.zone.local(2015, 9, 17)
|
||||
quantity "3"
|
||||
unit "individual"
|
||||
weight_quantity 6
|
||||
|
||||
@@ -3,7 +3,7 @@ FactoryBot.define do
|
||||
garden
|
||||
owner
|
||||
crop
|
||||
planted_at Time.zone.today
|
||||
planted_at Time.zone.local(2014, 7, 30)
|
||||
quantity 33
|
||||
description "This is a *really* good plant."
|
||||
|
||||
@@ -33,8 +33,8 @@ FactoryBot.define do
|
||||
|
||||
factory :finished_planting do
|
||||
finished true
|
||||
planted_at '2014-07-30'
|
||||
finished_at '2014-08-30'
|
||||
planted_at Time.zone.local(2014, 7, 30)
|
||||
finished_at Time.zone.local(2014, 8, 30)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -168,4 +168,88 @@ feature "crop detail page", js: true do
|
||||
expect(current_path).to eq seeds_by_owner_path(owner: member.slug)
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples "lots of harvests" do
|
||||
def planting
|
||||
FactoryBot.create :planting, crop: crop, planted_at: 100.days.ago, finished_at: 1.day.ago
|
||||
end
|
||||
before do
|
||||
# 50 days to harvest
|
||||
FactoryBot.create(:harvest, harvested_at: 50.days.ago, crop: crop, planting: planting)
|
||||
# 20 days to harvest
|
||||
FactoryBot.create(:harvest, harvested_at: 80.days.ago, crop: crop, planting: planting)
|
||||
# 10 days to harvest
|
||||
FactoryBot.create(:harvest, harvested_at: 90.days.ago, crop: crop, planting: planting)
|
||||
planting.crop.plantings.each(&:update_harvest_days)
|
||||
planting.crop.update_lifespan_medians
|
||||
planting.crop.update_harvest_medians
|
||||
end
|
||||
it { is_expected.to have_text("First harvest expected 20 days after planting") }
|
||||
it { is_expected.to have_text "Median lifespan of #{crop.name} plants is 99 days" }
|
||||
end
|
||||
|
||||
subject do
|
||||
visit crop_path(crop)
|
||||
page
|
||||
end
|
||||
|
||||
context 'predictions' do
|
||||
context 'crop is an annual' do
|
||||
let(:crop) { FactoryBot.create :annual_crop }
|
||||
|
||||
describe 'with no harvests' do
|
||||
end
|
||||
|
||||
describe 'with harvests' do
|
||||
include_examples "lots of harvests"
|
||||
end
|
||||
it do
|
||||
is_expected.to have_text(
|
||||
"#{crop.name} is an annual crop (living and reproducing in a single year or less)"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'crop is perennial' do
|
||||
let(:crop) { FactoryBot.create :perennial_crop }
|
||||
|
||||
describe 'with no harvests' do
|
||||
end
|
||||
|
||||
describe 'with harvests' do
|
||||
include_examples "lots of harvests"
|
||||
end
|
||||
it { is_expected.to have_text("#{crop.name} is a perennial crop (living more than two years)") }
|
||||
end
|
||||
|
||||
context 'crop perennial value is null' do
|
||||
let(:crop) { FactoryBot.create :crop, perennial: nil }
|
||||
|
||||
describe 'with no harvests' do
|
||||
end
|
||||
|
||||
describe 'with harvests' do
|
||||
include_examples "lots of harvests"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'annual and perennial' do
|
||||
before { visit crop_path(crop) }
|
||||
context 'crop is an annual' do
|
||||
let(:crop) { FactoryBot.create :annual_crop }
|
||||
it { expect(page).to have_text 'annual crop (living and reproducing in a single year or less)' }
|
||||
it { expect(page).not_to have_text 'perennial crop (living more than two years)' }
|
||||
end
|
||||
context 'crop is perennial' do
|
||||
let(:crop) { FactoryBot.create :perennial_crop }
|
||||
it { expect(page).to have_text 'perennial crop (living more than two years)' }
|
||||
it { expect(page).not_to have_text 'annual crop (living and reproducing in a single year or less)' }
|
||||
end
|
||||
context 'crop perennial value is null' do
|
||||
let(:crop) { FactoryBot.create :crop, perennial: nil }
|
||||
it { expect(page).not_to have_text 'perennial crop (living more than two years)' }
|
||||
it { expect(page).not_to have_text 'annual crop (living and reproducing in a single year or less)' }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -43,7 +43,7 @@ feature "Planting a crop", :js, :elasticsearch do
|
||||
end
|
||||
|
||||
expect(page).to have_content "planting was successfully created"
|
||||
expect(page).to have_content "Progress: Not calculated, days before maturity unknown"
|
||||
expect(page).to have_content "Progress: Not enough data"
|
||||
end
|
||||
|
||||
scenario "Clicking link to owner's profile" do
|
||||
@@ -91,8 +91,7 @@ feature "Planting a crop", :js, :elasticsearch do
|
||||
end
|
||||
|
||||
expect(page).to have_content "planting was successfully created"
|
||||
expect(page).to have_content "Progress: Not calculated, days before maturity unknown"
|
||||
expect(page).to have_content "Days until maturity: unknown"
|
||||
expect(page).to have_content "Progress: Not enough data"
|
||||
end
|
||||
|
||||
it "should show that planting is in progress" do
|
||||
@@ -110,7 +109,7 @@ feature "Planting a crop", :js, :elasticsearch do
|
||||
|
||||
expect(page).to have_content "planting was successfully created"
|
||||
expect(page).to_not have_content "Progress: 0% - not planted yet"
|
||||
expect(page).to_not have_content "Progress: Not calculated, days before maturity unknown"
|
||||
expect(page).to_not have_content "Not enough data"
|
||||
end
|
||||
|
||||
it "should show that planting is 100% complete (no date specified)" do
|
||||
@@ -127,9 +126,8 @@ feature "Planting a crop", :js, :elasticsearch do
|
||||
end
|
||||
|
||||
expect(page).to have_content "planting was successfully created"
|
||||
expect(page).to have_content "Progress: 100%"
|
||||
expect(page).to have_content "100%"
|
||||
expect(page).to have_content "Yes (no date specified)"
|
||||
expect(page).to have_content "Days until maturity: 0"
|
||||
end
|
||||
|
||||
it "should show that planting is 100% complete (date specified)" do
|
||||
@@ -146,8 +144,7 @@ feature "Planting a crop", :js, :elasticsearch do
|
||||
end
|
||||
|
||||
expect(page).to have_content "planting was successfully created"
|
||||
expect(page).to have_content "Progress: 100%"
|
||||
expect(page).to have_content "Days until maturity: 0"
|
||||
expect(page).to have_content "100%"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -173,13 +170,13 @@ feature "Planting a crop", :js, :elasticsearch do
|
||||
|
||||
scenario "Editing a planting to fill in the finished date" do
|
||||
visit planting_path(planting)
|
||||
expect(page).to have_content "Progress: Not calculated, days before maturity unknown"
|
||||
expect(page).to have_content "Progress: Not enough data"
|
||||
click_link "Edit"
|
||||
check "finished"
|
||||
fill_in "Finished date", with: "2015-06-25"
|
||||
click_button "Save"
|
||||
expect(page).to have_content "planting was successfully updated"
|
||||
expect(page).to_not have_content "Progress: Not calculated, days before maturity unknown"
|
||||
expect(page).to_not have_content "Progress: Not enough data"
|
||||
end
|
||||
|
||||
scenario "Marking a planting as finished" do
|
||||
@@ -232,7 +229,7 @@ feature "Planting a crop", :js, :elasticsearch do
|
||||
end
|
||||
expect(page).to have_content "planting was successfully created"
|
||||
expect(page).to have_content "Finished: Yes (no date specified)"
|
||||
expect(page).to have_content "Progress: 100%"
|
||||
expect(page).to have_content "100%"
|
||||
end
|
||||
|
||||
describe "Planting sunniness" do
|
||||
|
||||
@@ -1,64 +1,6 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe PlantingsHelper do
|
||||
describe "display_days_before_maturity" do
|
||||
it "handles nil planted_at, nil finished_at, non-nil days_until_maturity" do
|
||||
planting = FactoryBot.build(:planting,
|
||||
quantity: 5,
|
||||
planted_at: nil,
|
||||
finished_at: nil,
|
||||
days_before_maturity: 17)
|
||||
result = helper.display_days_before_maturity(planting)
|
||||
expect(result).to eq "unknown"
|
||||
end
|
||||
|
||||
it "handles non-nil planted_at and d_b_m, nil finished_at" do
|
||||
planting = FactoryBot.build(:planting,
|
||||
quantity: 5,
|
||||
planted_at: Date.current,
|
||||
finished_at: nil,
|
||||
days_before_maturity: 17)
|
||||
result = helper.display_days_before_maturity(planting)
|
||||
expect(result).to eq "17"
|
||||
end
|
||||
|
||||
it "handles completed plantings" do
|
||||
planting = FactoryBot.build(:planting, finished: true)
|
||||
result = helper.display_days_before_maturity(planting)
|
||||
expect(result).to eq "0"
|
||||
end
|
||||
|
||||
it "handles plantings that should have finished" do
|
||||
planting = FactoryBot.build(:planting,
|
||||
quantity: 5,
|
||||
planted_at: Date.new(0, 1, 1),
|
||||
finished_at: nil,
|
||||
days_before_maturity: "17")
|
||||
result = helper.display_days_before_maturity(planting)
|
||||
expect(result).to eq "0"
|
||||
end
|
||||
|
||||
it "handles nil d_b_m and nil finished_at" do
|
||||
planting = FactoryBot.build(:planting,
|
||||
quantity: 5,
|
||||
planted_at: Date.new(2012, 5, 12),
|
||||
finished_at: nil,
|
||||
days_before_maturity: nil)
|
||||
result = helper.display_days_before_maturity(planting)
|
||||
expect(result).to eq "unknown"
|
||||
end
|
||||
|
||||
it "handles finished_at dates in the future" do
|
||||
planting = FactoryBot.build(:planting,
|
||||
quantity: 5,
|
||||
planted_at: Date.current,
|
||||
finished_at: Date.current + 5,
|
||||
days_before_maturity: nil)
|
||||
result = helper.display_days_before_maturity(planting)
|
||||
expect(result).to eq "5"
|
||||
end
|
||||
end
|
||||
|
||||
describe "display_planting" do
|
||||
let!(:member) { FactoryBot.build(:member, login_name: 'crop_lady') }
|
||||
|
||||
|
||||
@@ -142,7 +142,7 @@ describe Harvest do
|
||||
it 'lists most recent harvests first' do
|
||||
@h1 = FactoryBot.create(:harvest, created_at: 1.day.ago)
|
||||
@h2 = FactoryBot.create(:harvest, created_at: 1.hour.ago)
|
||||
Harvest.all.should eq [@h2, @h1]
|
||||
Harvest.all.order(created_at: :desc).should eq [@h2, @h1]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -5,42 +5,142 @@ describe Planting do
|
||||
let(:garden_owner) { FactoryBot.create(:member) }
|
||||
let(:garden) { FactoryBot.create(:garden, owner: garden_owner) }
|
||||
let(:planting) { FactoryBot.create(:planting, crop: crop, garden: garden) }
|
||||
let(:finished_planting) { FactoryBot.create :planting, planted_at: 4.days.ago, finished_at: 2.days.ago }
|
||||
let(:finished_planting) do
|
||||
FactoryBot.create :planting, planted_at: 4.days.ago, finished_at: 2.days.ago, finished: true
|
||||
end
|
||||
|
||||
describe 'maturity calculations' do
|
||||
describe 'start_to_finish_diff' do
|
||||
it { expect(finished_planting.start_to_finish_diff).to eq(2) }
|
||||
describe 'planting lifespan predictions' do
|
||||
context 'no predications data yet' do
|
||||
describe 'planting planted, not finished' do
|
||||
let(:planting) { FactoryBot.create :planting, planted_at: 30.days.ago, finished_at: nil, finished: false }
|
||||
it { expect(planting.crop.median_lifespan).to eq(nil) }
|
||||
it { expect(planting.expected_lifespan).to eq(nil) }
|
||||
it { expect(planting.days_since_planted).to eq(30) }
|
||||
it { expect(planting.percentage_grown).to eq(nil) }
|
||||
end
|
||||
describe 'planting not planted yet' do
|
||||
let(:planting) { FactoryBot.create :planting, planted_at: nil, finished_at: nil, finished: false }
|
||||
it { expect(planting.crop.median_lifespan).to eq(nil) }
|
||||
it { expect(planting.expected_lifespan).to eq(nil) }
|
||||
it { expect(planting.days_since_planted).to eq(nil) }
|
||||
it { expect(planting.percentage_grown).to eq(nil) }
|
||||
end
|
||||
describe 'planting finished, no planted_at' do
|
||||
let(:planting) { FactoryBot.create :planting, planted_at: nil, finished_at: 1.day.ago, finished: true }
|
||||
it { expect(planting.crop.median_lifespan).to eq(nil) }
|
||||
it { expect(planting.expected_lifespan).to eq(nil) }
|
||||
it { expect(planting.days_since_planted).to eq(nil) }
|
||||
it { expect(planting.percentage_grown).to eq(100) }
|
||||
end
|
||||
describe 'planting all finished' do
|
||||
let(:planting) { FactoryBot.create :planting, planted_at: 30.days.ago, finished_at: 1.day.ago, finished: true }
|
||||
it { expect(planting.crop.median_lifespan).to eq(nil) }
|
||||
it { expect(planting.expected_lifespan).to eq(29) }
|
||||
it { expect(planting.days_since_planted).to eq(30) }
|
||||
it { expect(planting.percentage_grown).to eq(100) }
|
||||
end
|
||||
end
|
||||
|
||||
describe 'other_finished_plantings_same_crop' do
|
||||
context 'lots of data' do
|
||||
before do
|
||||
# eight finished plantings
|
||||
8.times { FactoryBot.create :planting, crop: crop, planted_at: 10.days.ago, finished_at: 2.days.ago }
|
||||
# eight not finished plantings
|
||||
8.times { FactoryBot.create :planting, crop: crop, finished_at: nil }
|
||||
FactoryBot.create :planting, crop: planting.crop, planted_at: 10.days.ago
|
||||
FactoryBot.create :planting, crop: planting.crop, planted_at: 100.days.ago, finished_at: 50.days.ago
|
||||
FactoryBot.create :planting, crop: planting.crop, planted_at: 100.days.ago, finished_at: 51.days.ago
|
||||
FactoryBot.create :planting, crop: planting.crop, planted_at: 2.years.ago, finished_at: 50.days.ago
|
||||
FactoryBot.create :planting, crop: planting.crop, planted_at: 150.days.ago, finished_at: 100.days.ago
|
||||
planting.crop.update_lifespan_medians
|
||||
end
|
||||
let!(:planting_with_diff_crop) { FactoryBot.create :planting, planted_at: 10.days.ago, finished_at: 2.days.ago }
|
||||
let(:planting_predictions) { PlantingPredictions.new(planting) }
|
||||
it { expect(planting_predictions.send(:other_finished_plantings_same_crop).size).to eq(8) }
|
||||
it { expect(planting_predictions.send(:other_finished_plantings_same_crop)).not_to include(planting) }
|
||||
it do
|
||||
expect(planting_predictions.send(:other_finished_plantings_same_crop))
|
||||
.not_to include(planting_with_diff_crop)
|
||||
|
||||
it { expect(planting.crop.median_lifespan).to eq 50 }
|
||||
|
||||
describe 'planting 30 days ago, not finished' do
|
||||
let(:planting) { FactoryBot.create :planting, planted_at: 30.days.ago }
|
||||
|
||||
# 30 / 50
|
||||
it { expect(planting.percentage_grown).to eq 60.0 }
|
||||
it { expect(planting.days_since_planted).to eq 30 }
|
||||
end
|
||||
|
||||
describe 'planting not planted yet' do
|
||||
let(:planting) { FactoryBot.create :planting, planted_at: nil, finished_at: nil }
|
||||
it { expect(planting.percentage_grown).to eq nil }
|
||||
end
|
||||
|
||||
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 }
|
||||
end
|
||||
|
||||
describe 'planted 30 days ago, finished 10 days ago' do
|
||||
let(:planting) { FactoryBot.create :planting, planted_at: 30.days.ago, finished_at: 10.days.ago }
|
||||
it { expect(planting.days_since_planted).to eq 30 }
|
||||
it { expect(planting.percentage_grown).to eq 100 }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'mean_days_until_maturity' do
|
||||
let(:plantings) do
|
||||
FactoryBot.create_list(:planting, 10, crop: crop, planted_at: 12.days.ago, finished_at: 2.days.ago)
|
||||
end
|
||||
it { expect(plantings.size).to eq(10) }
|
||||
it { expect(PlantingPredictions.mean_days_until_maturity(plantings)).to eq(10) }
|
||||
describe 'planting first harvest preductions' 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_days_to_last_harvest).to eq(nil) }
|
||||
it { expect(planting.days_to_first_harvest).to eq(nil) }
|
||||
it { expect(planting.days_to_last_harvest).to eq(nil) }
|
||||
it { expect(planting.expected_lifespan).to eq(nil) }
|
||||
end
|
||||
|
||||
describe 'saving planting calculates days_before_maturity' do
|
||||
before { 5.times { FactoryBot.create :planting, planted_at: 30.days.ago, finished_at: 9.days.ago, crop: crop } }
|
||||
before { planting.calc_and_set_days_before_maturity }
|
||||
it { expect(planting.days_before_maturity).to eq(21) }
|
||||
context 'lots of data' do
|
||||
def one_hundred_day_old_planting
|
||||
FactoryBot.create(:planting, crop: planting.crop, planted_at: 100.days.ago)
|
||||
end
|
||||
before do
|
||||
# 50 days to harvest
|
||||
FactoryBot.create(:harvest, harvested_at: 50.days.ago, crop: planting.crop,
|
||||
planting: one_hundred_day_old_planting)
|
||||
# 20 days to harvest
|
||||
FactoryBot.create(:harvest, harvested_at: 80.days.ago, crop: planting.crop,
|
||||
planting: one_hundred_day_old_planting)
|
||||
# 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) }
|
||||
end
|
||||
describe 'planting has no harvests' do
|
||||
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
|
||||
describe 'planting has first harvest' do
|
||||
let(:planting) { FactoryBot.create :planting, planted_at: 100.days.ago }
|
||||
before do
|
||||
FactoryBot.create :harvest, planting: planting, crop: planting.crop, harvested_at: 10.days.ago
|
||||
planting.update_harvest_days
|
||||
planting.crop.update_harvest_medians
|
||||
end
|
||||
it { expect(planting.days_to_first_harvest).to eq(90) }
|
||||
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_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 }
|
||||
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.crop.update_harvest_medians
|
||||
end
|
||||
it { expect(planting.days_to_first_harvest).to eq(10) }
|
||||
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_days_to_last_harvest).to eq(90) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -88,41 +188,6 @@ describe Planting do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#percentage_grown' do
|
||||
it 'should not be more than 100%' do
|
||||
@planting = FactoryBot.build(:planting, days_before_maturity: 1, planted_at: 1.day.ago)
|
||||
|
||||
Timecop.freeze(2.days.from_now) do
|
||||
@planting.percentage_grown.should be 100
|
||||
end
|
||||
end
|
||||
|
||||
it 'should not be less than 0%' do
|
||||
@planting = FactoryBot.build(:planting, days_before_maturity: 1, planted_at: 1.day.ago)
|
||||
|
||||
Timecop.freeze(2.days.ago) do
|
||||
@planting.percentage_grown.should be nil
|
||||
end
|
||||
end
|
||||
|
||||
it 'should reflect the current growth' do
|
||||
@planting = FactoryBot.build(:planting, days_before_maturity: 10, planted_at: 4.days.ago)
|
||||
expect(@planting.percentage_grown).to eq 40
|
||||
end
|
||||
|
||||
it 'should not be calculated for unplanted plantings' do
|
||||
@planting = FactoryBot.build(:planting, planted_at: nil)
|
||||
|
||||
@planting.planted?.should be false
|
||||
@planting.percentage_grown.should be nil
|
||||
end
|
||||
|
||||
it 'should not be calculated for plantings with an unknown days before maturity' do
|
||||
@planting = FactoryBot.build(:planting, days_before_maturity: nil)
|
||||
@planting.percentage_grown.should be nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'delegation' do
|
||||
it 'system name' do
|
||||
planting.crop_name.should eq planting.crop.name
|
||||
@@ -355,4 +420,14 @@ describe Planting do
|
||||
planting.owner.destroy
|
||||
expect(Planting.joins(:owner).all).not_to include(planting)
|
||||
end
|
||||
|
||||
# it 'predicts harvest times' do
|
||||
# crop = FactoryBot.create :crop
|
||||
# 10.times do
|
||||
# planting = FactoryBot.create :planting, crop: crop, planted_at: Time.zone.local(2013, 1, 1)
|
||||
# FactoryBot.create :harvest, crop: crop, planting: planting, harvested_at: Time.zone.local(2013, 2, 1)
|
||||
# end
|
||||
# planting = FactoryBot.create :planting, planted_at: Time.zone.local(2017, 1, 1), crop: crop
|
||||
# expect(planting.harvest_predicted_at).to eq Time.zone.local(2017, 2, 1)
|
||||
# end
|
||||
end
|
||||
|
||||
@@ -38,7 +38,7 @@ describe Seed do
|
||||
|
||||
context 'tradable' do
|
||||
it 'all valid tradable_to values should work' do
|
||||
['nowhere', 'locally', 'nationally', 'internationally'].each do |t|
|
||||
%w(nowhere locally nationally internationally).each do |t|
|
||||
@seed = FactoryBot.build(:seed, tradable_to: t)
|
||||
@seed.should be_valid
|
||||
end
|
||||
|
||||
@@ -32,12 +32,12 @@ describe "plantings/index" do
|
||||
garden: @garden,
|
||||
crop: @maize,
|
||||
description: '',
|
||||
planted_at: Time.local(2013, 1, 13)),
|
||||
planted_at: Time.zone.local(2013, 1, 13)),
|
||||
FactoryBot.create(:planting,
|
||||
garden: @garden,
|
||||
crop: @tomato,
|
||||
planted_at: Time.local(2013, 1, 13),
|
||||
finished_at: Time.local(2013, 1, 20),
|
||||
planted_at: Time.zone.local(2013, 1, 13),
|
||||
finished_at: Time.zone.local(2013, 1, 20),
|
||||
finished: true)
|
||||
])
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user