# -*- encoding : utf-8 -*- class Admin::CategoriesController < ApplicationController before_action :auth_admin layout "admin" before_action :find_categories def index end def reorder i = 1 params[:categories_ids].each do |id| category = Category.find(id) category.position = i puts i category.update_attributes :position => i i = i + 1 end if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id]) find_categories end flash[:success] = "Menus réorganisés avec succcès." end def cible @categories = Category.all render :layout => false end def new @category = Category.new(:parent_id => params[:parent_id]) LangSite.all.each do |ls| @category.category_langs << CategoryLang.new(:lang_site_id => ls.id) end end def edit @category = Category.find(params[:id]) end def create @category = Category.new(params.require(:category).permit!) if @category.save params[:parent_id] =@category.parent_id self.find_categories flash[:notice] = "La catégorie à été ajouté avec succès." else render :action => "new" end end def update @category = Category.find(params[:id]) if request.xhr? @category_parent = @category.parent if @category.parent @categories = Category.where(:parent_id => @category.parent_id).order(:position) end if params[:category][:parent_id] and params[:category][:parent_id] == "no-menu-selected" params[:category][:parent_id] = nil end @reorder = true if params[:reorder] respond_to do |format| if @category.update_attributes(params.require(:category).permit!) #Category.all.each do |mi| # mi.set_permalink #end flash[:notice] = "Le menu à été modifié avec succès." format.html { redirect_to(admin_categories_path(:parent_id => @category.parent_id)) } if @reorder format.js { render :action => "update" } else format.js { @category = Category.find(@category.id) render :action => "update_row" } end else flash[:error] = "Cet élément de menu n'a pas pu être déplacé. Vérifiez que son lien permanent n'éxiste pas déjà dans l'élément cible." if @reorder format.js { render :action => "update_reorder_failled" } else format.html { render :action => "edit" } format.js { render :action => "edit" } end end end end def destroy @category = Category.find(params[:id]) params[:parent_id] = @category.parent_id @category.destroy find_categories flash[:notice] = "La catégorie à bien été supprimée." end protected def find_categories params[:parent_id] = nil if !params[:parent_id] or params[:parent_id] == "" @category_parent = Category.find(params[:parent_id]) if params[:parent_id] @categories = Category.where(:parent_id => params[:parent_id]).order("position ASC") end end