Sprankelprachtig aan/afmeldsysteem

groups_controller.rb 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. class GroupsController < ApplicationController
  2. include GroupsHelper
  3. before_action :require_login!
  4. before_action :set_group, only: [:show, :edit, :update, :destroy, :create_default_subgroup, :update_default_subgroup, :destroy_default_subgroup, :mass_add_members, :process_mass_add_members]
  5. before_action :set_default_subgroup, only: [:update_default_subgroup, :destroy_default_subgroup]
  6. before_action :require_admin!, only: [:index]
  7. before_action :require_membership!, only: [:show]
  8. before_action :require_leader!, only: [:edit, :update, :destroy, :create_default_subgroup, :update_default_subgroup, :destroy_default_subgroup, :process_mass_add_members, :mass_add_members]
  9. # GET /groups
  10. # GET /groups.json
  11. def index
  12. @groups = Group.all
  13. end
  14. def user_groups
  15. @groups = current_person.groups
  16. end
  17. # GET /groups/1
  18. # GET /groups/1.json
  19. def show
  20. @organized_activities = current_person
  21. .organized_activities
  22. .joins(:activity)
  23. .where('activities.group_id': @group.id)
  24. .where('start > ?', Time.zone.today)
  25. @groupmenu = if @organized_activities.any?
  26. 'col-md-6'
  27. else
  28. 'col-md-12'
  29. end
  30. @upcoming = @group.activities
  31. .where('start > ?', Time.zone.today)
  32. .order('start ASC')
  33. @upcoming_ps = Participant
  34. .where(person: current_person)
  35. .where(activity: @upcoming)
  36. .map { |p| [p.activity_id, p] }
  37. .to_h
  38. end
  39. # GET /groups/new
  40. def new
  41. @group = Group.new
  42. end
  43. # GET /groups/1/edit
  44. def edit
  45. @defaultsubgroup = DefaultSubgroup.new
  46. end
  47. # POST /groups
  48. # POST /groups.json
  49. def create
  50. @group = Group.new(group_params)
  51. respond_to do |format|
  52. if @group.save
  53. format.html do
  54. redirect_to @group
  55. flash_message(:info, I18n.t('groups.created'))
  56. end
  57. format.json { render :show, status: :created, location: @group }
  58. else
  59. format.html { render :new }
  60. format.json { render json: @group.errors, status: :unprocessable_entity }
  61. end
  62. end
  63. end
  64. # PATCH/PUT /groups/1
  65. # PATCH/PUT /groups/1.json
  66. def update
  67. respond_to do |format|
  68. if @group.update(group_params)
  69. format.html do
  70. redirect_to @group
  71. flash_message(:info, I18n.t('groups.updated'))
  72. end
  73. format.json { render :show, status: :ok, location: @group }
  74. else
  75. @defaultsubgroup = DefaultSubgroup.new
  76. format.html { render :edit }
  77. format.json { render json: @group.errors, status: :unprocessable_entity }
  78. end
  79. end
  80. end
  81. # DELETE /groups/1
  82. # DELETE /groups/1.json
  83. def destroy
  84. @group.destroy
  85. respond_to do |format|
  86. format.html do
  87. redirect_to groups_url
  88. flash_message(:info, I18n.t('groups.destroyed'))
  89. end
  90. format.json { head :no_content }
  91. end
  92. end
  93. def mass_add_members; end
  94. def process_mass_add_members
  95. require 'csv'
  96. uploaded_io = params[:spreadsheet]
  97. result = Person.from_csv(uploaded_io.read)
  98. result.each do |p|
  99. m = Member.find_by(person: p, group: @group)
  100. unless m
  101. m = Member.new(person: p, group: @group)
  102. m.save!
  103. end
  104. end
  105. flash_message(:success, I18n.t('groups.mass_add_success', count: result.count))
  106. redirect_to group_members_path(@group)
  107. end
  108. # POST /groups/:id/default_subgroups
  109. def create_default_subgroup
  110. @defaultsubgroup = DefaultSubgroup.new(default_subgroup_params)
  111. @defaultsubgroup.group = @group
  112. if @defaultsubgroup.save
  113. flash_message(:success, I18n.t('defaultsubgroups.created'))
  114. redirect_to edit_group_path(@group)
  115. else
  116. flash_message(:danger, I18n.t('defaultsubgroups.create_failed'))
  117. render :edit
  118. end
  119. end
  120. # PATCH /groups/:id/default_subgroups/:default_subgroup_id
  121. def update_default_subgroup
  122. if @defaultsubgroup.update(default_subgroup_params)
  123. flash_message(:success, I18n.t('defaultsubgroups.updated'))
  124. redirect_to edit_group_path(@group)
  125. else
  126. flash_message(:danger, I18n.t('defaultsubgroups.update_failed'))
  127. render :edit
  128. end
  129. end
  130. # DELETE /groups/:id/default_subgroups/:default_subgroup_id
  131. def destroy_default_subgroup
  132. @defaultsubgroup.destroy
  133. flash_message(:info, I18n.t('defaultsubgroups.destroyed'))
  134. redirect_to edit_group_path(@group)
  135. end
  136. private
  137. # Use callbacks to share common setup or constraints between actions.
  138. def set_group
  139. @group = Group.find(params[:group_id] || params[:id])
  140. end
  141. # Retrieve DefaultSubgroup to update or delete
  142. def set_default_subgroup
  143. @defaultsubgroup = DefaultSubgroup.find(params[:default_subgroup_id])
  144. end
  145. # Never trust parameters from the scary internet, only allow the white list through.
  146. def group_params
  147. params.require(:group).permit(:name)
  148. end
  149. def default_subgroup_params
  150. params.require(:default_subgroup).permit(:name, :is_assignable)
  151. end
  152. end