From 27f4282f5b89c1ae10ea08885b976794f5e1b487 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Mon, 21 Sep 2026 21:28:46 +1200 Subject: [PATCH] Let plantings use a crop's alternate name A planting can now choose one of its crop's alternate names (eg. "lauki" for calabash, "aubergine" for eggplant) and shows it instead of the crop's primary name. - plantings.alternate_name_id, validated to belong to the planting's crop - Planting#display_name, used on cards, the planting page, garden lists, the member page, the homepage and the RSS/ICS feeds - planting form offers the crop's alternate names, preselecting the one that was searched for - crop autosuggest shows the matched alternate name, eg. "aubergine (eggplant)", via matched_alternate_name in the search JSON Part of #554 Co-Authored-By: Claude Sonnet 5 --- app/assets/javascripts/auto_suggest.js.coffee | 2 +- .../planting_alternate_name.js.coffee | 26 + app/controllers/alternate_names_controller.rb | 4 +- app/controllers/crops_controller.rb | 19 +- app/controllers/plantings_controller.rb | 2 +- app/helpers/gardens_helper.rb | 2 +- app/models/planting.rb | 15 +- app/views/home/_plantings.html.haml | 4 +- app/views/members/show.html.haml | 2 +- app/views/plantings/_card.html.haml | 4 +- app/views/plantings/_form.html.haml | 8 + .../plantings/_image_with_popover.html.haml | 2 +- app/views/plantings/_tiny.html.haml | 2 +- app/views/plantings/index.ics.erb | 6 +- app/views/plantings/index.rss.haml | 2 +- app/views/plantings/show.html.haml | 6 +- ...1000000_add_alternate_name_to_plantings.rb | 6 + db/schema.rb | 1065 +++++++++-------- spec/controllers/crops_controller_spec.rb | 15 + spec/models/planting_spec.rb | 20 + 20 files changed, 662 insertions(+), 550 deletions(-) create mode 100644 app/assets/javascripts/planting_alternate_name.js.coffee create mode 100644 db/migrate/20260921000000_add_alternate_name_to_plantings.rb diff --git a/app/assets/javascripts/auto_suggest.js.coffee b/app/assets/javascripts/auto_suggest.js.coffee index 60e9304c0..335e27d8d 100644 --- a/app/assets/javascripts/auto_suggest.js.coffee +++ b/app/assets/javascripts/auto_suggest.js.coffee @@ -33,5 +33,5 @@ jQuery -> el.data( 'uiAutocomplete' )._renderItem = ( ul, item ) -> $( '
  • ' ) .data( 'item.autocomplete', item ) - .append( "#{item.name}" ) + .append( if item.matched_alternate_name then "#{item.matched_alternate_name} (#{item.name})" else "#{item.name}" ) .appendTo( ul ) diff --git a/app/assets/javascripts/planting_alternate_name.js.coffee b/app/assets/javascripts/planting_alternate_name.js.coffee new file mode 100644 index 000000000..3d777e325 --- /dev/null +++ b/app/assets/javascripts/planting_alternate_name.js.coffee @@ -0,0 +1,26 @@ +# When a crop is chosen in the planting form, offer that crop's alternate names. +# Searching by an alternate name ("lauki") preselects it. +jQuery -> + wrapper = $( '#planting-alternate-name' ) + return unless wrapper.length + + select = wrapper.find( 'select' ) + crop_id = $( '#planting_crop_id' ) + crop_input = $( '#crop' ) + + load = ( id, typed ) -> + select.empty() + return wrapper.addClass( 'd-none' ) unless id + $.getJSON wrapper.data( 'source-url' ), { crop_id: id }, ( names ) -> + select.append( $( '