warehouses_controller.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. class WarehousesController < ApplicationController
  2. ##--- Abilities
  3. load_and_authorize_resource
  4. ##--- Breadcrum_rails
  5. add_breadcrumb I18n.t("breadcrumbs." + controller_name), :warehouses_path
  6. add_breadcrumb "Nuevo Almacén " , :new_warehouse_path, only: :new
  7. add_breadcrumb "Editar Almacén" , :edit_warehouse_path, only: :edit
  8. before_action :set_warehouse, only: [:show, :edit, :update, :destroy]
  9. before_action :get_filters, only: [:index, :show, :edit, :new]
  10. # GET /warehouses
  11. # GET /warehouses.json
  12. def index
  13. @warehouses = Warehouse.all
  14. end
  15. # GET /warehouses/1
  16. # GET /warehouses/1.json
  17. def show
  18. end
  19. # GET /warehouses/new
  20. def new
  21. @warehouse = Warehouse.new
  22. end
  23. # GET /warehouses/1/edit
  24. def edit
  25. end
  26. # POST /warehouses
  27. # POST /warehouses.json
  28. def create
  29. @warehouse = Warehouse.new(warehouse_params)
  30. @warehouse.prefix.upcase!
  31. respond_to do |format|
  32. message = "Almacén #{@warehouse.name} creado."
  33. @warehouse.audit_comment = message
  34. if @warehouse.save
  35. format.html { redirect_to warehouses_path, success: message }
  36. format.json { render :show, status: :created, location: @warehouse }
  37. else
  38. format.html { render :new }
  39. format.json { render json: @warehouse.errors, status: :unprocessable_entity }
  40. end
  41. end
  42. end
  43. # PATCH/PUT /warehouses/1
  44. # PATCH/PUT /warehouses/1.json
  45. def update
  46. respond_to do |format|
  47. message = "Almacén #{@warehouse.name} modificado."
  48. @warehouse.audit_comment = message
  49. if @warehouse.update(warehouse_params)
  50. format.html { redirect_to warehouses_path, success: message }
  51. format.json { render :show, status: :ok, location: @warehouse }
  52. else
  53. format.html { render :edit }
  54. format.json { render json: @warehouse.errors, status: :unprocessable_entity }
  55. end
  56. end
  57. end
  58. # DELETE /warehouses/1
  59. # DELETE /warehouses/1.json
  60. def destroy
  61. message = "Almacén #{@warehouse.name} eliminado."
  62. @warehouse.audit_comment = message
  63. @warehouse.destroy
  64. respond_to do |format|
  65. format.html { redirect_to warehouses_url, notice: message }
  66. format.json { head :no_content }
  67. end
  68. end
  69. def update_status
  70. warehouse = Warehouse.find(params[:warehouse_id])
  71. if warehouse.active?
  72. warehouse.status = "inactive"
  73. elsif warehouse.inactive?
  74. warehouse.status = "active"
  75. end
  76. respond_to do |format|
  77. message = "El almacen " + warehouse.name + " fue "+ (warehouse.active? ? "activado" : "desactivado")+"."
  78. warehouse.audit_comment = message
  79. if warehouse.save(:validate => false)
  80. format.html { redirect_to warehouses_path, warning: message }
  81. format.json { head :no_content }
  82. else
  83. format.html { redirect_to warehouses_path }
  84. format.json { render json: warehouse.errors, status: :unprocessable_entity }
  85. end
  86. end
  87. end
  88. private
  89. # Use callbacks to share common setup or constraints between actions.
  90. def set_warehouse
  91. @warehouse = Warehouse.find(params[:id])
  92. end
  93. def get_filters
  94. if params[:current_page].blank?
  95. @current_page = 1
  96. else
  97. @current_page = params[:current_page]
  98. end
  99. @filter = params[:filter]
  100. end
  101. # Never trust parameters from the scary internet, only allow the white list through.
  102. def warehouse_params
  103. params.require(:warehouse).permit(:name, :status, :prefix)
  104. end
  105. end