Sprankelprachtig aan/afmeldsysteem

activities_controller.rb 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. end
  36. # POST /activities
  37. # POST /activities.json
  38. def create
  39. @activity = Activity.new(activity_params)
  40. @activity.group = @group
  41. respond_to do |format|
  42. if @activity.save
  43. format.html {
  44. redirect_to group_activity_url(@group, @activity)
  45. flash_message(:info, I18n.t('activities.created'))
  46. }
  47. format.json { render :show, status: :created, location: @activity }
  48. else
  49. format.html { render :new }
  50. format.json { render json: @activity.errors, status: :unprocessable_entity }
  51. end
  52. end
  53. end
  54. # PATCH/PUT /activities/1
  55. # PATCH/PUT /activities/1.json
  56. def update
  57. respond_to do |format|
  58. if @activity.update(activity_params)
  59. format.html {
  60. redirect_to group_activity_url(@group, @activity)
  61. flash_message(:info, I18n.t('activities.updated'))
  62. }
  63. format.json { render :show, status: :ok, location: @activity }
  64. else
  65. format.html { render :edit }
  66. format.json { render json: @activity.errors, status: :unprocessable_entity }
  67. end
  68. end
  69. end
  70. # DELETE /activities/1
  71. # DELETE /activities/1.json
  72. def destroy
  73. @activity.destroy
  74. respond_to do |format|
  75. format.html {
  76. redirect_to group_activities_url(@group)
  77. flash_message(:info, 'Activity was successfully destroyed.')
  78. }
  79. format.json { head :no_content }
  80. end
  81. end
  82. # PATCH/PUT /groups/:group_id/activities/:id/presence
  83. # PATCH/PUT /groups/:group_id/activities/:id/presence.json
  84. def presence
  85. participant = Participant.find_by(
  86. person_id: params[:person_id],
  87. activity: @activity
  88. )
  89. if !@activity.may_change?(current_person)
  90. render status: :forbidden
  91. end
  92. if params[:participant]
  93. params[:notes] = params[:participant][:notes]
  94. end
  95. participant.update_attributes(params.permit(:notes, :attending))
  96. end
  97. private
  98. # Use callbacks to share common setup or constraints between actions.
  99. def set_activity
  100. @activity = Activity.find(params[:id])
  101. end
  102. def set_group
  103. @group = Group.find(params[:group_id])
  104. end
  105. # Never trust parameters from the scary internet, only allow the white list through.
  106. def activity_params
  107. params.require(:activity).permit(:name, :description, :location, :start, :end, :deadline)
  108. end
  109. end