parsley/app/controllers/ingredients_controller.rb

121 lines
3.2 KiB
Ruby
Raw Normal View History

2016-01-12 18:43:00 -06:00
class IngredientsController < ApplicationController
2016-01-28 14:19:51 -06:00
before_action :set_ingredient, only: [:edit, :update, :destroy, :select_ndbn]
2016-01-12 18:43:00 -06:00
before_filter :ensure_valid_user, only: [:new, :edit, :create, :update, :destroy]
2016-01-12 18:43:00 -06:00
# GET /ingredients
# GET /ingredients.json
def index
2016-01-14 15:22:15 -06:00
@ingredients = Ingredient.all.order(:name)
2016-01-12 18:43:00 -06:00
end
# GET /ingredients/new
def new
@ingredient = Ingredient.new
end
# GET /ingredients/1/edit
def edit
end
# POST /ingredients
# POST /ingredients.json
def create
@ingredient = Ingredient.new(ingredient_params)
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-01-28 14:19:51 -06:00
@ingredient.assign_attributes(ingredient_params)
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|
2016-01-28 14:19:51 -06:00
if @ingredient.save
format.html { redirect_to ingredients_path, notice: 'Ingredient was successfully updated.' }
2016-01-12 18:43:00 -06:00
format.json { render :show, status: :ok, location: @ingredient }
else
format.html { render :edit }
format.json { render json: @ingredient.errors, status: :unprocessable_entity }
end
end
end
# DELETE /ingredients/1
# DELETE /ingredients/1.json
def destroy
@ingredient.destroy
respond_to do |format|
format.html { redirect_to ingredients_url, notice: 'Ingredient was successfully destroyed.' }
format.json { head :no_content }
end
end
2016-01-28 14:19:51 -06:00
def select_ndbn
@ingredient.assign_attributes(ingredient_params)
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-01-28 14:19:51 -06:00
@foods = UsdaFood.search(params[:query]).limit(50)
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-01-28 14:19:51 -06:00
params.require(:ingredient).permit(:name, :notes, :ndbn, :density, :water, :protein, :lipids, :kcal, :fiber, :sugar)
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