PageRenderTime 250ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/schedules_controller.rb

https://gitlab.com/mdrz_92/prestasala
Ruby | 101 lines | 69 code | 17 blank | 15 comment | 3 complexity | 20dbeb793a9c56b7447947b4fcf6d5af MD5 | raw file
  1. class SchedulesController < ApplicationController
  2. before_action :set_schedule, only: [:show, :edit, :update, :destroy]
  3. # GET /schedules
  4. # GET /schedules.json
  5. def index
  6. @schedules = Schedule.all
  7. @spaces = Space.all
  8. @count_schedules = Schedule.count
  9. @q = Schedule.ransack(params[:q])
  10. @events = @q.result(distinct: true)
  11. end
  12. # GET /schedules/1
  13. # GET /schedules/1.json
  14. def show
  15. end
  16. # GET /schedules/new
  17. def new
  18. @schedule = Schedule.new
  19. end
  20. # GET /schedules/1/edit
  21. def edit
  22. end
  23. # POST /schedules
  24. # POST /schedules.json
  25. def create
  26. @schedule = Schedule.new(schedule_params)
  27. respond_to do |format|
  28. if @schedule.save
  29. if @schedule.permanent==true
  30. duplicated_schedule(@schedule)
  31. end
  32. format.html { redirect_to @schedule, notice: 'Schedule was successfully created.' }
  33. format.json { render :show, status: :created, location: @schedule }
  34. else
  35. format.html { render :new }
  36. format.json { render json: @schedule.errors, status: :unprocessable_entity }
  37. end
  38. end
  39. end
  40. # PATCH/PUT /schedules/1
  41. # PATCH/PUT /schedules/1.json
  42. def update
  43. respond_to do |format|
  44. if @schedule.update(schedule_params)
  45. format.html { redirect_to @schedule, notice: 'Schedule was successfully updated.' }
  46. format.json { render :show, status: :ok, location: @schedule }
  47. else
  48. format.html { render :edit }
  49. format.json { render json: @schedule.errors, status: :unprocessable_entity }
  50. end
  51. end
  52. end
  53. # DELETE /schedules/1
  54. # DELETE /schedules/1.json
  55. def destroy
  56. @schedule.destroy
  57. respond_to do |format|
  58. format.html { redirect_to schedules_url, notice: 'Schedule was successfully destroyed.' }
  59. format.json { head :no_content }
  60. end
  61. end
  62. def remove_all
  63. Schedule.delete_all
  64. flash[:notice] = "Has borrado todos los resultados"
  65. redirect_to schedules_path
  66. end
  67. private
  68. # Use callbacks to share common setup or constraints between actions.
  69. def set_schedule
  70. @schedule = Schedule.find(params[:id])
  71. end
  72. # Never trust parameters from the scary internet, only allow the white list through.
  73. def schedule_params
  74. params.require(:schedule).permit(:permanent, :begin, :block1, :block2, :block3, :block4, :block5, :block6, :block7, :block8, :block9, :space_id, :mandated, :mail, :description, :proyector, :pc, :audio, :parking, :numberparking)
  75. end
  76. def duplicated_schedule(schedule)
  77. 18.times do |i|
  78. @new_schedule =@schedule.dup
  79. #@new_schedule.permanent = false
  80. @new_schedule.begin = @new_schedule.begin + ((i+1)*7).days
  81. @new_schedule.save
  82. end
  83. end
  84. end