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



119
120
121
# File 'app/models/concerns/memberships/base.rb', line 119

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

#first_nameObject



127
128
129
# File 'app/models/concerns/memberships/base.rb', line 127

def first_name
  user&.first_name || user_first_name
end

#first_name_last_initialObject



140
141
142
# File 'app/models/concerns/memberships/base.rb', line 140

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

#full_nameObject



123
124
125
# File 'app/models/concerns/memberships/base.rb', line 123

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

#is_first_membership?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'app/models/concerns/memberships/base.rb', line 166

def is_first_membership?
  team.memberships.count == 1 && team.memberships.first.id == id
end

#label_stringObject



50
51
52
# File 'app/models/concerns/memberships/base.rb', line 50

def label_string
  full_name
end

#last_admin?Boolean

Returns:

  • (Boolean)


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

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

#last_initialObject



135
136
137
138
# File 'app/models/concerns/memberships/base.rb', line 135

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

#last_nameObject



131
132
133
# File 'app/models/concerns/memberships/base.rb', line 131

def last_name
  user&.last_name || user_last_name
end

#nameObject



46
47
48
# File 'app/models/concerns/memberships/base.rb', line 46

def name
  full_name
end

#nullify_userObject



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
116
117
# File 'app/models/concerns/memberships/base.rb', line 82

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



158
159
160
# File 'app/models/concerns/memberships/base.rb', line 158

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

#remove_user_profile_photoObject



154
155
156
# File 'app/models/concerns/memberships/base.rb', line 154

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.



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

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

#set_team_time_zoneObject



162
163
164
# File 'app/models/concerns/memberships/base.rb', line 162

def set_team_time_zone
  team.set_time_zone_from_user(user)
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)


146
147
148
# File 'app/models/concerns/memberships/base.rb', line 146

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

#tombstone?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/concerns/memberships/base.rb', line 72

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

#unclaimed?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/concerns/memberships/base.rb', line 68

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

#user_profile_photo_removal?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'app/models/concerns/memberships/base.rb', line 150

def 
  .present?
end