membership_actions.rb 1.06 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, params).execute
13
14
15
16
17

    redirect_to polymorphic_url([membershipable, :members])
  end

  def leave
18
19
    member = Members::DestroyService.new(membershipable, current_user, user_id: current_user.id).
      execute(:all)
Rémy Coutable's avatar
Rémy Coutable committed
20

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

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

  protected

  def membershipable
    raise NotImplementedError
  end
end