Class: Ditty::Identity

Inherits:
Sequel::Model
  • Object
show all
Includes:
Base, OmniAuth::Identity::Model
Defined in:
lib/ditty/models/identity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#display_id, #for_json

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



14
15
16
# File 'lib/ditty/models/identity.rb', line 14

def password
  @password
end

#password_confirmationObject

Returns the value of attribute password_confirmation.



14
15
16
# File 'lib/ditty/models/identity.rb', line 14

def password_confirmation
  @password_confirmation
end

Class Method Details

.locate(conditions) ⇒ Object



19
20
21
# File 'lib/ditty/models/identity.rb', line 19

def self.locate(conditions)
  where(conditions).first
end

Instance Method Details

#authenticate(unencrypted) ⇒ Object



23
24
25
26
27
# File 'lib/ditty/models/identity.rb', line 23

def authenticate(unencrypted)
  return false if crypted_password.blank?

  self if ::BCrypt::Password.new(crypted_password) == unencrypted
end

#before_saveObject

Callbacks



68
69
70
71
# File 'lib/ditty/models/identity.rb', line 68

def before_save
  super
  encrypt_password unless password == '' || password.nil?
end

#infoObject

Return whatever we want to pass to the omniauth hash here



34
35
36
37
38
# File 'lib/ditty/models/identity.rb', line 34

def info
  {
    email: username
  }
end

#persisted?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/ditty/models/identity.rb', line 29

def persisted?
  !new? && @destroyed != true
end

#validateObject

Validation



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ditty/models/identity.rb', line 41

def validate
  super
  validates_presence :username
  unless username.blank?
    validates_unique :username
    validates_format(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :username)
  end

  if password_required
    validates_presence :password
    validates_presence :password_confirmation
    validates_format(
      # 1 Uppercase
      # 1 lowercase
      # 1 Special Character
      # 1 Number
      # At least 8 characters
      %r[\A(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#&$*)(}{%^=_+|\\:";'<>,.\-\/?\[\]])(?=.*[0-9]).{8,}\Z],
      :password,
      message: 'is not strong enough'
    )
  end

  errors.add(:password_confirmation, 'must match password') if !password.blank? && password != password_confirmation
end