Skip to content

Commit

Permalink
add meal plans
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Zarazan committed Nov 27, 2024
1 parent bd06def commit bf460f3
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem "cssbundling-rails"
gem "jbuilder"
gem "faker"
gem "humanize"
gem "ruby-measurement"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ GEM
rubocop-minitest
rubocop-performance
rubocop-rails
ruby-measurement (1.3.0)
ruby-progressbar (1.13.0)
rubyzip (2.3.2)
securerandom (0.3.1)
Expand Down Expand Up @@ -355,6 +356,7 @@ DEPENDENCIES
puma (>= 5.0)
rails (~> 8.0.0)
rubocop-rails-omakase
ruby-measurement
selenium-webdriver
solid_cable
solid_cache
Expand Down
44 changes: 44 additions & 0 deletions app/controllers/meal_plans_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class MealPlansController < ApplicationController

Check failure on line 2 in app/controllers/meal_plans_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning.
def create
@meal_plan = MealPlan.new(meal_plan_params)

Check failure on line 5 in app/controllers/meal_plans_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
if @meal_plan.save
render json: @meal_plan, status: :created
else
render json: @meal_plan.errors, status: :unprocessable_entity
end
end

def show
@meal_plan = MealPlan.find(params[:id])
end

def add_recipe
@meal_plan = MealPlan.find(params[:id])
@recipe = Recipe.find(params[:recipe_id])

Check failure on line 20 in app/controllers/meal_plans_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
if @meal_plan.recipes << @recipe
render json: @meal_plan, status: :ok
else
render json: { error: "Could not add recipe to meal plan" }, status: :unprocessable_entity
end
end

def remove_recipe
@meal_plan = MealPlan.find(params[:id])
@recipe = Recipe.find(params[:recipe_id])

Check failure on line 31 in app/controllers/meal_plans_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
if @meal_plan.recipes.delete(@recipe)
render json: @meal_plan, status: :ok
else
render json: { error: "Could not remove recipe from meal plan" }, status: :unprocessable_entity
end
end

private

def meal_plan_params
params.require(:meal_plan).permit(:name)
end
end
19 changes: 16 additions & 3 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def create
end
end

def show
@recipe = Recipe.find(params[:id])
end

def update
@recipe = Recipe.find(params[:id])
if @recipe.update(recipe_params)
Expand All @@ -34,12 +38,21 @@ def destroy
private

def recipe_params
rp = params.require(:recipe).permit(:name, :description, ingredients_attributes: [:id, :food_id, :food_name, :measurement, :_destroy])
rp = params.require(:recipe).permit(
:name,
:description,
:instructions,
ingredients_attributes: [:id, :food_id, :food_name, :measurement, :_destroy]
)
create_new_foods(rp)
end

def create_new_foods(rp)
rp[:ingredients_attributes].each do |ingredient|
food_name = ingredient.delete(:food_name)
if ingredient[:food_id].blank? || ingredient[:food_id] == 0
new_food = Food.new(name: food_name)
ingredient[:food_id] = new_food.id if new_food.save
food = Food.find_or_initialize_by(name: food_name)
ingredient[:food_id] = food.id if food.persisted? || food.save
end
end
rp
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/components/recipes/RecipeEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const EditRecipeForm: React.FC = () => {
const [formData, setFormData] = useState<RecipeFormData>({
name: '',
description: '',
instructions: '',
ingredients_attributes: []
});

Expand All @@ -30,6 +31,7 @@ const EditRecipeForm: React.FC = () => {
setFormData({
name: recipe.name,
description: recipe.description,
instructions: recipe.instructions,
ingredients_attributes: recipe.ingredients
});
}
Expand Down
10 changes: 10 additions & 0 deletions app/javascript/components/recipes/RecipeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Ingredient } from '../../types/types';
export interface RecipeFormData {
name: string;
description: string;
instructions: string;
ingredients_attributes: Ingredient[];
}

Expand Down Expand Up @@ -113,6 +114,15 @@ const RecipeForm: React.FC<RecipeFormProps> = ({ formData, setFormData, onSubmit
/>
</div>

<div className="mb-4">
<label className="block mb-2">Instructions:</label>
<textarea
value={formData.instructions || ''}
onChange={(e) => setFormData({...formData, instructions: e.target.value})}
className="w-full p-2 border rounded"
/>
</div>

<div className="mb-4">
<h3 className="mb-2">Ingredients</h3>
{formData.ingredients_attributes.map((ingredient, index) => (
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/components/recipes/RecipeNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ const NewRecipeForm: React.FC = () => {
const [formData, setFormData] = useState<RecipeFormData>({
name: '',
description: '',
instructions: '',
ingredients_attributes: []
});

const clearForm = () => {
setFormData({
name: '',
description: '',
instructions: '',
ingredients_attributes: []
});
};
Expand Down
1 change: 1 addition & 0 deletions app/javascript/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export interface Recipe {
id: number;
name: string;
description: string;
instructions: string;
ingredients: Ingredient[];
}
3 changes: 3 additions & 0 deletions app/models/meal_plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class MealPlan < ApplicationRecord
has_and_belongs_to_many :recipes
end
1 change: 1 addition & 0 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Recipe < ApplicationRecord
has_many :ingredients, dependent: :destroy
has_and_belongs_to_many :meal_plans

accepts_nested_attributes_for :ingredients, allow_destroy: true

Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
scope "/api" do
resources :recipes
resources :foods, only: [:index]
resources :meal_plans, only: [:create, :show] do
member do
post :add_recipe
delete :remove_recipe
end
end
end

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20241126205103_create_meal_plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateMealPlan < ActiveRecord::Migration[8.0]
def change
create_table :meal_plans do |t|
t.name :string
t.timestamps
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20241126205410_create_meal_plans_recipes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateMealPlansRecipes < ActiveRecord::Migration[8.0]
def change
create_join_table :meal_plans, :recipes do |t|
t.integer :quantity
t.index :recipe_id
t.index :meal_plan_id
end
end
end
15 changes: 14 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bf460f3

Please sign in to comment.