From 51d4db11c6a70659cfa3772741794c74dd9104f6 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Mon, 29 Mar 2021 20:11:37 -0700 Subject: [PATCH] Refactor nutrients units array as Collection --- .../Controllers/JournalEntryController.php | 4 +- app/Http/Controllers/RecipeController.php | 2 +- app/Support/Nutrients.php | 45 +++++++++++++++---- ...212856_create_ingredient_amounts_table.php | 3 +- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index 4c4f681..62b161b 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -97,7 +97,7 @@ class JournalEntryController extends Controller return view('journal-entries.create') ->with('ingredients', $ingredients) ->with('meals', JournalEntry::$meals) - ->with('units', Nutrients::$units) + ->with('units', Nutrients::units()->toArray()) ->with('default_date', Carbon::createFromFormat('Y-m-d', $date)); } @@ -109,7 +109,7 @@ class JournalEntryController extends Controller $date = $request->date ?? Carbon::now()->toDateString(); return view('journal-entries.create-from-nutrients') ->with('meals', JournalEntry::$meals) - ->with('units', Nutrients::$units) + ->with('units', Nutrients::units()->toArray()) ->with('default_date', Carbon::createFromFormat('Y-m-d', $date)); } diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 40efec8..6ea3e1b 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -167,7 +167,7 @@ class RecipeController extends Controller ->with('recipe_tags', $recipe_tags) ->with('ingredients_list', new Collection([...$ingredients, ...$separators])) ->with('steps', $steps) - ->with('ingredients_units', Nutrients::$units); + ->with('ingredients_units', Nutrients::units()->toArray()); } /** diff --git a/app/Support/Nutrients.php b/app/Support/Nutrients.php index 4ea4e89..6cc61c0 100644 --- a/app/Support/Nutrients.php +++ b/app/Support/Nutrients.php @@ -10,14 +10,43 @@ class Nutrients { public static float $gramsPerOunce = 28.349523125; - public static array $units = [ - 'cup' => ['value' => 'cup', 'label' => 'cup'], - 'gram' => ['value' => 'gram', 'label' => 'grams'], - 'oz' => ['value' => 'oz', 'label' => 'oz'], - 'serving' => ['value' => 'serving', 'label' => 'servings'], - 'tbsp' => ['value' => 'tbsp', 'label' => 'tbsp.'], - 'tsp' => ['value' => 'tsp', 'label' => 'tsp.'], - ]; + /** + * Get all supported units and metadata. + * + * Each entry has two keys: + * - value: Machine name for the unit. + * - label: Human-readable name for the unit. + * + * @return \Illuminate\Support\Collection + */ + public static function units(): Collection { + return new Collection([ + 'cup' => [ + 'value' => 'cup', + 'label' => 'cup' + ], + 'gram' => [ + 'value' => 'gram', + 'label' => 'gram' + ], + 'oz' => [ + 'value' => 'oz', + 'label' => 'oz' + ], + 'serving' => [ + 'value' => 'serving', + 'label' => 'serving' + ], + 'tbsp' => [ + 'value' => 'tbsp', + 'label' => 'tbsp.' + ], + 'tsp' => [ + 'value' => 'tsp', + 'label' => 'tsp.' + ], + ]); + } /** * Get all trackable "nutrients" (calories are not technically a nutrient). diff --git a/database/migrations/2021_01_22_212856_create_ingredient_amounts_table.php b/database/migrations/2021_01_22_212856_create_ingredient_amounts_table.php index 602ca54..7638da3 100644 --- a/database/migrations/2021_01_22_212856_create_ingredient_amounts_table.php +++ b/database/migrations/2021_01_22_212856_create_ingredient_amounts_table.php @@ -1,5 +1,6 @@ unsignedInteger('ingredient_id'); $table->string('ingredient_type'); $table->unsignedFloat('amount'); - $table->enum('unit', ['tsp', 'tbsp', 'cup', 'oz', 'gram', 'serving'])->nullable(); + $table->enum('unit', Nutrients::units()->pluck('value')->toArray())->nullable(); $table->string('detail')->nullable(); $table->unsignedInteger('weight'); $table->unsignedInteger('parent_id')->index();