Refactor nutrients units array as Collection

This commit is contained in:
Christopher C. Wells 2021-03-29 20:11:37 -07:00 committed by Christopher Charbonneau Wells
parent 9571454175
commit 51d4db11c6
4 changed files with 42 additions and 12 deletions

View File

@ -97,7 +97,7 @@ class JournalEntryController extends Controller
return view('journal-entries.create') return view('journal-entries.create')
->with('ingredients', $ingredients) ->with('ingredients', $ingredients)
->with('meals', JournalEntry::$meals) ->with('meals', JournalEntry::$meals)
->with('units', Nutrients::$units) ->with('units', Nutrients::units()->toArray())
->with('default_date', Carbon::createFromFormat('Y-m-d', $date)); ->with('default_date', Carbon::createFromFormat('Y-m-d', $date));
} }
@ -109,7 +109,7 @@ class JournalEntryController extends Controller
$date = $request->date ?? Carbon::now()->toDateString(); $date = $request->date ?? Carbon::now()->toDateString();
return view('journal-entries.create-from-nutrients') return view('journal-entries.create-from-nutrients')
->with('meals', JournalEntry::$meals) ->with('meals', JournalEntry::$meals)
->with('units', Nutrients::$units) ->with('units', Nutrients::units()->toArray())
->with('default_date', Carbon::createFromFormat('Y-m-d', $date)); ->with('default_date', Carbon::createFromFormat('Y-m-d', $date));
} }

View File

@ -167,7 +167,7 @@ class RecipeController extends Controller
->with('recipe_tags', $recipe_tags) ->with('recipe_tags', $recipe_tags)
->with('ingredients_list', new Collection([...$ingredients, ...$separators])) ->with('ingredients_list', new Collection([...$ingredients, ...$separators]))
->with('steps', $steps) ->with('steps', $steps)
->with('ingredients_units', Nutrients::$units); ->with('ingredients_units', Nutrients::units()->toArray());
} }
/** /**

View File

@ -10,14 +10,43 @@ class Nutrients
{ {
public static float $gramsPerOunce = 28.349523125; public static float $gramsPerOunce = 28.349523125;
public static array $units = [ /**
'cup' => ['value' => 'cup', 'label' => 'cup'], * Get all supported units and metadata.
'gram' => ['value' => 'gram', 'label' => 'grams'], *
'oz' => ['value' => 'oz', 'label' => 'oz'], * Each entry has two keys:
'serving' => ['value' => 'serving', 'label' => 'servings'], * - value: Machine name for the unit.
'tbsp' => ['value' => 'tbsp', 'label' => 'tbsp.'], * - label: Human-readable name for the unit.
'tsp' => ['value' => 'tsp', 'label' => 'tsp.'], *
]; * @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). * Get all trackable "nutrients" (calories are not technically a nutrient).

View File

@ -1,5 +1,6 @@
<?php <?php
use App\Support\Nutrients;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
@ -18,7 +19,7 @@ class CreateIngredientAmountsTable extends Migration
$table->unsignedInteger('ingredient_id'); $table->unsignedInteger('ingredient_id');
$table->string('ingredient_type'); $table->string('ingredient_type');
$table->unsignedFloat('amount'); $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->string('detail')->nullable();
$table->unsignedInteger('weight'); $table->unsignedInteger('weight');
$table->unsignedInteger('parent_id')->index(); $table->unsignedInteger('parent_id')->index();