123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- class MembersController < ApplicationController
- include GroupsHelper
- before_action :set_group
- before_action :set_member, only: [:show, :edit, :update, :destroy, :promote, :demote]
- before_action :require_leader!, except: [:index]
- # GET /members
- # GET /members.json
- def index
- @admin = @group.leader?(current_person)
- @members = @group.members
- .joins(:person)
- .order('members.is_leader DESC, people.first_name ASC')
- end
- # GET /members/1
- # GET /members/1.json
- def show; end
- # GET /members/new
- def new
- @member = Member.new
- @possible_members = Person.where.not(id: @group.person_ids)
- end
- def invite
- @person = Person.new
- end
- def promote
- @member.update!(is_leader: true)
- flash_message(:success, I18n.t('groups.leader_added', name: @member.person.full_name))
- redirect_to group_members_path(@group)
- end
- def demote
- @member.update!(is_leader: false)
- flash_message(:success, I18n.t('groups.leader_removed', name: @member.person.full_name))
- redirect_to group_members_path(@group)
- end
- def process_invite
- @person = Person.find_by(email: params[:person][:email])
- new_rec = false
- unless @person
- @person = Person.new(invite_params)
- new_rec = true
- unless @person.save
- respond_to do |format|
- format.html { render 'invite' }
- format.json { render json: @person.errors, status: :unprocessable_entity }
- end
- return
- end
- end
- @member = Member.new(person: @person, group: @group, is_leader: false)
- @member.save!
- respond_to do |format|
- format.html do
- invited = ""
- invited = "invited to Aardbei and " if new_rec
- flash_message(
- :success,
- "#{@person.full_name} #{invited}added to group."
- )
- redirect_to group_members_path(@group)
- end
- format.json { render :show, status: :created, location: @person }
- end
- end
- # GET /members/1/edit
- def edit
- @possible_members = Person.where.not(id: @group.person_ids)
- end
- # POST /members
- # POST /members.json
- def create
- @member = Member.new(member_params)
- @member.group = @group
- respond_to do |format|
- if @member.save
- format.html do
- redirect_to group_member_url(@group, @member)
- flash_message(:info, I18n.t('groups.member_added', name: @member.person.full_name))
- end
- format.json { render :show, status: :created, location: @member }
- else
- @possible_members = Person.where.not(id: @group.person_ids)
- format.html { render :new }
- format.json { render json: @member.errors, status: :unprocessable_entity }
- end
- end
- end
- # PATCH/PUT /members/1
- # PATCH/PUT /members/1.json
- def update
- respond_to do |format|
- if @member.update(member_params)
- format.html do
- redirect_to group_member_url(@group, @member)
- flash_message(:info, I18n.t('groups.member_updated'))
- end
- format.json { render :show, status: :ok, location: @member }
- else
- @possible_members = Person.where.not(id: @group.person_ids)
- format.html { render :edit }
- format.json { render json: @member.errors, status: :unprocessable_entity }
- end
- end
- end
- # DELETE /members/1
- # DELETE /members/1.json
- def destroy
- @member.destroy
- respond_to do |format|
- format.html do
- redirect_to group_members_url(@group)
- flash_message(:info, I18n.t('groups.member_removed', name: @member.person.full_name))
- end
- format.json { head :no_content }
- end
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def set_member
- @member = Member.find(params[:id])
- end
- def set_group
- @group = Group.find(params[:group_id])
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def member_params
- params.require(:member).permit(:person_id, :is_leader)
- end
- # Never trust parameters from the scary internet, only allow the white list
- # through. Note: differs from the ones in PeopleController because
- # creating admins is not allowed.
- def invite_params
- params.require(:person).permit(:first_name, :infix, :last_name, :email)
- end
- end
|