Sprankelprachtig aan/afmeldsysteem

activities_controller.rb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Provides read-only access to Activities.
  2. module Api
  3. class ActivitiesController < ApiController
  4. has_no_activity = [:index]
  5. # Session-based authentication/authorization
  6. before_action :set_activity, except: has_no_activity
  7. before_action :require_membership!, except: has_no_activity
  8. before_action :api_require_admin!, only: has_no_activity
  9. skip_before_action :api_require_authentication!, :set_activity, :require_membership!, if: 'request.authorization'
  10. # Group API-key-based authentication/authorization
  11. before_action :api_auth_group_token, if: 'request.authorization'
  12. before_action :set_activity_with_group, if: 'request.authorization'
  13. # GET /api/activities
  14. def index
  15. @activities = Activity.all
  16. end
  17. # GET /api/activities/1
  18. def show; end
  19. # GET /api/activities/1/response_summary
  20. def response_summary
  21. as = @activity
  22. .participants
  23. .joins(:person)
  24. .order('people.first_name ASC')
  25. present = as.where(attending: true)
  26. unknown = as.where(attending: nil)
  27. absent = as.where(attending: false)
  28. presentnames = present.map { |p| p.person.first_name }
  29. unknownnames = unknown.map { |p| p.person.first_name }
  30. absentnames = absent.map { |p| p.person.first_name }
  31. present_mess = if presentnames.positive?
  32. I18n.t('activities.participant.these_present', count: present.count, names: presentnames.join(', '))
  33. else
  34. I18n.t('activities.participant.none_present')
  35. end
  36. unknown_mess = if unknownnames.positive?
  37. I18n.t('activities.participant.these_unknown', count: unknown.count, names: unknownnames.join(', '))
  38. else
  39. I18n.t('activities.participant.none_unknown')
  40. end
  41. absent_mess = if absentnames.positive?
  42. I18n.t('activities.participant.these_absent', count: absent.count, names: absentnames.join(', '))
  43. else
  44. I18n.t('activities.participant.none_absent')
  45. end
  46. @summary = {
  47. present: {
  48. count: present.count,
  49. names: presentnames,
  50. message: present_mess
  51. },
  52. unknown: {
  53. count: unknown.count,
  54. names: unknownnames,
  55. message: unknown_mess
  56. },
  57. absent: {
  58. count: absent.count,
  59. names: absentnames,
  60. message: absent_mess
  61. }
  62. }
  63. end
  64. def presence
  65. participant = Participant.find_by(
  66. person_id: params[:person_id],
  67. activity: @activity
  68. )
  69. participant.update!(params.permit(:attending))
  70. head :no_content
  71. end
  72. private
  73. # Set activity from the :id-parameter
  74. def set_activity
  75. @activity = Activity.find(params[:id])
  76. @group = @activity.group
  77. end
  78. # Set activity from the :id-parameter, and assert that it belongs to the set @group.
  79. def set_activity_with_group
  80. @activity = Activity.find_by id: params[:id]
  81. unless @activity
  82. head :not_found
  83. return
  84. end
  85. head :unauthorized unless @activity.group == @group
  86. end
  87. end
  88. end