Skip to content
Draft
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/garden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Garden < ApplicationRecord

has_many :plantings, dependent: :destroy
has_many :crops, through: :plantings
has_many :weather_observations, dependent: :destroy

belongs_to :garden_type, optional: true

Expand Down
22 changes: 22 additions & 0 deletions app/models/weather_observation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

Check notice

Code scanning / Brakeman

Potentially dangerous attribute available for mass assignment.

Potentially dangerous attribute available for mass assignment.

# A weather observation is intended to be a snapshot aligned to
# https://github.com/schemaorg/schemaorg/issues/362
class WeatherObservation < ApplicationRecord
Comment thread Fixed
belongs_to :garden

validates :source, presence: true
validates :observed_at, presence: true

attr_accessible :source, :observation_at, :solvar_uv_index, :dew_point_temperature_centigrade,
:air_temperature_centigrade, :relative_humidity, :wind_speed_kmh, :wind_gust_speed_kmh, :garden_id, :wind_direction, :precipitation_probability, :pressure,
:visibility_distance_metres, :weather_type

# Lowest temp on earth: -89.2°C (-128.6°F)
# Highest: 56.7°C
validates :dew_point_temperature_centigrade, numericality: { min: -90, max: 60 }, allow_nil: true
validates :air_temperature_centigrade, numericality: { min: -90, max: 60 }, allow_nil: true
validates :relative_humidity, numericality: { min: 0, max: 100 }, allow_nil: true
validates :wind_speed_kmh, numericality: { min: 0, max: 450 }, allow_nil: true # Highest 408 km/h
validates :wind_gust_speed_kmh, min: 0, max: 450, allow_nil: true # Highest 408 km/h
end
24 changes: 24 additions & 0 deletions db/migrate/20230916061712_add_weather_observations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

class AddWeatherObservations < ActiveRecord::Migration[7.0]
def change
# See https://github.com/schemaorg/schemaorg/issues/362
create_table :weather_observations do |t|
t.string :source
t.datetime :observation_at
t.integer :solar_uv_index
t.decimal :wind_speed_kmh
t.decimal :wind_gust_speed_kmh
t.string :wind_direction
t.decimal :air_temperature_centigrade
t.decimal :relative_humidity
t.decimal :precipitation_probability
t.decimal :dew_point_temperature_centigrade
t.decimal :pressure
t.integer :visibility_distance_metres
t.string :weather_type
t.references :garden_id
t.timestamps
end
end
end