Module: Users::Base
Instance Method Summary collapse
-
#administrating_team_ids ⇒ Object
TODO github.com/bullet-train-co/bullet_train-base/pull/121 should have removed this, but it caused errors.
- #create_default_team ⇒ Object
- #details_provided? ⇒ Boolean
- #developer? ⇒ Boolean
- #email_is_oauth_placeholder? ⇒ Boolean
- #formatted_email_address ⇒ Object
- #full_name ⇒ Object
-
#invalidate_ability_cache ⇒ Object
TODO github.com/bullet-train-co/bullet_train-base/pull/121 should have removed this, but it caused errors.
- #label_string ⇒ Object
- #multiple_teams? ⇒ Boolean
- #name ⇒ Object
- #one_team? ⇒ Boolean
- #otp_qr_code ⇒ Object
-
#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.
- #profile_photo_removal? ⇒ Boolean
- #real_emails_only ⇒ Object
- #remove_profile_photo ⇒ Object
- #send_welcome_email ⇒ Object
- #set_memberships_email ⇒ Object
- #set_teams_time_zone ⇒ Object
Instance Method Details
#administrating_team_ids ⇒ Object
TODO github.com/bullet-train-co/bullet_train-base/pull/121 should have removed this, but it caused errors.
122 123 124 |
# File 'app/models/concerns/users/base.rb', line 122 def parent_ids_for(Role.admin, :memberships, :team) end |
#create_default_team ⇒ Object
74 75 76 77 78 79 |
# File 'app/models/concerns/users/base.rb', line 74 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
66 67 68 |
# File 'app/models/concerns/users/base.rb', line 66 def details_provided? first_name.present? && last_name.present? && current_team.name.present? end |
#developer? ⇒ Boolean
150 151 152 153 154 155 |
# File 'app/models/concerns/users/base.rb', line 150 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
50 51 52 |
# File 'app/models/concerns/users/base.rb', line 50 def email_is_oauth_placeholder? !!email.match(/noreply@\h{32}\.example\.com/) end |
#formatted_email_address ⇒ Object
113 114 115 116 117 118 119 |
# File 'app/models/concerns/users/base.rb', line 113 def formatted_email_address if details_provided? "\"#{first_name} #{last_name}\" <#{email}>" else email end end |
#full_name ⇒ Object
62 63 64 |
# File 'app/models/concerns/users/base.rb', line 62 def full_name [first_name_was, last_name_was].select(&:present?).join(" ") end |
#invalidate_ability_cache ⇒ Object
TODO github.com/bullet-train-co/bullet_train-base/pull/121 should have removed this, but it caused errors.
140 141 142 |
# File 'app/models/concerns/users/base.rb', line 140 def invalidate_ability_cache update_column(:ability_cache, {}) end |
#label_string ⇒ Object
54 55 56 |
# File 'app/models/concerns/users/base.rb', line 54 def label_string name end |
#multiple_teams? ⇒ Boolean
105 106 107 |
# File 'app/models/concerns/users/base.rb', line 105 def multiple_teams? teams.count > 1 end |
#name ⇒ Object
58 59 60 |
# File 'app/models/concerns/users/base.rb', line 58 def name full_name.present? ? full_name : email end |
#one_team? ⇒ Boolean
109 110 111 |
# File 'app/models/concerns/users/base.rb', line 109 def one_team? !multiple_teams? end |
#otp_qr_code ⇒ Object
144 145 146 147 148 |
# File 'app/models/concerns/users/base.rb', line 144 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.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/models/concerns/users/base.rb', line 127 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
169 170 171 |
# File 'app/models/concerns/users/base.rb', line 169 def profile_photo_removal? profile_photo_removal.present? end |
#real_emails_only ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/concerns/users/base.rb', line 81 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_photo ⇒ Object
173 174 175 |
# File 'app/models/concerns/users/base.rb', line 173 def remove_profile_photo profile_photo.purge end |
#send_welcome_email ⇒ Object
70 71 72 |
# File 'app/models/concerns/users/base.rb', line 70 def send_welcome_email UserMailer.welcome(self).deliver_later end |
#set_memberships_email ⇒ Object
163 164 165 166 167 |
# File 'app/models/concerns/users/base.rb', line 163 def set_memberships_email if email_previously_changed? memberships.where.not(user_email: email).update(user_email: email) end end |
#set_teams_time_zone ⇒ Object
157 158 159 160 161 |
# File 'app/models/concerns/users/base.rb', line 157 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 |