# -*- encoding : utf-8 -*- class Admin::ProductCollectionsController < ApplicationController before_filter :auth_admin layout "admin" before_filter :find_product_collections def index end def reorder i = 1 params[:product_collections_ids].each do |id| product_collection = ProductCollection.find(id) product_collection.position = i puts i product_collection.update_attributes :position => i i = i + 1 end if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id]) find_product_collections end flash[:success] = "Collections réorganisées avec succcès." end def cible @product_collections = ProductCollection.all render :layout => false end def new @product_category = ProductCategory.find(params[:product_category_id]) @product_collection = ProductCollection.new(:product_category_id => @product_category.id) LangSite.all.each do |ls| @product_collection.product_collection_langs << ProductCollectionLang.new(:lang => ls.slug) end end def edit @product_category = ProductCategory.find(params[:product_category_id]) @product_collection = ProductCollection.find(params[:id]) end def show @product_collection = ProductCollection.find(params[:id]) end def create @product_category = ProductCategory.find(params[:product_category_id]) @product_collection = ProductCollection.new(params.require(:product_collection).permit!) if @product_collection.save @product_collections = @product_collection.product_category.product_collections.order(:position) flash[:notice] = "La collection à bien été créée." else render :action => "new" end end def update @product_category = ProductCategory.find(params[:product_category_id]) @product_collection = ProductCollection.find(params[:id]) @reorder = true if params[:reorder] respond_to do |format| if @product_collection.update_attributes(params.require(:product_collection).permit!) #ProductCollection.all.each do |mi| # mi.set_permalink #end flash[:notice] = "La collection à été modifié avec succès." format.html { redirect_to(admin_product_collections_path(:parent_id => @product_collection.parent_id)) } if @reorder format.js { render :action => "update" } else format.js { @product_collection = ProductCollection.find(@product_collection.id) render :action => "update_row" } end else flash[:error] = "Cette collection n'a pas pu être déplacée. 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 @product_collection = ProductCollection.find(params[:id]) @product_collection.destroy flash[:notice] = "La collection à bien été supprimée." end protected def find_product_collections params[:parent_id] = nil if !params[:parent_id] or params[:parent_id] == "" @product_collection_parent = ProductCollection.find(params[:parent_id]) if params[:parent_id] @product_collections = ProductCollection.where(:parent_id => params[:parent_id]).order("position ASC") end end