categories_controller.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. class CategoriesController < ApplicationController
  2. ##--- Abilities
  3. load_and_authorize_resource
  4. ##--- Breadcrum_rails
  5. add_breadcrumb I18n.t("breadcrumbs." + controller_name), :categories_path
  6. add_breadcrumb "Nueva Línea de producto " , :new_category_path, only: :new
  7. add_breadcrumb "Detalle de la Línea de producto" , :category_path, only: :show
  8. add_breadcrumb "Editar Línea de producto" , :edit_category_path, only: :edit
  9. before_action :set_category, only: [:show, :edit, :update, :destroy]
  10. before_action :get_filters, only: [:index, :edit, :new]
  11. # GET /categories
  12. # GET /categories.json
  13. def index
  14. @categories = Category.vigentes.includes(:children, :products).where("parent_id = 0").order("category ASC")
  15. end
  16. # GET /categories/1
  17. # GET /categories/1.json
  18. def show
  19. end
  20. # GET /categories/new
  21. def new
  22. @category = Category.new
  23. end
  24. # GET /categories/1/edit
  25. def edit
  26. end
  27. # POST /categories
  28. # POST /categories.json
  29. def create
  30. @category = Category.new(category_params)
  31. @notice_title = 'Registro creado';
  32. @category.audit_comment = "La línea de producto " + @category.category + " fue creada."
  33. respond_to do |format|
  34. if @category.save
  35. format.html { redirect_to categories_url, success: "La línea de producto " + @category.category + " fue creada." }
  36. format.json { render :show, status: :created, location: @category }
  37. else
  38. format.html { render :new }
  39. format.json { render json: @category.errors, status: :unprocessable_entity }
  40. end
  41. end
  42. end
  43. # PATCH/PUT /categories/1
  44. # PATCH/PUT /categories/1.json
  45. def update
  46. respond_to do |format|
  47. @category.audit_comment = 'La línea de producto ' + params[:category][:category] + ' fue modificada.'
  48. if @category.update(category_params)
  49. format.html { redirect_to categories_url, success: 'La línea de producto ' + @category.category + ' fue modificada.' }
  50. format.json { render :show, status: :ok, location: @category }
  51. else
  52. format.html { render :edit }
  53. format.json { render json: @category.errors, status: :unprocessable_entity }
  54. end
  55. end
  56. end
  57. def update_status
  58. category = Category.find(params[:category_id])
  59. puts category.active?
  60. if category.active?
  61. category.status = 2
  62. elsif category.inactive?
  63. category.status = 1
  64. end
  65. respond_to do |format|
  66. if category.save(:validate => false)
  67. format.html { redirect_to categories_url, warning: "La línea de producto " + category.category + " fue "+ (category.active? ? "activada" : "desactivada")+"." }
  68. # format.json { render :show, status: :ok, location: @category }
  69. format.json { head :no_content }
  70. else
  71. format.html { redirect_to categories_url }
  72. format.json { render json: @category.errors, status: :unprocessable_entity }
  73. end
  74. end
  75. end
  76. # DELETE /categories/1
  77. # DELETE /categories/1.json
  78. def destroy
  79. # Borrar la relacion entre la categoria a "eliminar" y los productos que pertenecen a ella
  80. @category.products.clear
  81. @category.audit_comment = "La línea de producto " + @category.category + " fue eliminada."
  82. respond_to do |format|
  83. if @category.update_attributes(:status => 0)
  84. format.html { redirect_to categories_url, warning: "La línea de producto " + @category.category + " fue eliminada." }
  85. format.json { head :no_content }
  86. else
  87. format.html { render :edit }
  88. format.json { render json: @pointsale.errors, status: :unprocessable_entity }
  89. end
  90. end
  91. end
  92. private
  93. # Use callbacks to share common setup or constraints between actions.
  94. def set_category
  95. @category = Category.find(params[:id])
  96. end
  97. def get_filters
  98. if params[:current_page].blank?
  99. @current_page = 1
  100. else
  101. @current_page = params[:current_page]
  102. end
  103. @filter = params[:filter]
  104. end
  105. # Never trust parameters from the scary internet, only allow the white list through.
  106. def category_params
  107. params.require(:category).permit(:category, :description, :parent_id, :status)
  108. end
  109. end