2016-01-12 18:43:00 -06:00
|
|
|
class IngredientsController < ApplicationController
|
2016-01-19 16:50:39 -06:00
|
|
|
|
2018-04-02 00:10:06 -05:00
|
|
|
before_action :set_ingredient, only: [:show, :edit, :update, :destroy]
|
2016-01-12 18:43:00 -06:00
|
|
|
|
2016-07-07 17:47:47 -05:00
|
|
|
before_action :ensure_valid_user, except: [:index]
|
2016-01-19 16:50:39 -06:00
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
# GET /ingredients
|
|
|
|
# GET /ingredients.json
|
|
|
|
def index
|
2018-04-02 00:10:06 -05:00
|
|
|
@ingredients = Ingredient.all.order(:name).page(params[:page]).per(params[:per])
|
|
|
|
if params[:name].present?
|
|
|
|
@ingredients = @ingredients.matches_tokens(:name, params[:name].split.take(4))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /ingredients/new
|
|
|
|
def new
|
|
|
|
@ingredient = Ingredient.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /ingredients/1/edit
|
|
|
|
def edit
|
2016-03-03 13:12:42 -06:00
|
|
|
ensure_owner @ingredient
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# POST /ingredients
|
|
|
|
# POST /ingredients.json
|
|
|
|
def create
|
|
|
|
@ingredient = Ingredient.new(ingredient_params)
|
2016-03-03 13:12:42 -06:00
|
|
|
@ingredient.user = current_user
|
|
|
|
if @ingredient.ndbn.present?
|
|
|
|
@ingredient.set_usda_food(UsdaFood.find_by_ndbn(@ingredient.ndbn))
|
|
|
|
end
|
2016-01-12 18:43:00 -06:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @ingredient.save
|
2016-01-14 15:22:15 -06:00
|
|
|
format.html { redirect_to ingredients_path, notice: 'Ingredient was successfully created.' }
|
2016-01-12 18:43:00 -06:00
|
|
|
format.json { render :show, status: :created, location: @ingredient }
|
|
|
|
else
|
|
|
|
format.html { render :new }
|
|
|
|
format.json { render json: @ingredient.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# PATCH/PUT /ingredients/1
|
|
|
|
def update
|
2016-03-03 13:12:42 -06:00
|
|
|
ensure_owner @ingredient do
|
|
|
|
@ingredient.assign_attributes(ingredient_params)
|
|
|
|
if @ingredient.ndbn.present?
|
|
|
|
@ingredient.set_usda_food(UsdaFood.find_by_ndbn(@ingredient.ndbn))
|
|
|
|
end
|
2016-01-28 14:19:51 -06:00
|
|
|
|
2016-03-03 13:12:42 -06:00
|
|
|
respond_to do |format|
|
|
|
|
if @ingredient.save
|
|
|
|
format.html { redirect_to ingredients_path, notice: 'Ingredient was successfully updated.' }
|
|
|
|
format.json { render :show, status: :ok, location: @ingredient }
|
|
|
|
else
|
|
|
|
format.html { render :edit }
|
|
|
|
format.json { render json: @ingredient.errors, status: :unprocessable_entity }
|
|
|
|
end
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE /ingredients/1
|
|
|
|
# DELETE /ingredients/1.json
|
|
|
|
def destroy
|
2016-03-03 13:12:42 -06:00
|
|
|
ensure_owner @ingredient do
|
|
|
|
@ingredient.destroy
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to ingredients_url, notice: 'Ingredient was successfully destroyed.' }
|
|
|
|
format.json { head :no_content }
|
|
|
|
end
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-28 14:19:51 -06:00
|
|
|
def select_ndbn
|
2016-01-29 18:45:20 -06:00
|
|
|
if params[:id].present?
|
|
|
|
@ingredient = Ingredient.find(params[:id])
|
|
|
|
else
|
|
|
|
@ingredient = Ingredient.new
|
|
|
|
end
|
|
|
|
|
2016-01-28 14:19:51 -06:00
|
|
|
@ingredient.assign_attributes(ingredient_params)
|
2016-01-29 18:45:20 -06:00
|
|
|
|
2016-01-28 14:19:51 -06:00
|
|
|
if @ingredient.ndbn.present?
|
|
|
|
@ingredient.set_usda_food(UsdaFood.find_by_ndbn(@ingredient.ndbn))
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-14 15:22:15 -06:00
|
|
|
def prefetch
|
|
|
|
@ingredients = Ingredient.all.order(:name)
|
|
|
|
render :search
|
|
|
|
end
|
|
|
|
|
|
|
|
def search
|
2016-01-28 14:19:51 -06:00
|
|
|
@ingredients = Ingredient.search(params[:query]).order(:name)
|
2016-01-14 15:22:15 -06:00
|
|
|
end
|
|
|
|
|
2016-01-15 13:49:08 -06:00
|
|
|
def convert
|
2016-01-18 12:58:54 -06:00
|
|
|
@conversion = Conversion.new(conversion_params)
|
|
|
|
|
|
|
|
if @conversion.valid?
|
|
|
|
@output_quantity = @conversion.output_quantity
|
|
|
|
@conversion = Conversion.new
|
|
|
|
else
|
|
|
|
@output_quantity = ''
|
|
|
|
end
|
2016-01-15 13:49:08 -06:00
|
|
|
end
|
|
|
|
|
2016-01-24 19:06:26 -06:00
|
|
|
def usda_food_search
|
2016-09-28 14:12:09 -05:00
|
|
|
@foods = UsdaFood.search(params[:query]).limit(250).order(:long_description)
|
2016-01-28 14:19:51 -06:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :layout => false }
|
|
|
|
format.json { }
|
|
|
|
end
|
2016-01-24 19:06:26 -06:00
|
|
|
end
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
private
|
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
|
|
def set_ingredient
|
|
|
|
@ingredient = Ingredient.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
# Never trust parameters from the scary internet, only allow the white list through.
|
|
|
|
def ingredient_params
|
2016-07-05 16:31:36 -05:00
|
|
|
params.require(:ingredient).permit(:name, :notes, :ndbn, :density, :water, :protein, :lipids, :carbohydrates, :kcal, :fiber, :sugar, :calcium, :sodium, :vit_k, :ingredient_units_attributes => [:name, :gram_weight, :id, :_destroy])
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|
2016-01-18 12:58:54 -06:00
|
|
|
|
|
|
|
def conversion_params
|
2016-01-18 15:10:25 -06:00
|
|
|
params.require(:conversion).permit(:input_quantity, :input_units, :scale, :output_units, :ingredient_id)
|
2016-01-18 12:58:54 -06:00
|
|
|
end
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|