Add support for "oz" as a food serving unit

This commit is contained in:
Christopher C. Wells 2021-04-20 11:51:19 -07:00
parent 82601406a0
commit 057433eb90
3 changed files with 14 additions and 4 deletions

View File

@ -65,6 +65,7 @@ class FoodController extends Controller
['value' => 'tsp', 'label' => 'tsp.'],
['value' => 'tbsp', 'label' => 'tbsp.'],
['value' => 'cup', 'label' => 'cup'],
['value' => 'oz', 'label' => 'oz'],
]));
}

View File

@ -27,7 +27,7 @@ class FoodFactory extends Factory
'source' => $this->faker->optional()->url,
'notes' => $this->faker->optional(0.25)->realText(),
'serving_size' => $this->faker->numberBetween(1, 3),
'serving_unit' => $this->faker->randomElement(['tsp', 'tbsp', 'cup']),
'serving_unit' => $this->faker->randomElement(['tsp', 'tbsp', 'cup', 'oz']),
'serving_weight' => $this->faker->numberBetween(5, 500),
'calories' => $this->faker->randomFloat(1, 0, 100),
'fat' => $this->faker->randomFloat(1, 0, 10),

View File

@ -25,12 +25,10 @@ class IngredientAmountFactory extends Factory
if ($this->faker->boolean(90)) {
$ingredient_factory = Food::factory();
$ingredient_type = Food::class;
$ingredient_unit = Nutrients::units()->pluck('value')->random(1)->first();
}
else {
$ingredient_factory = Recipe::factory();
$ingredient_type = Recipe::class;
$ingredient_unit = 'serving';
}
$amounts = [1/8, 1/4, 1/3, 1/2, 2/3, 3/4, 1, 1 + 1/4, 1 + 1/3, 1 + 1/2, 1 + 2/3, 1 + 3/4, 2, 2 + 1/2, 3];
@ -38,7 +36,6 @@ class IngredientAmountFactory extends Factory
'ingredient_id' => $ingredient_factory,
'ingredient_type' => $ingredient_type,
'amount' => $this->faker->randomElement($amounts),
'unit' => $ingredient_unit,
'detail' => $this->faker->boolean() ? Words::randomWords('a') : null,
'weight' => $this->faker->numberBetween(0, 50),
'parent_id' => Recipe::factory(),
@ -46,6 +43,18 @@ class IngredientAmountFactory extends Factory
];
}
public function configure(): static
{
return $this->afterMaking(function (IngredientAmount $ingredient_amount) {
// Set the unit to a random one supported by the ingredient.
$ingredient_amount->unit = $ingredient_amount->ingredient
->units_supported
->random(1)
->pluck('value')
->first();
});
}
/**
* Define a specific parent.
*/