Read about our upcoming Code of Conduct on this issue

membership_actions.rb 1.39 KB
Newer Older
1
2
3
module MembershipActions
  extend ActiveSupport::Concern

4
5
6
7
8
9
10
11
12
13
14
15
  def create
    status = Members::CreateService.new(membershipable, current_user, params).execute

    redirect_url = polymorphic_url([membershipable, :members])

    if status
      redirect_to redirect_url, notice: 'Users were successfully added.'
    else
      redirect_to redirect_url, alert: 'No users specified.'
    end
  end

16
17
18
19
20
21
22
23
  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
24
    Members::ApproveAccessRequestService.new(membershipable, current_user, params).execute
25
26
27
28
29

    redirect_to polymorphic_url([membershipable, :members])
  end

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

33
    source_type = membershipable.class.to_s.humanize(capitalize: false)
34
    notice =
35
      if member.request?
36
        "Your access request to the #{source_type} has been withdrawn."
37
      else
38
        "You left the \"#{membershipable.human_name}\" #{source_type}."
39
      end
40
    redirect_path = member.request? ? member.source : [:dashboard, membershipable.class.to_s.tableize]
41
42

    redirect_to redirect_path, notice: notice
43
44
45
46
47
48
49
50
  end

  protected

  def membershipable
    raise NotImplementedError
  end
end