Class: UserEmail

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/user_email.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_and_confirm(confirmation_token) ⇒ Object


27
28
29
30
31
# File 'app/models/user_email.rb', line 27

def self.find_and_confirm(confirmation_token)
  email = find_by(confirmation_token: confirmation_token)
  return false unless email.present? && email.confirmation_sent_at > 6.hours.ago
  email.confirm!
end

Instance Method Details

#can_skip_confirmation?Boolean


100
101
102
103
# File 'app/models/user_email.rb', line 100

def can_skip_confirmation?
  pattern = EmailPattern.identify(email)
  pattern.present? && pattern[:skip_confirmation]
end

#confirmObject


84
85
86
87
88
89
90
91
92
# File 'app/models/user_email.rb', line 84

def confirm
  return false unless valid?
  ActiveRecord::Base.transaction do
    update(confirmed_at: Time.now, confirmation_token: nil)
    identify!
  end
  return false unless user.reload.valid?
  self
end

#confirm!Object


94
95
96
97
98
# File 'app/models/user_email.rb', line 94

def confirm!
  confirmed = confirm
  fail unless confirmed
  confirmed
end

#confirmed?Boolean


33
34
35
# File 'app/models/user_email.rb', line 33

def confirmed?
  confirmed_at.present?
end

#generate_confirmation_tokenObject


41
42
43
44
# File 'app/models/user_email.rb', line 41

def generate_confirmation_token
  self.confirmation_token = SecureRandom.urlsafe_base64(36)
  self.confirmation_sent_at = Time.now
end

#identify!Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/user_email.rb', line 61

def identify!
  ActiveRecord::Base.transaction do
    # find if there is a predefined identity with this email
    identity = UserIdentity.find_by(user_id: nil, email: email)
    if identity
      identity.update(user: user)
      identity.update(department_code: department_code) unless department_code.blank?    # or matching email patterns

    elsif (pattern_identity = EmailPattern.identify(email))
      identity = user.identities.create!(pattern_identity)
      identity.update(department_code: department_code) unless department_code.blank?
    end
  end
end

#linked_associated_user_identityObject


56
57
58
59
# File 'app/models/user_email.rb', line 56

def linked_associated_user_identity
  return nil if associated_user_identity.blank?
  associated_user_identity.user_id == user_id ? associated_user_identity : nil
end

#re_identify!Object


76
77
78
79
80
81
82
# File 'app/models/user_email.rb', line 76

def re_identify!
  return if linked_associated_user_identity.present? && !linked_associated_user_identity.generated?
  ActiveRecord::Base.transaction do
    linked_associated_user_identity.destroy if linked_associated_user_identity.present?
    identify!
  end
end

#resend_confirmation_instructionsObject


50
51
52
53
54
# File 'app/models/user_email.rb', line 50

def resend_confirmation_instructions
  generate_confirmation_token
  save!
  send_confirmation_instructions
end

#send_confirmation_instructionsObject


46
47
48
# File 'app/models/user_email.rb', line 46

def send_confirmation_instructions
  UserEmailMailer.confirm(self).deliver_now
end

#strip_emailObject


37
38
39
# File 'app/models/user_email.rb', line 37

def strip_email
  email.strip! if email.present?
end