Sprankelprachtig aan/afmeldsysteem

members_controller.rb 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. class MembersController < ApplicationController
  2. include GroupsHelper
  3. before_action :set_group
  4. before_action :set_member, only: [:show, :edit, :update, :destroy, :promote, :demote]
  5. before_action :require_leader!, except: [:index]
  6. # GET /members
  7. # GET /members.json
  8. def index
  9. @admin = @group.leader?(current_person)
  10. @members = @group.members
  11. .joins(:person)
  12. .order('members.is_leader DESC, people.first_name ASC')
  13. end
  14. # GET /members/1
  15. # GET /members/1.json
  16. def show; end
  17. # GET /members/new
  18. def new
  19. @member = Member.new
  20. @possible_members = Person.where.not(id: @group.person_ids)
  21. end
  22. def invite
  23. @person = Person.new
  24. end
  25. def promote
  26. @member.update!(is_leader: true)
  27. flash_message(:success, I18n.t('groups.leader_added', name: @member.person.full_name))
  28. redirect_to group_members_path(@group)
  29. end
  30. def demote
  31. @member.update!(is_leader: false)
  32. flash_message(:success, I18n.t('groups.leader_removed', name: @member.person.full_name))
  33. redirect_to group_members_path(@group)
  34. end
  35. def process_invite
  36. @person = Person.find_by(email: params[:person][:email])
  37. new_rec = false
  38. unless @person
  39. @person = Person.new(invite_params)
  40. new_rec = true
  41. unless @person.save
  42. respond_to do |format|
  43. format.html { render 'invite' }
  44. format.json { render json: @person.errors, status: :unprocessable_entity }
  45. end
  46. return
  47. end
  48. end
  49. @member = Member.new(person: @person, group: @group, is_leader: false)
  50. @member.save!
  51. respond_to do |format|
  52. format.html do
  53. invited = ""
  54. invited = "invited to Aardbei and " if new_rec
  55. flash_message(
  56. :success,
  57. "#{@person.full_name} #{invited}added to group."
  58. )
  59. redirect_to group_members_path(@group)
  60. end
  61. format.json { render :show, status: :created, location: @person }
  62. end
  63. end
  64. # GET /members/1/edit
  65. def edit
  66. @possible_members = Person.where.not(id: @group.person_ids)
  67. end
  68. # POST /members
  69. # POST /members.json
  70. def create
  71. @member = Member.new(member_params)
  72. @member.group = @group
  73. respond_to do |format|
  74. if @member.save
  75. format.html do
  76. redirect_to group_member_url(@group, @member)
  77. flash_message(:info, I18n.t('groups.member_added', name: @member.person.full_name))
  78. end
  79. format.json { render :show, status: :created, location: @member }
  80. else
  81. @possible_members = Person.where.not(id: @group.person_ids)
  82. format.html { render :new }
  83. format.json { render json: @member.errors, status: :unprocessable_entity }
  84. end
  85. end
  86. end
  87. # PATCH/PUT /members/1
  88. # PATCH/PUT /members/1.json
  89. def update
  90. respond_to do |format|
  91. if @member.update(member_params)
  92. format.html do
  93. redirect_to group_member_url(@group, @member)
  94. flash_message(:info, I18n.t('groups.member_updated'))
  95. end
  96. format.json { render :show, status: :ok, location: @member }
  97. else
  98. @possible_members = Person.where.not(id: @group.person_ids)
  99. format.html { render :edit }
  100. format.json { render json: @member.errors, status: :unprocessable_entity }
  101. end
  102. end
  103. end
  104. # DELETE /members/1
  105. # DELETE /members/1.json
  106. def destroy
  107. @member.destroy
  108. respond_to do |format|
  109. format.html do
  110. redirect_to group_members_url(@group)
  111. flash_message(:info, I18n.t('groups.member_removed', name: @member.person.full_name))
  112. end
  113. format.json { head :no_content }
  114. end
  115. end
  116. private
  117. # Use callbacks to share common setup or constraints between actions.
  118. def set_member
  119. @member = Member.find(params[:id])
  120. end
  121. def set_group
  122. @group = Group.find(params[:group_id])
  123. end
  124. # Never trust parameters from the scary internet, only allow the white list through.
  125. def member_params
  126. params.require(:member).permit(:person_id, :is_leader)
  127. end
  128. # Never trust parameters from the scary internet, only allow the white list
  129. # through. Note: differs from the ones in PeopleController because
  130. # creating admins is not allowed.
  131. def invite_params
  132. params.require(:person).permit(:first_name, :infix, :last_name, :email)
  133. end
  134. end