Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class EmployeesController < ApplicationController
load_and_authorize_resource
before_action :set_employee, only: [:show, :edit, :update, :destroy]
before_action :set_project, only: [:new,:create,:index]
# GET /employees
# GET /employees.json
def index
@employees = @project.employees.active.sorting.includes(:project,:service_company)
respond_to do |format|
format.html
format.csv { send_data @employees.to_csv}
format.pdf do
pdf = EmployeesReportPdf.new(@employees,@project)
send_data pdf.render,filename: "empleados_reporte.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
# GET /employees/1
# GET /employees/1.json
def show
respond_to do |format|
format.html
format.pdf do
pdf = SingleEmployeeReportPdf.new(@employee)
send_data pdf.render,filename: "#{@employee.identifier}_reporte.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
# GET /employees/new
def new
@employee = @project.employees.new
end
# GET /employees/1/edit
def edit
end
# POST /employees
# POST /employees.json
def create
@employee = @project.employees.new(employee_params)
@employee.service_company_id = @service_company.id
respond_to do |format|
if @employee.save
@employee.create_activity :create, owner: current_user
format.html { redirect_to @employee, notice: 'Empleado a sido creado con éxito.' }
format.json { render :show, status: :created, location: @employee }
else
format.html { render :new }
format.json { render json: @employee.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /employees/1
# PATCH/PUT /employees/1.json
def update
respond_to do |format|
if @employee.update(employee_params)
@employee.create_activity :update, owner: current_user
format.html { redirect_to @employee, notice: 'Empleado se ha actualizado correctamente.' }
format.json { render :show, status: :ok, location: @employee }
else
format.html { render :edit }
format.json { render json: @employee.errors, status: :unprocessable_entity }
end
end
end
# DELETE /employees/1
# DELETE /employees/1.json
def destroy
@employee.transaction do
@employee.create_activity :destroy, owner: current_user
@employee.destroy
end
respond_to do |format|
format.html { redirect_to project_employees_path(@project), notice: 'Fue Eliminado.' }
format.json { head :no_content }
end
end
def import
service_company = current_user.service_user.service_company
@project.employees.import(params[:file], service_company).delay(queue: "importar empleados", priority: 2)
redirect_to employees_path, notice: "Empleados Importados."
end
def remove_photo
@employee = Employee.find(params[:id])
@employee.photo = nil
@employee.save
redirect_to @employee, notice: "La foto de perfil de usuario se ha eliminado."
end
def check_photo_processing
@photo_status = Employee.find(params[:employees_id]).attachment_is_processing
respond_to do |format|
check_photo_processing.js
end
end
private
def set_project
@project = Project.find(params[:project_id])
end
def set_employee
@employee = Employee.find(params[:id])
@project = @employee.project
@service_company = @employee.service_company
end
# Never trust parameters from the scary internet, only allow the white list through.
def employee_params
params.require(:employee).permit(:identifier, :first_name, :last_name, :address, :phone,:photo,:status,:file ,:project_id,:induction )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.