lectures_controller.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. class LecturesController < ApplicationController
  2. before_action :set_lecture, only: [:show, :edit, :update, :destroy]
  3. before_action :check_can_edit, only: [:edit, :update]
  4. before_action :check_can_create, only: [:create]
  5. def show
  6. end
  7. def new
  8. @lecture = Lecture.new
  9. end
  10. def edit; end
  11. def create
  12. @lecture = Lecture.new(lecture_params)
  13. if @lecture.save
  14. flash[:success] = 'Created lecture'
  15. redirect_to @lecture
  16. else
  17. render :new
  18. end
  19. end
  20. def update
  21. if @lecture.update(lecture_params)
  22. flash[:success] = 'Updated lecture'
  23. redirect_to @lecture
  24. else
  25. render :edit
  26. end
  27. end
  28. private
  29. # Use callbacks to share common setup or constraints between actions.
  30. def set_lecture
  31. @lecture = Lecture.find(params[:id])
  32. end
  33. def current_course
  34. begin
  35. Course.find(params.require(:lecture)[:course_id])
  36. rescue
  37. @lecture.course
  38. end
  39. end
  40. def lecture_params
  41. allowed = [:description]
  42. allowed += [:course_id, :starts_at, :ends_at, :location] if current_user.is_course_administrator?(current_course)
  43. params.require(:lecture).permit(*allowed)
  44. end
  45. def check_can_create
  46. redirect_to root_url unless params.require(:lecture)[:course_id] && current_user.is_course_administrator?(current_course)
  47. end
  48. def check_can_edit
  49. redirect_to root_url unless current_user.is_course_teacher?(@lecture.course)
  50. end
  51. end