Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.55 KB | None | 0 0
  1. # encoding: utf-8
  2.  
  3. require 'datatables/depts_datatable.rb'
  4.  
  5. class DeptsController < ApplicationController
  6.   load_and_authorize_resource
  7.   # GET /depts
  8.   # GET /depts.json
  9.   def index
  10.     @depts = Dept.order(:name)
  11.  
  12.     if params[:json_request]
  13.      
  14.       respond_to do |format|
  15.         format.html # index.html.erb
  16.         format.json { render :json => @depts}
  17.       end
  18.     else
  19.       respond_to do |format|
  20.         format.html # index.html.erb
  21.         #format.csv {send_data @depts.all_to_csv}
  22.         format.json { render :json => DeptsDatatable.new(view_context, current_user)}
  23.       end
  24.     end
  25.  
  26.   end
  27.  
  28.   # GET /depts/1
  29.   # GET /depts/1.json
  30.   def show
  31.     @dept = Dept.find(params[:id])
  32.     @dept_address = DeptAddress.where(:dept_id => params[:id])
  33.     @dept_telephones = DeptTelephone.where("dept_id = ?", params[:id])
  34.     @dept_equipaments = @dept.teaching_equipaments
  35.     respond_to do |format|
  36.       format.html # show.html.erb
  37.       if params[:type_document] == 'estudante_por_campus'
  38.         authorize! :read, :dept_students
  39.         format.csv {send_data @dept.students_to_csv}
  40.       end
  41.       if params[:type_document] == 'relatorio_mensal'
  42.         authorize! :read, :dept_mensal_report
  43.         format.csv { send_data @dept.to_csv_mensal(@dept) }
  44.       end
  45.       format.xls
  46.       format.json { render :json => @dept }
  47.  
  48.       if params[:type_document] == "campus"
  49.           format.pdf do
  50.           pdf = AllStudentsPDF.new(:dept => @dept)
  51.           send_data pdf.render, :filename => "alunos_por_campus", :type => "application/pdf", :disposition => "inline"
  52.         end
  53.       end
  54.  
  55.     end
  56.   end
  57.  
  58.   # GET /depts/new
  59.   # GET /depts/new.json
  60.   def new
  61.     @dept = Dept.new
  62.     @dept.build_dept_address
  63.  
  64.     respond_to do |format|
  65.       format.html # new.html.erb
  66.       format.json { render :json => @dept }
  67.     end
  68.   end
  69.  
  70.   # GET /depts/1/edit
  71.   def edit
  72.     @dept = Dept.find(params[:id])
  73.   end
  74.  
  75.   # POST /depts
  76.   # POST /depts.json
  77.   def create
  78.     @dept = Dept.new(params[:dept])
  79.    
  80.     #comando para transformar as "/" para "." pois o sistema nao le datas com "/"
  81.     params[:dept][:started_at].gsub!("/",".")
  82.     params[:dept][:finished_at].gsub!("/",".")
  83.  
  84.     respond_to do |format|
  85.       if @dept.save
  86.         format.html { redirect_to @dept, :notice => 'Departamento criado com sucesso.' }
  87.         format.json { render :json => @dept, :status => :created, :location => @dept }
  88.       else
  89.         format.html { render :action => "new" }
  90.         format.json { render :json => @dept.errors, :status => :unprocessable_entity }
  91.       end
  92.     end
  93.   end
  94.  
  95.   # PUT /depts/1
  96.   # PUT /depts/1.json
  97.   def update
  98.     @dept = Dept.find(params[:id])
  99.    
  100.     #comando para transformar as "/" para "." pois o sistema nao le datas com "/"
  101.     params[:dept][:started_at].gsub!("/",".")
  102.     params[:dept][:finished_at].gsub!("/",".")
  103.  
  104.     respond_to do |format|
  105.       if @dept.update_attributes(params[:dept])
  106.         format.html { redirect_to @dept, :notice => 'Departamento atualizado com sucesso.' }
  107.         format.json { head :no_content }
  108.       else
  109.         format.html { render :action => "edit" }
  110.         format.json { render :json => @dept.errors, :status => :unprocessable_entity }
  111.       end
  112.     end
  113.   end
  114.  
  115.   # DELETE /depts/1
  116.   # DELETE /depts/1.json
  117.   def destroy
  118.     @dept = Dept.find(params[:id])
  119.     @dept.destroy
  120.  
  121.     respond_to do |format|
  122.       format.html { redirect_to depts_url, :notice => 'Departamento excluído com sucesso.' }
  123.       format.json { head :no_content }
  124.     end
  125.   end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement