Files
growstuff/app/assets/javascripts/auto_suggest.js.coffee
T
Brenda WallaceandClaude Sonnet 5 27f4282f5b 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 <noreply@anthropic.com>
2026-09-21 21:28:46 +12:00

38 lines
1.2 KiB
CoffeeScript

# Uses JQuery's autocomplete to make a suggestion in lieu of a
# preposterously long select dropdown. To implement add code to
# the view like this:
#
# = auto_suggest @resource, :auto_suggest_source
#
# You must also add a search method to the resource's controller.
jQuery ->
if el = $( '.auto-suggest' )
id = $( '.auto-suggest-id' )
el.autocomplete
minLength: 1,
source: el.attr( 'data-source-url' ),
focus: ( event, ui ) ->
el.val( ui.item.name )
id.val( ui.item.id )
false
select: ( event, ui ) ->
el.val( ui.item.name )
id.val( ui.item.id )
false
response: ( event, ui ) ->
id.val( "" )
for item in ui.content
if item.name == el.val()
id.val( item.id )
if el.data( 'uiAutocomplete' )
el.data( 'uiAutocomplete' )._renderItem = ( ul, item ) ->
$( '<li class="list-group-item"></li>' )
.data( 'item.autocomplete', item )
.append( if item.matched_alternate_name then "<a>#{item.matched_alternate_name} <small class=\"text-muted\">(#{item.name})</small></a>" else "<a>#{item.name}</a>" )
.appendTo( ul )