summaryrefslogtreecommitdiff
path: root/app/helpers/members_helper.rb
blob: c70cd19b587a50ced7651c19cee33251ad4678e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module MembersHelper
  # Returns a `<action>_<source>_member` association, e.g.:
  # - admin_project_member, update_project_member, destroy_project_member
  # - admin_group_member, update_group_member, destroy_group_member
  def action_member_permission(action, member)
    "#{action}_#{member.type.underscore}".to_sym
  end

  def default_show_roles(member)
    can?(current_user, action_member_permission(:update, member), member) ||
      can?(current_user, action_member_permission(:destroy, member), member) ||
      can?(current_user, action_member_permission(:admin, member), member.source)
  end

  def can_see_request_access_button?(source)
    source_parent = source.respond_to?(:group) && source.group

    return false if source_parent && source.group.members.exists?(user_id: current_user.id)
    return false if source_parent && source.group.requesters.exists?(user_id: current_user.id)
    return false if source.members.exists?(user_id: current_user.id)
    return true if source.requesters.exists?(user_id: current_user.id)

    true
  end

  def remove_member_message(member, user: nil)
    user = current_user if defined?(current_user)

    text = 'Are you sure you want to '
    action =
      if member.request?
        if member.user == user
          'withdraw your access request for'
        else
          "deny #{member.user.name}'s request to join"
        end
      elsif member.invite?
        "revoke the invitation for #{member.invite_email} to join"
      else
        "remove #{member.user.name} from"
      end

    text << action << " the #{member.source.human_name} #{member.real_source_type.humanize(capitalize: false)}?"
  end

  def remove_member_title(member)
    text = " from #{member.real_source_type.humanize(capitalize: false)}"

    text.prepend(member.request? ? 'Deny access request' : 'Remove user')
  end

  def leave_confirmation_message(member_source)
    "Are you sure you want to leave the " \
    "\"#{member_source.human_name}\" #{member_source.class.to_s.humanize(capitalize: false)}?"
  end
end