Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class EquipmentController < ApplicationController
- before_action :set_equipment, only: [:show, :edit, :update, :destroy]
- skip_before_action :verify_authenticity_token
- before_filter :authenticate_admin_user!, only: [:index, :edit, :update, :destroy] unless Rails.env.test? # Permissions for authenticated users to add, edit, view, update and destroy
- # GET /equipment
- # GET /equipment.json
- def index
- @equipment = Equipment.order("created_at DESC").search(params[:search]).paginate(:page => params[:page], :per_page => 10)
- end
- # GET /equipment/1
- # GET /equipment/1.json
- def show
- end
- # GET /equipment/new
- def new
- @equipment = Equipment.new
- end
- # GET /equipment/1/edit
- def edit
- end
- # POST /equipment
- # POST /equipment.json
- def create
- @equipment_params = getParams(equipment_params)
- @equipment = Equipment.new(@equipment_params)
- respond_to do |format|
- if @equipment.save
- unless Rails.env.test? # Don't send mails if in test
- ItEquipmentMailer.send_IT_equipment_email(@equipment).deliver #if !@equipment.equipment_requested.empty? && @equipment.computer_type == 'Other'
- ItEquipmentMailer.send_phone_email(@equipment).deliver if @equipment.brli_cell_phone
- ItEquipmentMailer.send_desk_equipment_email(@equipment).deliver if @equipment.desk_equipment
- end
- format.html { redirect_to @equipment, notice: 'Equipment was successfully created.' }
- format.json { render :show, status: :created, location: @equipment }
- else
- @user_id = params[:equipment][:user_id]
- format.html { render :new }
- format.json { render json: @equipment.errors, status: :unprocessable_entity }
- end
- end
- end
- # PATCH/PUT /equipment/1
- # PATCH/PUT /equipment/1.json
- def update
- @equipment_params = getParams(equipment_params)
- respond_to do |format|
- unless Rails.env.test? || Rails.env.development? # Don't send mails if in test
- ItEquipmentMailer.send_IT_equipment_email(@equipment).deliver
- ItEquipmentMailer.send_phone_email(@equipment).deliver if @equipment.brli_cell_phone
- ItEquipmentMailer.send_desk_equipment_email(@equipment).deliver if @equipment.desk_equipment
- end
- if @equipment.update(@equipment_params)
- format.html { redirect_to @equipment, notice: 'Equipment was successfully updated.' }
- format.json { render :show, status: :ok, location: @equipment }
- else
- format.html { render :edit }
- format.json { render json: @equipment.errors, status: :unprocessable_entity }
- end
- end
- end
- # DELETE /equipment/1
- # DELETE /equipment/1.json
- def destroy
- @equipment.destroy
- respond_to do |format|
- format.html { redirect_to equipment_index_url, notice: 'Equipment was successfully destroyed.' }
- format.json { head :no_content }
- end
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def set_equipment
- @equipment = Equipment.find(params[:id])
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def equipment_params
- params.require(:equipment).permit({:desk_phone_authorization => [], :desk_equipment => [], :groups_privs => []},:computer_type, :phone, :brli_cell_phone, :laptop_notes, :desktop_notes, :supervisor_email, :manager_email, :user_id, :dept_code, :location, :shared_folder, :email_group_access, :other, :floor, :office_num, :remote, :hiring_manager, :address).merge(desk_phone_authorization: params[:desk_phone_authorization], desk_equipment: params[:desk_equipment], groups_privs: params[:groups_privs])
- end
- def getParams(equipment_params)
- if equipment_params[:desk_phone_authorization]
- dpa = equipment_params[:desk_phone_authorization]
- dpa_s = dpa.join(',')
- equipment_params[:desk_phone_authorization] = dpa_s
- end
- if equipment_params[:desk_equipment]
- de = equipment_params[:desk_equipment]
- de_s = de.join(',')
- equipment_params[:desk_equipment] = de_s
- end
- # Parse desktop_authorization groups and privileges elements
- equipment_params[:groups_privs] = equipment_params[:groups_privs].join(",") if equipment_params[:groups_privs]
- return equipment_params
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement