Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ingredients #18

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/controllers/api/v1/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ def create
instructions: JSON.parse(params[:instructions]),
user: current_user
)

recipe.image.attach(params[:image]) if params[:image].present?

JSON.parse(params[:ingredients]).each do |ingredient_data|
ingredient = Ingredient.find_or_create_by!(name: ingredient_data['name'].downcase.strip)
recipe.ingredient_quantities << IngredientQuantity.new(
ingredient: ingredient,
quantity: ingredient_data['quantity'],
unit: ingredient_data['unit'].downcase.strip
)
end

recipe.save!
render json: nil, status: :created
rescue ActiveRecord::RecordInvalid => e
Expand Down
3 changes: 3 additions & 0 deletions app/models/ingredient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Ingredient < ApplicationRecord
validates :name, presence: true
end
6 changes: 6 additions & 0 deletions app/models/ingredient_quantity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class IngredientQuantity < ApplicationRecord
belongs_to :recipe
belongs_to :ingredient

validates :quantity, presence: true, numericality: true
end
1 change: 1 addition & 0 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Recipe < ApplicationRecord
belongs_to :user
alias :author :user
has_one_attached :image
has_many :ingredient_quantities

# Return the url for the attached image, if any.
def image_url
Expand Down
12 changes: 12 additions & 0 deletions app/webpack/api/v1/recipes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import axios from 'axios';

import { getCsrfToken } from '../../helpers/form-helper';

export function createRecipe(data) {
axios.defaults.headers.common['X-CSRF-TOKEN'] = getCsrfToken();
return axios.post('/api/v1/recipes', data, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
}
178 changes: 0 additions & 178 deletions app/webpack/components/RecipeForm.js

This file was deleted.

26 changes: 0 additions & 26 deletions app/webpack/components/RecipeInstructionInput.js

This file was deleted.

Loading