Sprankelprachtig aan/afmeldsysteem

activities_controller.rb 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. @organizers = @activity.participants
  19. .joins(:person)
  20. .where(is_organizer: true)
  21. .order('people.first_name ASC')
  22. .map{|p| p.person.full_name}
  23. .join(', ')
  24. @ownparticipant = @activity.participants
  25. .find_by(person: current_person)
  26. @counts = @activity.state_counts
  27. @num_participants = @counts.values.sum
  28. end
  29. # GET /activities/new
  30. def new
  31. @activity = Activity.new
  32. end
  33. # GET /activities/1/edit
  34. def edit
  35. @non_organizers = @activity.participants.where.not(is_organizer: true)
  36. @organizers = @activity.organizers
  37. @non_organizers_options = @non_organizers.map{|p| [p.person.full_name, p.id] }
  38. @organizers_options = @organizers.map{|p| [p.person.full_name, p.id] }
  39. @non_organizers_options.sort!
  40. @organizers_options.sort!
  41. end
  42. # POST /activities
  43. # POST /activities.json
  44. def create
  45. @activity = Activity.new(activity_params)
  46. @activity.group = @group
  47. respond_to do |format|
  48. if @activity.save
  49. format.html {
  50. redirect_to group_activity_url(@group, @activity)
  51. flash_message(:info, I18n.t('activities.created'))
  52. }
  53. format.json { render :show, status: :created, location: @activity }
  54. else
  55. format.html { render :new }
  56. format.json { render json: @activity.errors, status: :unprocessable_entity }
  57. end
  58. end
  59. end
  60. # Change organizer state for a Participant
  61. def change_organizer
  62. @activity = Activity.find(params[:activity_id])
  63. @participant = @activity.participants.find(params[:participant_id])
  64. @participant.is_organizer = params[:new_state]
  65. @participant.save
  66. if params[:new_state] == "true"
  67. message = I18n.t('activities.organizers.added', name: @participant.person.full_name)
  68. else
  69. message = I18n.t('activities.organizers.removed', name: @participant.person.full_name)
  70. end
  71. flash_message(:success, message)
  72. redirect_to edit_group_activity_path(@group, @activity)
  73. end
  74. # PATCH/PUT /activities/1
  75. # PATCH/PUT /activities/1.json
  76. def update
  77. respond_to do |format|
  78. if @activity.update(activity_params)
  79. format.html {
  80. redirect_to group_activity_url(@group, @activity)
  81. flash_message(:info, I18n.t('activities.updated'))
  82. }
  83. format.json { render :show, status: :ok, location: @activity }
  84. else
  85. format.html { render :edit }
  86. format.json { render json: @activity.errors, status: :unprocessable_entity }
  87. end
  88. end
  89. end
  90. # DELETE /activities/1
  91. # DELETE /activities/1.json
  92. def destroy
  93. @activity.destroy
  94. respond_to do |format|
  95. format.html {
  96. redirect_to group_activities_url(@group)
  97. flash_message(:info, 'Activity was successfully destroyed.')
  98. }
  99. format.json { head :no_content }
  100. end
  101. end
  102. # PATCH/PUT /groups/:group_id/activities/:id/presence
  103. # PATCH/PUT /groups/:group_id/activities/:id/presence.json
  104. def presence
  105. participant = Participant.find_by(
  106. person_id: params[:person_id],
  107. activity: @activity
  108. )
  109. if !@activity.may_change?(current_person)
  110. render status: :forbidden
  111. end
  112. if params[:participant]
  113. params[:notes] = params[:participant][:notes]
  114. end
  115. participant.update_attributes(params.permit(:notes, :attending))
  116. end
  117. private
  118. # Use callbacks to share common setup or constraints between actions.
  119. def set_activity
  120. @activity = Activity.find(params[:id])
  121. end
  122. def set_group
  123. @group = Group.find(params[:group_id])
  124. end
  125. # Never trust parameters from the scary internet, only allow the white list through.
  126. def activity_params
  127. params.require(:activity).permit(:name, :description, :location, :start, :end, :deadline)
  128. end
  129. end