# -*- encoding : utf-8 -*-

class Admin::PartitionsController < ApplicationController
  layout "admin"
  before_action :auth_admin


  before_action :admin_space

  def admin_space
    @admin_space = "documents"
  end


  def index
    @partitions = Partition.all

    @partitions = sort_by_sorting(@partitions, "id DESC")
    respond_to do |format|
      format.html{

        params[:search][:per_page] = params[:search][:per_page] || 100
        per_page = params[:search][:per_page]
        page = (params[:page] and params[:page] != "") ? params[:page] : 1
        @partitions = @partitions.page(page).per(per_page)

      }
    end
  end

  def show
    @partition = Partition.find(params[:id])

  end

  def new
    @partition = Partition.new

  end

  def edit
    @partition = Partition.find(params[:id])

  end

  def create
    @partition = Partition.new(params.require(:partition).permit!)

    if @partition.save

    else
      render action: "new"

    end

  end


  def update
    @partition = Partition.find(params[:id])


    if @partition.update_attributes(params.require(:partition).permit!)

    else
     render action: "edit"

    end

  end


  def destroy
    @partition = Partition.find(params[:id])
    @partition.destroy

  end

  def generate_partition
    @partition = Partition.find(params[:id])
    @partition.generate_stock

    redirect_back(fallback_location: admin_partitions_path)
  end
end
