Files
growstuff/app/controllers/alternate_names_controller.rb
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

65 lines
1.7 KiB
Ruby

# frozen_string_literal: true
class AlternateNamesController < ApplicationController
before_action :authenticate_member!, except: %i(index)
load_and_authorize_resource
respond_to :html, :json
responders :flash
# GET /alternate_names
# GET /alternate_names.json
def index
@alternate_names = AlternateName.all.order(:name)
@alternate_names = @alternate_names.where(crop_id: params[:crop_id]) if params[:crop_id].present?
@alternate_names = @alternate_names.paginate(page: params[:page], per_page: 100)
respond_with(@alternate_names)
end
# GET /alternate_names/new
# GET /alternate_names/new.json
def new
@alternate_name = AlternateName.new
@crop = Crop.find_or_initialize_by(id: params[:crop_id])
end
# GET /alternate_names/1/edit
def edit; end
# POST /alternate_names
# POST /alternate_names.json
def create
params[:alternate_name][:creator_id] = current_member.id
@alternate_name = AlternateName.new(alternate_name_params)
if @alternate_name.save
redirect_to @alternate_name.crop, notice: t('alternate_names.created')
else
render action: "new"
end
end
# PUT /alternate_names/1
# PUT /alternate_names/1.json
def update
if @alternate_name.update(alternate_name_params)
redirect_to @alternate_name.crop, notice: t('alternate_names.updated')
else
render action: "edit"
end
end
# DELETE /alternate_names/1
# DELETE /alternate_names/1.json
def destroy
@crop = @alternate_name.crop
@alternate_name.destroy
redirect_to @crop, notice: t('alternate_names.deleted')
end
private
def alternate_name_params
params.require(:alternate_name).permit(:crop_id, :name, :creator_id, :language)
end
end