Module: Users::Base

Extended by:
ActiveSupport::Concern
Included in:
User
Defined in:
app/models/concerns/users/base.rb

Instance Method Summary collapse

Instance Method Details

#administrating_team_idsObject

TODO github.com/bullet-train-co/bullet_train-base/pull/121 should have removed this, but it caused errors.



120
121
122
# File 'app/models/concerns/users/base.rb', line 120

def administrating_team_ids
  parent_ids_for(Role.admin, :memberships, :team)
end

#create_default_teamObject



72
73
74
75
76
77
# File 'app/models/concerns/users/base.rb', line 72

def create_default_team
  # This creates a `Membership`, because `User` `has_many :teams, through: :memberships`
  default_team = teams.create(name: I18n.t("teams.new.default_team_name"), time_zone: time_zone)
  memberships.find_by(team: default_team).update(user_email: email, role_ids: [Role.admin.id])
  update(current_team: default_team)
end

#details_provided?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/concerns/users/base.rb', line 64

def details_provided?
  first_name.present? && last_name.present? && current_team.name.present?
end

#developer?Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
# File 'app/models/concerns/users/base.rb', line 148

def developer?
  return false unless ENV["DEVELOPER_EMAILS"]
  # we use email_was so they can't try setting their email to the email of an admin.
  return false unless email_was
  ENV["DEVELOPER_EMAILS"].split(",").include?(email_was)
end

#email_is_oauth_placeholder?Boolean

Returns:

  • (Boolean)


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

def email_is_oauth_placeholder?
  !!email.match(/noreply@\h{32}\.example\.com/)
end

#formatted_email_addressObject



111
112
113
114
115
116
117
# File 'app/models/concerns/users/base.rb', line 111

def formatted_email_address
  if details_provided?
    "\"#{first_name} #{last_name}\" <#{email}>"
  else
    email
  end
end

#full_nameObject



60
61
62
# File 'app/models/concerns/users/base.rb', line 60

def full_name
  [first_name_was, last_name_was].select(&:present?).join(" ")
end

#invalidate_ability_cacheObject

TODO github.com/bullet-train-co/bullet_train-base/pull/121 should have removed this, but it caused errors.



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

def invalidate_ability_cache
  update_column(:ability_cache, {})
end

#label_stringObject



52
53
54
# File 'app/models/concerns/users/base.rb', line 52

def label_string
  name
end

#multiple_teams?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/concerns/users/base.rb', line 103

def multiple_teams?
  teams.count > 1
end

#nameObject



56
57
58
# File 'app/models/concerns/users/base.rb', line 56

def name
  full_name.present? ? full_name : email
end

#one_team?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/models/concerns/users/base.rb', line 107

def one_team?
  !multiple_teams?
end

#otp_qr_codeObject



142
143
144
145
146
# File 'app/models/concerns/users/base.rb', line 142

def otp_qr_code
  issuer = I18n.t("application.name")
  label = "#{issuer}:#{email}"
  RQRCode::QRCode.new(otp_provisioning_uri(label, issuer: issuer))
end

#parent_ids_for(role, through, parent) ⇒ Object

TODO github.com/bullet-train-co/bullet_train-base/pull/121 should have removed this, but it caused errors.



125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/concerns/users/base.rb', line 125

def parent_ids_for(role, through, parent)
  parent_id_column = "#{parent}_id"
  key = "#{role.key}_#{through}_#{parent_id_column}s"
  return ability_cache[key] if ability_cache && ability_cache[key]
  role = nil if role.default?
  value = send(through).with_role(role).distinct.pluck(parent_id_column)
  current_cache = ability_cache || {}
  current_cache[key] = value
  update_column :ability_cache, current_cache
  value
end

#profile_photo_removal?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'app/models/concerns/users/base.rb', line 161

def profile_photo_removal?
  profile_photo_removal.present?
end

#real_emails_onlyObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/concerns/users/base.rb', line 79

def real_emails_only
  if ENV["REALEMAIL_API_KEY"] && !Rails.env.test?
    uri = URI("https://realemail.expeditedaddons.com")

    # Change the input parameters here
    uri.query = URI.encode_www_form({
      api_key: ENV["REAL_EMAIL_KEY"],
      email: email,
      fix_typos: false
    })

    # Results are returned as a JSON object
    result = JSON.parse(Net::HTTP.get_response(uri).body)

    if result["syntax_error"]
      errors.add(:email, "is not a valid email address")
    elsif result["domain_error"] || (result.key?("mx_records_found") && !result["mx_records_found"])
      errors.add(:email, "can't actually receive emails")
    elsif result["is_disposable"]
      errors.add(:email, "is a disposable email address")
    end
  end
end

#remove_profile_photoObject



165
166
167
# File 'app/models/concerns/users/base.rb', line 165

def remove_profile_photo
  profile_photo.purge
end

#send_welcome_emailObject



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

def send_welcome_email
  UserMailer.welcome(self).deliver_later
end

#set_teams_time_zoneObject



155
156
157
158
159
# File 'app/models/concerns/users/base.rb', line 155

def set_teams_time_zone
  teams.where(time_zone: [nil, "UTC"]).each do |team|
    team.update(time_zone: time_zone) if team.users.count == 1
  end
end