membership_actions.rb 1.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
module MembershipActions
  extend ActiveSupport::Concern

  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
12
    Members::ApproveAccessRequestService.new(membershipable, current_user, user_id: params[:id]).execute
13
14
15
16
17

    redirect_to polymorphic_url([membershipable, :members])
  end

  def leave
18
19
    @member = membershipable.members.find_by(user_id: current_user) ||
      membershipable.requesters.find_by(user_id: current_user)
20
    Members::DestroyService.new(@member, current_user).execute
Rémy Coutable's avatar
Rémy Coutable committed
21

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

    redirect_to redirect_path, notice: notice
32
33
34
35
36
37
38
39
  end

  protected

  def membershipable
    raise NotImplementedError
  end
end