parsley/app/controllers/recipes_controller.rb

101 lines
2.6 KiB
Ruby
Raw Normal View History

2016-01-12 18:43:00 -06:00
class RecipesController < ApplicationController
2016-02-27 20:12:41 -06:00
before_action :set_recipe, 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: [:show, :scale, :index]
2016-01-12 18:43:00 -06:00
# GET /recipes
def index
2018-04-01 21:43:23 -05:00
@criteria = ViewModels::RecipeCriteria.new(criteria_params)
2016-09-28 17:28:40 -05:00
@criteria.page = params[:page]
@criteria.per = params[:per]
2016-10-20 15:48:33 -05:00
@recipes = Recipe.for_criteria(@criteria).includes(:tags)
2016-01-12 18:43:00 -06:00
end
# GET /recipes/1
# GET /recipes/1.json
def show
2016-02-27 20:12:41 -06:00
if params[:scale].present?
@scale = params[:scale]
@recipe.scale(params[:scale], true)
end
if params[:system].present?
@system = params[:system]
case @system
when 'metric'
@recipe.convert_to_metric
when 'standard'
@recipe.convert_to_standard
end
end
if params[:unit].present?
@unit = params[:unit]
case @unit
when 'mass'
@recipe.convert_to_mass
when 'volume'
@recipe.convert_to_volume
end
end
2016-01-12 18:43:00 -06:00
end
# POST /recipes
def create
@recipe = Recipe.new(recipe_params)
@recipe.user = current_user
2016-01-12 18:43:00 -06:00
if @recipe.save
2018-04-01 21:43:23 -05:00
render json: { success: true }
else
2018-04-01 21:43:23 -05:00
render json: @recipe.errors, status: :unprocessable_entity
2016-01-12 18:43:00 -06:00
end
end
# PATCH/PUT /recipes/1
def update
2016-01-21 11:47:30 -06:00
ensure_owner(@recipe) do
if @recipe.update(recipe_params)
2018-04-01 21:43:23 -05:00
render json: { success: true }
2016-01-21 11:47:30 -06:00
else
2018-04-01 21:43:23 -05:00
render json: @recipe.errors, status: :unprocessable_entity
2016-01-21 11:47:30 -06:00
end
2016-01-12 18:43:00 -06:00
end
end
2018-04-01 21:43:23 -05:00
# POST /recipes/preview_steps
def preview_steps
render json: { rendered_steps: MarkdownProcessor.render(params[:step_text]) }
end
2016-01-12 18:43:00 -06:00
# DELETE /recipes/1
def destroy
2016-01-21 11:47:30 -06:00
ensure_owner(@recipe) do
@recipe.deleted = true
2016-01-18 19:41:26 -06:00
2016-04-04 14:28:23 -05:00
if @recipe.save(validate: false)
2016-01-21 11:47:30 -06:00
redirect_to recipes_url, notice: 'Recipe was successfully destroyed.'
else
redirect_to recipes_url, error: 'Recipe could not be destroyed.'
end
2016-01-12 18:43:00 -06:00
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_recipe
@recipe = Recipe.includes(recipe_ingredients: {ingredient: :ingredient_units }).find(params[:id])
2016-01-12 18:43:00 -06:00
end
# Never trust parameters from the scary internet, only allow the white list through.
def recipe_params
2017-04-13 16:18:20 -05:00
params.require(:recipe).permit(:name, :description, :source, :yields, :total_time, :active_time, :step_text, tag_names: [], recipe_ingredients_attributes: [:name, :ingredient_id, :quantity, :units, :preparation, :sort_order, :id, :_destroy])
2016-01-12 18:43:00 -06:00
end
2018-04-01 21:43:23 -05:00
def criteria_params
params.require(:criteria).permit(*ViewModels::RecipeCriteria::PARAMS)
end
2016-01-12 18:43:00 -06:00
end