class CategoriesController < ApplicationController ##--- Abilities load_and_authorize_resource ##--- Breadcrum_rails add_breadcrumb I18n.t("breadcrumbs." + controller_name), :categories_path add_breadcrumb "Nueva Línea de producto " , :new_category_path, only: :new add_breadcrumb "Detalle de la Línea de producto" , :category_path, only: :show add_breadcrumb "Editar Línea de producto" , :edit_category_path, only: :edit before_action :set_category, only: [:show, :edit, :update, :destroy] before_action :get_filters, only: [:index, :edit, :new] # GET /categories # GET /categories.json def index @categories = Category.vigentes.includes(:children, :products).where("parent_id = 0").order("category ASC") end # GET /categories/1 # GET /categories/1.json def show end # GET /categories/new def new @category = Category.new end # GET /categories/1/edit def edit end # POST /categories # POST /categories.json def create @category = Category.new(category_params) @notice_title = 'Registro creado'; @category.audit_comment = "La línea de producto " + @category.category + " fue creada." respond_to do |format| if @category.save format.html { redirect_to categories_url, success: "La línea de producto " + @category.category + " fue creada." } format.json { render :show, status: :created, location: @category } else format.html { render :new } format.json { render json: @category.errors, status: :unprocessable_entity } end end end # PATCH/PUT /categories/1 # PATCH/PUT /categories/1.json def update respond_to do |format| @category.audit_comment = 'La línea de producto ' + params[:category][:category] + ' fue modificada.' if @category.update(category_params) format.html { redirect_to categories_url, success: 'La línea de producto ' + @category.category + ' fue modificada.' } format.json { render :show, status: :ok, location: @category } else format.html { render :edit } format.json { render json: @category.errors, status: :unprocessable_entity } end end end def update_status category = Category.find(params[:category_id]) puts category.active? if category.active? category.status = 2 elsif category.inactive? category.status = 1 end respond_to do |format| if category.save(:validate => false) format.html { redirect_to categories_url, warning: "La línea de producto " + category.category + " fue "+ (category.active? ? "activada" : "desactivada")+"." } # format.json { render :show, status: :ok, location: @category } format.json { head :no_content } else format.html { redirect_to categories_url } format.json { render json: @category.errors, status: :unprocessable_entity } end end end # DELETE /categories/1 # DELETE /categories/1.json def destroy # Borrar la relacion entre la categoria a "eliminar" y los productos que pertenecen a ella @category.products.clear @category.audit_comment = "La línea de producto " + @category.category + " fue eliminada." respond_to do |format| if @category.update_attributes(:status => 0) format.html { redirect_to categories_url, warning: "La línea de producto " + @category.category + " fue eliminada." } format.json { head :no_content } else format.html { render :edit } format.json { render json: @pointsale.errors, status: :unprocessable_entity } end end end private # Use callbacks to share common setup or constraints between actions. def set_category @category = Category.find(params[:id]) end def get_filters if params[:current_page].blank? @current_page = 1 else @current_page = params[:current_page] end @filter = params[:filter] end # Never trust parameters from the scary internet, only allow the white list through. def category_params params.require(:category).permit(:category, :description, :parent_id, :status) end end