Files
growstuff/app/serializers/garden_form_serializer.rb
T
Brenda WallaceandClaude Sonnet 5.5 b7dbb84ebd Add a garden and plant from the home page without leaving it
The garden dialog now adds a garden as well as edits one (new.json and
create.json on GardensController), and a GardenPicker button opens it from the
home page. A plant_picker helper replaces the home page's separate plant
links and the plant_button partial. The plant dialog asks when it was
planted (Today, Yesterday, Enter date), using a WhenPicker shared with the
harvest dialog. Members with no garden still get the full-page plant link.

Part of #4933

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-16 22:21:26 +13:00

35 lines
1.3 KiB
Ruby

# frozen_string_literal: true
# What the React edit-garden dialog needs to fill in its form: the garden's
# current values and the choices for its selects. Served by GET /gardens/:slug/edit.json.
class GardenFormSerializer
def initialize(garden, member:)
@garden = garden
@member = member
end
def as_json(*)
{
garden: values,
area_units: Garden::AREA_UNITS_VALUES.map { |label, value| { label: label, value: value } },
garden_types: GardenType.order(:name).map { |type| { id: type.id, name: type.name } },
location_help: I18n.t('gardens.form.location_helper'),
member_has_location: @member.location.present?,
settings_url: Rails.application.routes.url_helpers.edit_member_registration_path
}
end
private
# As the edit page does: a garden with no location of its own shows the member's.
def values
{
id: @garden.id, url: (Rails.application.routes.url_helpers.garden_path(@garden) if @garden.persisted?),
name: @garden.name, description: @garden.description,
location: @garden.location.presence || @member.location,
area: @garden.area, area_unit: @garden.area_unit.presence || Garden::AREA_UNITS_VALUES.values.first,
garden_type_id: @garden.garden_type_id, active: @garden.active
}
end
end