Sprankelprachtig aan/afmeldsysteem

people_controller.rb 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. class PeopleController < ApplicationController
  2. before_action :set_person, only: [:show, :edit, :update, :destroy]
  3. before_action :set_person_from_token, only: [:calendar]
  4. before_action :require_login!, except: [:calendar]
  5. before_action :require_admin!, except: [:calendar, :show]
  6. # GET /people
  7. # GET /people.json
  8. def index
  9. @people = Person.all
  10. end
  11. # GET /people/1
  12. # GET /people/1.json
  13. def show
  14. require_admin! if @person != current_person
  15. end
  16. # GET /people/new
  17. def new
  18. @person = Person.new
  19. end
  20. # GET /people/1/edit
  21. def edit; end
  22. # POST /people
  23. # POST /people.json
  24. def create
  25. @person = Person.new(person_params)
  26. respond_to do |format|
  27. if @person.save
  28. format.html do
  29. flash_message(:success, I18n.t('person.created'))
  30. redirect_to @person
  31. end
  32. format.json { render :show, status: :created, location: @person }
  33. else
  34. format.html { render :new }
  35. format.json { render json: @person.errors, status: :unprocessable_entity }
  36. end
  37. end
  38. end
  39. def mass_new; end
  40. def mass_create
  41. require 'csv'
  42. uploaded_io = params[:spreadsheet]
  43. result = Person.from_csv(uploaded_io.read)
  44. flash_message(:success, "#{result.count} people created")
  45. redirect_to :people
  46. end
  47. # PATCH/PUT /people/1
  48. # PATCH/PUT /people/1.json
  49. def update
  50. respond_to do |format|
  51. if @person.update(person_params)
  52. format.html do
  53. flash_message(:success, I18n.t('person.updated'))
  54. redirect_to @person
  55. end
  56. format.json { render :show, status: :ok, location: @person }
  57. else
  58. format.html { render :edit }
  59. format.json { render json: @person.errors, status: :unprocessable_entity }
  60. end
  61. end
  62. end
  63. # DELETE /people/1
  64. # DELETE /people/1.json
  65. def destroy
  66. @person.destroy
  67. respond_to do |format|
  68. format.html do
  69. flash_message(:success, I18n.t('person.destroyed'))
  70. redirect_to people_url
  71. end
  72. format.json { head :no_content }
  73. end
  74. end
  75. # GET /c/:calendar_token
  76. def calendar
  77. response.content_type = 'text/calendar'
  78. cal = @person.calendar_feed
  79. render plain: cal.to_ical
  80. end
  81. private
  82. # Use callbacks to share common setup or constraints between actions.
  83. def set_person
  84. @person = Person.find(params[:id])
  85. end
  86. # Set person from calendar token
  87. def set_person_from_token
  88. @person = Person.find_by(calendar_token: params[:calendar_token])
  89. end
  90. # Never trust parameters from the scary internet, only allow the white list through.
  91. def person_params
  92. params.require(:person).permit(:first_name, :infix, :last_name, :email, :is_admin)
  93. end
  94. end