From bb3f1a2e1d96815ecf726080010991cf42c6ea86 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sun, 7 Feb 2021 20:09:14 -0800 Subject: [PATCH] Improve serving name handling for recipes and journal entries --- app/Http/Controllers/JournalEntryController.php | 10 +++++++++- app/Models/Food.php | 10 +++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index a6ca7ce..4f14e6d 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -144,7 +144,15 @@ class JournalEntryController extends Controller } // Update summary - $unit = $item->serving_unit_formatted ?? $ingredient['unit']; + if (empty($item->serving_unit) && empty($item->serving_unit_name)) { + $unit = null; + } + elseif (!empty($item->serving_unit_name)) { + $unit = $item->serving_unit_formatted; + } + else { + $unit = $ingredient['unit']; + } $entries[$entry_key]->summary .= (!empty($entries[$entry_key]->summary) ? ', ' : null); $entries[$entry_key]->summary .= "{$ingredient['amount']} {$unit} {$item->name}"; } diff --git a/app/Models/Food.php b/app/Models/Food.php index 808b23d..1de55f3 100644 --- a/app/Models/Food.php +++ b/app/Models/Food.php @@ -136,9 +136,13 @@ final class Food extends Model * Get the "formatted" serving unit (for custom unit support). */ public function getServingUnitFormattedAttribute(): ?string { - $unit = $this->serving_unit; - if (empty($unit)) { - $unit = $this->serving_unit_name ?? null; + // No unit or unit name can be used for e.g. "bell pepper" or "onion" so + // the food name will be displayed directly. + if (empty($this->serving_unit) && empty($this->serving_unit_name)) { + $unit = null; + } + else { + $unit = $this->serving_unit_name ?? $this->serving_unit ?? 'serving'; } return $unit; }