Sprankelprachtig aan/afmeldsysteem

activities_controller.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. class ActivitiesController < ApplicationController
  2. include GroupsHelper
  3. before_action :set_activity, only: [:show, :edit, :update, :destroy, :presence]
  4. before_action :set_group
  5. before_action :require_membership!
  6. # GET /groups/:id/activities
  7. # GET /activities.json
  8. def index
  9. @activities = @group.activities
  10. end
  11. # GET /activities/1
  12. # GET /activities/1.json
  13. def show
  14. @participants = @activity.participants
  15. .joins(:person)
  16. .order(attending: :desc)
  17. .order('people.first_name ASC')
  18. @counts = @activity.state_counts
  19. @num_participants = @counts.values.sum
  20. end
  21. # GET /activities/new
  22. def new
  23. @activity = Activity.new
  24. end
  25. # GET /activities/1/edit
  26. def edit
  27. end
  28. # POST /activities
  29. # POST /activities.json
  30. def create
  31. @activity = Activity.new(activity_params)
  32. @activity.group = @group
  33. respond_to do |format|
  34. if @activity.save
  35. format.html {
  36. redirect_to group_activity_url(@group, @activity)
  37. flash_message(:info, 'Activity was successfully created.')
  38. }
  39. format.json { render :show, status: :created, location: @activity }
  40. else
  41. format.html { render :new }
  42. format.json { render json: @activity.errors, status: :unprocessable_entity }
  43. end
  44. end
  45. end
  46. # PATCH/PUT /activities/1
  47. # PATCH/PUT /activities/1.json
  48. def update
  49. respond_to do |format|
  50. if @activity.update(activity_params)
  51. format.html {
  52. redirect_to group_activity_url(@group, @activity)
  53. flash_message(:info, 'Activity was successfully updated.')
  54. }
  55. format.json { render :show, status: :ok, location: @activity }
  56. else
  57. format.html { render :edit }
  58. format.json { render json: @activity.errors, status: :unprocessable_entity }
  59. end
  60. end
  61. end
  62. # DELETE /activities/1
  63. # DELETE /activities/1.json
  64. def destroy
  65. @activity.destroy
  66. respond_to do |format|
  67. format.html {
  68. redirect_to group_activities_url(@group)
  69. flash_message(:info, 'Activity was successfully destroyed.')
  70. }
  71. format.json { head :no_content }
  72. end
  73. end
  74. # PATCH/PUT /groups/:group_id/activities/:id/presence
  75. # PATCH/PUT /groups/:group_id/activities/:id/presence.json
  76. def presence
  77. participant = Participant.find_by(
  78. person_id: params[:person_id],
  79. activity: @activity
  80. )
  81. if !@activity.may_change?(current_person)
  82. render status: :forbidden
  83. end
  84. if params[:participant]
  85. params[:notes] = params[:participant][:notes]
  86. end
  87. participant.update_attributes(params.permit(:notes, :attending))
  88. end
  89. private
  90. # Use callbacks to share common setup or constraints between actions.
  91. def set_activity
  92. @activity = Activity.find(params[:id])
  93. end
  94. def set_group
  95. @group = Group.find(params[:group_id])
  96. end
  97. # Never trust parameters from the scary internet, only allow the white list through.
  98. def activity_params
  99. params.require(:activity).permit(:public_name, :secret_name, :description, :location, :start, :end, :deadline, :show_hidden)
  100. end
  101. end