From bc11a1b8db1b6d00edfa049946da69475dabc8e4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 7 Sep 2025 20:03:18 +0930 Subject: [PATCH] Merge pull request #4209 from Growstuff/extend-crop-model Extend Crop Model and Migrate Data from OpenFarm --- app/controllers/crops_controller.rb | 2 ++ app/models/concerns/open_farm_data.rb | 24 -------------- app/views/crops/_form.html.haml | 8 +++++ app/views/crops/_openfarm_data.html.haml | 33 +++++++++++++++++++ app/views/crops/show.html.haml | 2 ++ .../20240101010101_add_fields_to_crops.rb | 10 ++++++ ...populate_crop_fields_from_openfarm_data.rb | 21 ++++++++++++ db/schema.rb | 6 ++++ spec/controllers/crops_controller_spec.rb | 30 +++++++++++++++++ spec/features/crops/creating_a_crop_spec.rb | 8 +++++ 10 files changed, 120 insertions(+), 24 deletions(-) create mode 100644 app/views/crops/_openfarm_data.html.haml create mode 100644 db/migrate/20240101010101_add_fields_to_crops.rb create mode 100644 db/migrate/20240101010102_populate_crop_fields_from_openfarm_data.rb diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 5a3822e64..8a76566d0 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -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 diff --git a/app/models/concerns/open_farm_data.rb b/app/models/concerns/open_farm_data.rb index 64bcee0b5..891a71c07 100644 --- a/app/models/concerns/open_farm_data.rb +++ b/app/models/concerns/open_farm_data.rb @@ -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 diff --git a/app/views/crops/_form.html.haml b/app/views/crops/_form.html.haml index 18d082172..8797bfe81 100644 --- a/app/views/crops/_form.html.haml +++ b/app/views/crops/_form.html.haml @@ -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' diff --git a/app/views/crops/_openfarm_data.html.haml b/app/views/crops/_openfarm_data.html.haml new file mode 100644 index 000000000..900a47c90 --- /dev/null +++ b/app/views/crops/_openfarm_data.html.haml @@ -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 diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index 8478b09d4..25edfc4c8 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -111,6 +111,8 @@ = render 'harvests', crop: @crop = render 'find_seeds', crop: @crop + = render 'openfarm_data', crop: @crop + = cute_icon .card .card-body diff --git a/db/migrate/20240101010101_add_fields_to_crops.rb b/db/migrate/20240101010101_add_fields_to_crops.rb new file mode 100644 index 000000000..59cbcda90 --- /dev/null +++ b/db/migrate/20240101010101_add_fields_to_crops.rb @@ -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 diff --git a/db/migrate/20240101010102_populate_crop_fields_from_openfarm_data.rb b/db/migrate/20240101010102_populate_crop_fields_from_openfarm_data.rb new file mode 100644 index 000000000..32c35aead --- /dev/null +++ b/db/migrate/20240101010102_populate_crop_fields_from_openfarm_data.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index b09d3956b..982f1e06a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/controllers/crops_controller_spec.rb b/spec/controllers/crops_controller_spec.rb index 2dec49ae7..3b3b4ab10 100644 --- a/spec/controllers/crops_controller_spec.rb +++ b/spec/controllers/crops_controller_spec.rb @@ -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 diff --git a/spec/features/crops/creating_a_crop_spec.rb b/spec/features/crops/creating_a_crop_spec.rb index afaaad7e2..f87a44fbb 100644 --- a/spec/features/crops/creating_a_crop_spec.rb +++ b/spec/features/crops/creating_a_crop_spec.rb @@ -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