/
planting.rb
131 lines (111 loc) · 4.21 KB
/
planting.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true
class Planting < ApplicationRecord
extend FriendlyId
include PhotoCapable
include Finishable
include Ownable
include PredictPlanting
include PredictHarvest
include SearchPlantings
friendly_id :planting_slug, use: %i(slugged finders)
# Constants
SUNNINESS_VALUES = %w(sun semi-shade shade).freeze
PLANTED_FROM_VALUES = [
'seed', 'seedling', 'cutting', 'root division', 'runner',
'bulb', 'root/tuber', 'bare root plant', 'advanced plant',
'graft', 'layering'
].freeze
belongs_to :garden
belongs_to :crop, counter_cache: true
has_many :harvests, dependent: :destroy
has_many :activities, dependent: :destroy
#
# Ancestry of food
belongs_to :parent_seed, class_name: 'Seed', # parent,
optional: true,
inverse_of: :child_plantings
has_many :child_seeds, class_name: 'Seed', # children
foreign_key: 'parent_planting_id',
inverse_of: :parent_planting,
dependent: :nullify
##
## Scopes
scope :located, lambda {
joins(:garden)
.where.not(gardens: { location: '' })
.where.not(gardens: { latitude: nil })
.where.not(gardens: { longitude: nil })
}
scope :active, -> { where('finished <> true').where('finished_at IS NULL OR finished_at < ?', Time.zone.now) }
scope :annual, -> { joins(:crop).where(crops: { perennial: false }) }
scope :perennial, -> { joins(:crop).where(crops: { perennial: true }) }
scope :interesting, -> { has_photos.one_per_owner.order(planted_at: :desc) }
scope :recent, -> { order(created_at: :desc) }
scope :has_harvests, -> { where('plantings.harvests_count > 0') }
scope :one_per_owner, lambda {
joins("JOIN members m ON (m.id=plantings.owner_id)
LEFT OUTER JOIN plantings p2
ON (m.id=p2.owner_id AND plantings.id < p2.id)").where("p2 IS NULL")
}
##
## Delegations
delegate :name, :slug, :en_wikipedia_url, :default_scientific_name, :plantings_count, :perennial,
to: :crop, prefix: true
delegate :login_name, :slug, :location, to: :owner, prefix: true
delegate :slug, to: :planting, prefix: true
delegate :annual?, :perennial?, :svg_icon, to: :crop
delegate :location, :longitude, :latitude, to: :garden
##
## 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
validate :owner_must_match_garden_owner
validates :quantity, allow_nil: true, numericality: {
only_integer: true, greater_than_or_equal_to: 0
}
validates :sunniness, allow_blank: true, inclusion: {
in: SUNNINESS_VALUES, message: "%<value>s is not a valid sunniness value"
}
validates :planted_from, allow_blank: true, inclusion: {
in: PLANTED_FROM_VALUES, message: "%<value>s is not a valid planting method"
}
def planting_slug
[
owner.login_name,
garden.present? ? garden.name : 'null',
crop.present? ? crop.name : 'null'
].join('-').tr(' ', '-').downcase
end
# stringify as "beet in Skud's backyard" or similar
def to_s
I18n.t('plantings.string', crop: crop.name, garden: garden.name, owner:)
end
def finished?
finished || (finished_at.present? && finished_at <= Time.zone.today)
end
def planted?
planted_at.present? && planted_at <= Time.zone.today
end
def growing?
planted? && !finished?
end
def nearby_same_crop
return Planting.none if location.blank? || latitude.blank? || longitude.blank?
# latitude, longitude = Geocoder.coordinates(location, params: { limit: 1 })
Planting.joins(:garden)
.where(crop:)
.located
.where('gardens.latitude < ? AND gardens.latitude > ?',
latitude + 10, latitude - 10)
end
private
# 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
def owner_must_match_garden_owner
errors.add(:owner, "must be the same as garden") unless owner == garden.owner
end
end