Improve serving name handling for recipes and journal entries

This commit is contained in:
Christopher C. Wells 2021-02-07 20:09:14 -08:00
parent c909d3b535
commit bb3f1a2e1d
2 changed files with 16 additions and 4 deletions

View File

@ -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}";
}

View File

@ -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;
}