Module: Memberships::Base

Extended by:
ActiveSupport::Concern
Included in:
Membership
Defined in:
app/models/concerns/memberships/base.rb

Instance Method Summary collapse

Instance Method Details

#emailObject



117
118
119
# File 'app/models/concerns/memberships/base.rb', line 117

def email
  user&.email || user_email.presence || invitation&.email
end

#first_nameObject



125
126
127
# File 'app/models/concerns/memberships/base.rb', line 125

def first_name
  user&.first_name || user_first_name
end

#first_name_last_initialObject



138
139
140
# File 'app/models/concerns/memberships/base.rb', line 138

def first_name_last_initial
  [first_name, last_initial].select(&:present?).join(" ")
end

#full_nameObject



121
122
123
# File 'app/models/concerns/memberships/base.rb', line 121

def full_name
  user&.full_name || [first_name.presence, last_name.presence].join(" ").presence || email
end

#label_stringObject



48
49
50
# File 'app/models/concerns/memberships/base.rb', line 48

def label_string
  full_name
end

#last_admin?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
# File 'app/models/concerns/memberships/base.rb', line 74

def last_admin?
  return false unless admin?
  return false unless user.present?
  team.memberships.current.select(&:admin?) == [self]
end

#last_initialObject



133
134
135
136
# File 'app/models/concerns/memberships/base.rb', line 133

def last_initial
  return nil unless last_name.present?
  "#{last_name[0]}."
end

#last_nameObject



129
130
131
# File 'app/models/concerns/memberships/base.rb', line 129

def last_name
  user&.last_name || user_last_name
end

#nameObject



44
45
46
# File 'app/models/concerns/memberships/base.rb', line 44

def name
  full_name
end

#nullify_userObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/concerns/memberships/base.rb', line 80

def nullify_user
  if last_admin?
    raise RemovingLastTeamAdminException.new("You can't remove the last team admin.")
  end

  if (user_was = user)
    unless user_first_name.present?
      self.user_first_name = user.first_name
    end

    unless user_last_name.present?
      self.user_last_name = user.last_name
    end

    unless .present?
      self. = user.profile_photo_id
    end

    unless user_email.present?
      self.user_email = user.email
    end

    self.user = nil
    save

    user_was.invalidate_ability_cache

    user_was.update(
      current_team: user_was.teams.first,
      former_user: user_was.teams.empty?
    )
  end

  # we do this here just in case by some weird chance an active membership had an invitation attached.
  invitation&.destroy
end

#publish_changed_quantityObject



156
157
158
# File 'app/models/concerns/memberships/base.rb', line 156

def publish_changed_quantity
  ActiveSupport::Notifications.instrument("memberships.quantity-changed", {team:})
end

#remove_user_profile_photoObject



152
153
154
# File 'app/models/concerns/memberships/base.rb', line 152

def 
  .purge
end

#role_ids=(ids) ⇒ Object

we overload this method so that when setting the list of role ids associated with a membership, admins can never remove the last admin of a team.



55
56
57
58
59
60
61
62
63
64
# File 'app/models/concerns/memberships/base.rb', line 55

def role_ids=(ids)
  # if this membership was an admin, and the new list of role ids don't include admin.
  if admin? && !ids.include?(Role.admin.id)
    unless team.admins.count > 1
      raise RemovingLastTeamAdminException.new("You can't remove the last team admin.")
    end
  end

  super(ids)
end

#should_receive_notifications?Boolean

TODO utilize this. members shouldn’t receive notifications unless they are either an active user or an outstanding invitation.

Returns:

  • (Boolean)


144
145
146
# File 'app/models/concerns/memberships/base.rb', line 144

def should_receive_notifications?
  invitation.present? || user.present?
end

#tombstone?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/concerns/memberships/base.rb', line 70

def tombstone?
  user.nil? && invitation.nil? && !platform_agent
end

#unclaimed?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/models/concerns/memberships/base.rb', line 66

def unclaimed?
  user.nil? && !invitation.nil?
end

#user_profile_photo_removal?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'app/models/concerns/memberships/base.rb', line 148

def 
  .present?
end