# -*- encoding : utf-8 -*-

class Admin::IngredientsController < ApplicationController
  layout "admin"
  before_action :auth_admin

  before_action :admin_space
  
  def admin_space
    @admin_space = "stocks"
  end
  
  def index
    @ingredients = Ingredient.all

    @ingredients = sort_by_sorting(@ingredients, "id DESC")
    respond_to do |format|
      format.html{
      
        params[:search][:per_page] = params[:search][:per_page] || 100
        per_page = params[:search][:per_page]
        page = (params[:page] and params[:page] != "") ? params[:page] : 1 
        @ingredients = @ingredients.page(page).per(per_page)
      
      }
    end
  end

  def show
    @ingredient = Ingredient.find(params[:id])

  end

  def new
    @ingredient = Ingredient.new
  
  end

  def edit
    @ingredient = Ingredient.find(params[:id])
    
  end

  def create
    @ingredient = Ingredient.new(params.require(:ingredient).permit!)

    if @ingredient.save
      
    else
      render action: "new"
 
    end

  end


  def update
    @ingredient = Ingredient.find(params[:id])


    if @ingredient.update_attributes(params.require(:ingredient).permit!)
      
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @ingredient = Ingredient.find(params[:id])
    @ingredient.destroy
    
  end
end
