forked from qed42/omniauth-authlogic-demo
/
user.rb
31 lines (28 loc) · 1.74 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class User < ActiveRecord::Base
attr_accessor :password_confirmation
has_many :authorizations, :dependent => :destroy
has_many :statuses, :dependent => :destroy
acts_as_authentic do |c|
c.ignore_blank_passwords = true #ignoring passwords
c.validate_password_field = false #ignoring validations for password fields
end
#here we add required validations for a new record and pre-existing record
validate do |user|
if user.new_record? #adds validation if it is a new record
user.errors.add(:password, "is required") if user.password.blank?
user.errors.add(:password_confirmation, "is required") if user.password_confirmation.blank?
user.errors.add(:password, "Password and confirmation must match") if user.password != user.password_confirmation
elsif !(!user.new_record? && user.password.blank? && user.password_confirmation.blank?) #adds validation only if password or password_confirmation are modified
user.errors.add(:password, "is required") if user.password.blank?
user.errors.add(:password_confirmation, "is required") if user.password_confirmation.blank?
user.errors.add(:password, " and confirmation must match.") if user.password != user.password_confirmation
user.errors.add(:password, " and confirmation should be atleast 4 characters long.") if user.password.length < 4 || user.password_confirmation.length < 4
end
end
def self.create_from_hash(hash)
user = User.new(:username => hash['user_info']['name'].scan(/[a-zA-Z0-9_]/).to_s.downcase)
user.save(false) #create the user without performing validations. This is because most of the fields are not set.
user.reset_persistence_token! #set persistence_token else sessions will not be created
user
end
end