Sprankelprachtig aan/afmeldsysteem

groups_controller.rb 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. class GroupsController < ApplicationController
  2. include GroupsHelper
  3. before_action :set_group, only: [:show, :edit, :update, :destroy]
  4. before_action :require_admin!, only: [:index]
  5. before_action :require_membership!, only: [:show]
  6. before_action :require_leader!, only: [:edit, :update, :destroy]
  7. # GET /groups
  8. # GET /groups.json
  9. def index
  10. @groups = Group.all
  11. end
  12. def user_groups
  13. @groups = current_person.groups
  14. end
  15. # GET /groups/1
  16. # GET /groups/1.json
  17. def show
  18. @organized_activities = current_person.organized_activities.
  19. joins(:activity).where(
  20. 'activities.group_id': @group.id
  21. )
  22. if @organized_activities.count > 0
  23. @groupmenu = 'col-md-6'
  24. else
  25. @groupmenu = 'col-md-12'
  26. end
  27. @upcoming = @group.activities
  28. .where('start > ?', Date.today)
  29. .order('start ASC')
  30. @upcoming_ps = Participant
  31. .where(person: current_person)
  32. .where(activity: @upcoming)
  33. .map{ |p| [p.activity_id, p]}
  34. .to_h
  35. end
  36. # GET /groups/new
  37. def new
  38. @group = Group.new
  39. end
  40. # GET /groups/1/edit
  41. def edit
  42. end
  43. # POST /groups
  44. # POST /groups.json
  45. def create
  46. @group = Group.new(group_params)
  47. respond_to do |format|
  48. if @group.save
  49. format.html {
  50. redirect_to @group
  51. flash_message(:info, I18n.t('groups.created'))
  52. }
  53. format.json { render :show, status: :created, location: @group }
  54. else
  55. format.html { render :new }
  56. format.json { render json: @group.errors, status: :unprocessable_entity }
  57. end
  58. end
  59. end
  60. # PATCH/PUT /groups/1
  61. # PATCH/PUT /groups/1.json
  62. def update
  63. respond_to do |format|
  64. if @group.update(group_params)
  65. format.html {
  66. redirect_to @group
  67. flash_message(:info, I18n.t('groups.updated'))
  68. }
  69. format.json { render :show, status: :ok, location: @group }
  70. else
  71. format.html { render :edit }
  72. format.json { render json: @group.errors, status: :unprocessable_entity }
  73. end
  74. end
  75. end
  76. # DELETE /groups/1
  77. # DELETE /groups/1.json
  78. def destroy
  79. @group.destroy
  80. respond_to do |format|
  81. format.html {
  82. redirect_to groups_url
  83. flash_message(:info, I18n.t('groups.destroyed'))
  84. }
  85. format.json { head :no_content }
  86. end
  87. end
  88. private
  89. # Use callbacks to share common setup or constraints between actions.
  90. def set_group
  91. @group = Group.find(params[:id])
  92. end
  93. # Never trust parameters from the scary internet, only allow the white list through.
  94. def group_params
  95. params.require(:group).permit(:name)
  96. end
  97. end