mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-19 06:15:44 -04:00
Merge pull request #4209 from Growstuff/extend-crop-model
Extend Crop Model and Migrate Data from OpenFarm
This commit is contained in:
committed by
GitHub
parent
12f6b76dca
commit
bc11a1b8db
@@ -192,6 +192,8 @@ class CropsController < ApplicationController
|
||||
:parent_id, :perennial,
|
||||
:request_notes, :reason_for_rejection,
|
||||
:rejection_notes,
|
||||
:row_spacing, :spread, :height,
|
||||
:sowing_method, :sun_requirements, :growing_degree_days,
|
||||
scientific_names_attributes: %i(scientific_name _destroy id)
|
||||
)
|
||||
end
|
||||
|
||||
@@ -8,14 +8,6 @@ module OpenFarmData
|
||||
fetch_attr('main_image_path')
|
||||
end
|
||||
|
||||
def height
|
||||
fetch_attr('height')
|
||||
end
|
||||
|
||||
def spread
|
||||
fetch_attr('spread')
|
||||
end
|
||||
|
||||
def svg_icon
|
||||
icon = fetch_attr('svg_icon')
|
||||
return icon if icon.present?
|
||||
@@ -31,10 +23,6 @@ module OpenFarmData
|
||||
fetch_attr('description')
|
||||
end
|
||||
|
||||
def row_spacing
|
||||
fetch_attr('row_spacing')
|
||||
end
|
||||
|
||||
def common_names
|
||||
fetch_attr('common_names')
|
||||
end
|
||||
@@ -43,22 +31,10 @@ module OpenFarmData
|
||||
fetch_attr('binomial_name')
|
||||
end
|
||||
|
||||
def sowing_method
|
||||
fetch_attr('sowing_method')
|
||||
end
|
||||
|
||||
def main_image_path
|
||||
fetch_attr('main_image_path')
|
||||
end
|
||||
|
||||
def sun_requirements
|
||||
fetch_attr('sun_requirements')
|
||||
end
|
||||
|
||||
def growing_degree_days
|
||||
fetch_attr('growing_degree_days')
|
||||
end
|
||||
|
||||
def processing_pictures
|
||||
fetch_attr('processing_pictures')
|
||||
end
|
||||
|
||||
@@ -41,6 +41,14 @@
|
||||
= f.radio_button(:perennial, true, label: "Perennial")
|
||||
%span.help-block Living more than two years
|
||||
|
||||
%h2 OpenFarm Data
|
||||
= f.number_field :row_spacing, label: 'Row Spacing (cm)', min: 0
|
||||
= f.number_field :spread, label: 'Spread (cm)', min: 0
|
||||
= f.number_field :height, label: 'Height (cm)', min: 0
|
||||
= f.text_field :sowing_method
|
||||
= f.text_field :sun_requirements
|
||||
= f.number_field :growing_degree_days, min: 0
|
||||
|
||||
- unless @crop.approved?
|
||||
= link_to 'Search wikipedia', "https://en.wikipedia.org/w/index.php?search=#{@crop.name}", target: '_blank'
|
||||
= f.url_field :en_wikipedia_url, id: "en_wikipedia_url", label: 'Wikipedia URL'
|
||||
|
||||
33
app/views/crops/_openfarm_data.html.haml
Normal file
33
app/views/crops/_openfarm_data.html.haml
Normal file
@@ -0,0 +1,33 @@
|
||||
- if crop.row_spacing || crop.spread || crop.height || crop.sowing_method || crop.sun_requirements || crop.growing_degree_days
|
||||
= cute_icon
|
||||
.card
|
||||
.card-body
|
||||
%h4 OpenFarm Data
|
||||
%ul.list-group.list-group-flush
|
||||
- if crop.row_spacing
|
||||
%li.list-group-item
|
||||
%strong Row Spacing:
|
||||
= crop.row_spacing
|
||||
cm
|
||||
- if crop.spread
|
||||
%li.list-group-item
|
||||
%strong Spread:
|
||||
= crop.spread
|
||||
cm
|
||||
- if crop.height
|
||||
%li.list-group-item
|
||||
%strong Height:
|
||||
= crop.height
|
||||
cm
|
||||
- if crop.sowing_method
|
||||
%li.list-group-item
|
||||
%strong Sowing Method:
|
||||
= crop.sowing_method
|
||||
- if crop.sun_requirements
|
||||
%li.list-group-item
|
||||
%strong Sun Requirements:
|
||||
= crop.sun_requirements
|
||||
- if crop.growing_degree_days
|
||||
%li.list-group-item
|
||||
%strong Growing Degree Days:
|
||||
= crop.growing_degree_days
|
||||
@@ -111,6 +111,8 @@
|
||||
= render 'harvests', crop: @crop
|
||||
= render 'find_seeds', crop: @crop
|
||||
|
||||
= render 'openfarm_data', crop: @crop
|
||||
|
||||
= cute_icon
|
||||
.card
|
||||
.card-body
|
||||
|
||||
10
db/migrate/20240101010101_add_fields_to_crops.rb
Normal file
10
db/migrate/20240101010101_add_fields_to_crops.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class AddFieldsToCrops < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :crops, :row_spacing, :integer
|
||||
add_column :crops, :spread, :integer
|
||||
add_column :crops, :height, :integer
|
||||
add_column :crops, :sowing_method, :string
|
||||
add_column :crops, :sun_requirements, :string
|
||||
add_column :crops, :growing_degree_days, :integer
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
class PopulateCropFieldsFromOpenfarmData < ActiveRecord::Migration[5.2]
|
||||
def up
|
||||
Crop.find_each do |crop|
|
||||
if crop.openfarm_data.present?
|
||||
attributes = crop.openfarm_data.fetch('attributes', {})
|
||||
crop.update_columns(
|
||||
row_spacing: attributes['row_spacing'],
|
||||
spread: attributes['spread'],
|
||||
height: attributes['height'],
|
||||
sowing_method: attributes['sowing_method'],
|
||||
sun_requirements: attributes['sun_requirements'],
|
||||
growing_degree_days: attributes['growing_degree_days']
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
# This migration is not reversible.
|
||||
end
|
||||
end
|
||||
@@ -252,6 +252,12 @@ ActiveRecord::Schema[7.2].define(version: 2025_09_01_130830) do
|
||||
t.jsonb "openfarm_data"
|
||||
t.integer "harvests_count", default: 0
|
||||
t.integer "photo_associations_count", default: 0
|
||||
t.integer "row_spacing"
|
||||
t.integer "spread"
|
||||
t.integer "height"
|
||||
t.string "sowing_method"
|
||||
t.string "sun_requirements"
|
||||
t.integer "growing_degree_days"
|
||||
t.index ["creator_id"], name: "index_crops_on_creator_id"
|
||||
t.index ["name"], name: "index_crops_on_name"
|
||||
t.index ["parent_id"], name: "index_crops_on_parent_id"
|
||||
|
||||
@@ -100,6 +100,36 @@ describe CropsController do
|
||||
it { expect { subject }.to change(Crop, :count).by(1) }
|
||||
it { expect { subject }.to change(AlternateName, :count).by(2) }
|
||||
it { expect { subject }.to change(ScientificName, :count).by(1) }
|
||||
|
||||
context 'with openfarm data' do
|
||||
let(:crop_params) do
|
||||
{
|
||||
crop: {
|
||||
name: 'aubergine',
|
||||
en_wikipedia_url: "https://en.wikipedia.org/wiki/Eggplant",
|
||||
row_spacing: 10,
|
||||
spread: 20,
|
||||
height: 30,
|
||||
sowing_method: 'direct',
|
||||
sun_requirements: 'full sun',
|
||||
growing_degree_days: 100
|
||||
},
|
||||
alt_name: { '1': "egg plant", '2': "purple apple" },
|
||||
sci_name: { '1': "fancy sci name", '2': "" }
|
||||
}
|
||||
end
|
||||
|
||||
it 'saves openfarm data' do
|
||||
subject
|
||||
crop = Crop.last
|
||||
expect(crop.row_spacing).to eq(10)
|
||||
expect(crop.spread).to eq(20)
|
||||
expect(crop.height).to eq(30)
|
||||
expect(crop.sowing_method).to eq('direct')
|
||||
expect(crop.sun_requirements).to eq('full sun')
|
||||
expect(crop.growing_degree_days).to eq(100)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -19,6 +19,14 @@ describe "Crop", :js do
|
||||
click_button class: "add-altname-row"
|
||||
fill_in "alt_name[3]", with: "Jazmin"
|
||||
fill_in "alt_name[4]", with: "Matsurika"
|
||||
|
||||
fill_in "crop_row_spacing", with: "12"
|
||||
fill_in "crop_spread", with: "30"
|
||||
fill_in "crop_height", with: "10"
|
||||
fill_in "crop_sowing_method", with: "directly into final position"
|
||||
|
||||
fill_in "crop_sun_requirements", with: "full sun"
|
||||
fill_in "crop_growing_degree_days", with: 100
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user