PageRenderTime 1142ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/faculties/klasses_controller.rb

https://gitlab.com/cd2/developingexperts
Ruby | 87 lines | 72 code | 15 blank | 0 comment | 7 complexity | a5680f5e250331a5d6f22324133a2566 MD5 | raw file
  1. class Faculties::KlassesController < FacultyController
  2. before_action :set_class, only: [:show, :edit, :update, :destroy, :show_graph_json]
  3. before_action :verify_administrator, except: [:index, :graph_json, :show_graph_json]
  4. def index
  5. if signed_in_as_administrator?
  6. @q = @school.klasses.ransack(params[:q])
  7. else
  8. @q = current_user.klasses.ransack(params[:q])
  9. end
  10. @classes = @q.result(distinct: true)
  11. end
  12. def new
  13. @klass = @school.klasses.new
  14. end
  15. def show
  16. end
  17. def create
  18. @klass = @school.klasses.build(klass_params)
  19. if @klass.save
  20. @school.notify("#{view_context.link_to(@klass.name, faculty_klass_path(@klass))} has been created")
  21. flash[:success] = 'Class Created'
  22. redirect_to faculty_klass_path(@klass)
  23. else
  24. render :new
  25. end
  26. end
  27. def edit
  28. end
  29. def update
  30. if params[:commit] == 'add_mass_to_klass'
  31. if params[:add_to_klass]
  32. params[:add_to_klass].each do |id|
  33. @school.unassigned_pupils.find(id).enroll_in(@klass)
  34. end
  35. end
  36. render :edit
  37. else
  38. if @klass.update(klass_params)
  39. @school.notify("#{view_context.link_to(@klass.name, faculty_klass_path(@klass))} has been updated")
  40. flash[:success] = 'Class Updated'
  41. redirect_to faculty_klass_path(@klass)
  42. else
  43. render :edit
  44. end
  45. end
  46. end
  47. def destroy
  48. @klass.destroy
  49. redirect_to faculty_klasses_path
  50. end
  51. def graph_json
  52. @klasses = @school.klasses
  53. respond_to do |format|
  54. format.json {render "faculty/klasses/graphs/marks"}
  55. end
  56. end
  57. def show_graph_json
  58. respond_to do |format|
  59. format.json {render "faculty/klasses/graphs/show_graph"}
  60. end
  61. end
  62. private
  63. def set_class
  64. @klass = Klass.find(params[:id])
  65. end
  66. def klass_params
  67. params.require(:klass).permit(:name, :faculty_id, :course_id)
  68. end
  69. def verify_administrator
  70. redirect_to faculty_root_path unless signed_in_as_administrator? || current_user.klasses.include?(@klass)
  71. end
  72. end