membership_actions.rb 1.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module MembershipActions
  extend ActiveSupport::Concern
  include MembersHelper

  def request_access
    membershipable.request_access(current_user)

    redirect_to polymorphic_path(membershipable),
                notice: 'Your request for access has been queued for review.'
  end

  def approve_access_request
    @member = membershipable.members.request.find(params[:id])

    return render_403 unless can?(current_user, action_member_permission(:update, @member), @member)

    @member.accept_request

    redirect_to polymorphic_url([membershipable, :members])
  end

  def leave
    @member = membershipable.members.find_by(user_id: current_user)
24
    Members::DestroyService.new(@member, current_user).execute
Rémy Coutable's avatar
Rémy Coutable committed
25

26
    source_type = @member.real_source_type.humanize(capitalize: false)
27
28
29
    notice =
      if @member.request?
        "Your access request to the #{source_type} has been withdrawn."
30
      else
31
        "You left the \"#{@member.source.human_name}\" #{source_type}."
32
      end
33
34
35
    redirect_path = @member.request? ? @member.source : [:dashboard, @member.real_source_type.tableize]

    redirect_to redirect_path, notice: notice
36
37
38
39
40
41
42
43
  end

  protected

  def membershipable
    raise NotImplementedError
  end
end