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.
123 124 125 |
# File 'app/models/concerns/users/base.rb', line 123 def parent_ids_for(Role.admin, :memberships, :team) end |
#create_default_team ⇒ Object
75 76 77 78 79 80 |
# File 'app/models/concerns/users/base.rb', line 75 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
67 68 69 |
# File 'app/models/concerns/users/base.rb', line 67 def details_provided? first_name.present? && last_name.present? && current_team.name.present? end |
#developer? ⇒ Boolean
151 152 153 154 155 156 |
# File 'app/models/concerns/users/base.rb', line 151 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
51 52 53 |
# File 'app/models/concerns/users/base.rb', line 51 def email_is_oauth_placeholder? !!email.match(/noreply@\h{32}\.example\.com/) end |
#formatted_email_address ⇒ Object
114 115 116 117 118 119 120 |
# File 'app/models/concerns/users/base.rb', line 114 def formatted_email_address if details_provided? "\"#{first_name} #{last_name}\" <#{email}>" else email end end |
#full_name ⇒ Object
63 64 65 |
# File 'app/models/concerns/users/base.rb', line 63 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.
141 142 143 |
# File 'app/models/concerns/users/base.rb', line 141 def invalidate_ability_cache update_column(:ability_cache, {}) end |
#label_string ⇒ Object
55 56 57 |
# File 'app/models/concerns/users/base.rb', line 55 def label_string name end |
#multiple_teams? ⇒ Boolean
106 107 108 |
# File 'app/models/concerns/users/base.rb', line 106 def multiple_teams? teams.count > 1 end |
#name ⇒ Object
59 60 61 |
# File 'app/models/concerns/users/base.rb', line 59 def name full_name.present? ? full_name : email end |
#one_team? ⇒ Boolean
110 111 112 |
# File 'app/models/concerns/users/base.rb', line 110 def one_team? !multiple_teams? end |
#otp_qr_code ⇒ Object
145 146 147 148 149 |
# File 'app/models/concerns/users/base.rb', line 145 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.
128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/models/concerns/users/base.rb', line 128 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
170 171 172 |
# File 'app/models/concerns/users/base.rb', line 170 def profile_photo_removal? profile_photo_removal.present? end |
#real_emails_only ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/concerns/users/base.rb', line 82 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
174 175 176 |
# File 'app/models/concerns/users/base.rb', line 174 def remove_profile_photo profile_photo.purge end |
#send_welcome_email ⇒ Object
71 72 73 |
# File 'app/models/concerns/users/base.rb', line 71 def send_welcome_email UserMailer.welcome(self).deliver_later end |
#set_memberships_email ⇒ Object
164 165 166 167 168 |
# File 'app/models/concerns/users/base.rb', line 164 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
158 159 160 161 162 |
# File 'app/models/concerns/users/base.rb', line 158 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 |