Last active
August 29, 2015 14:26
-
-
Save ChunAllen/753a91ca3d0795f0adfe to your computer and use it in GitHub Desktop.
Roomarama Test Exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
MAGIC_NUMBER = 3 | |
VALIDCHARS = 'abcdefghjkmnpqrstuvwxyzABCDEFGJKMNPQRSTUVWXYZ23456789' | |
has_many :host_inquiries, class_name: 'Inquiry', foreign_key: 'host_id' | |
validates :password, presence: true | |
before_save :create_new_password | |
after_save :certify_if_needed, unless: -> { certified } | |
private | |
def certify_if_needed | |
inquiries = Inquiry.where("host_id = ? AND status IN (?, ?)", id, (Inquiry.statusLookup['STATUS_ROOMORAMA_TO_PAY'], | |
Inquiry.statusLookup['STATUS_PAYMENT_COMPLETE'])).limit(MAGIC_NUMBER) | |
update(certified: true) if !inquiries.blank? && inquiries.length >= MAGIC_NUMBER | |
return true | |
end | |
def create_new_password | |
newpass = [] | |
8.times do | |
r = rand(VALIDCHARS.length) | |
newpass << VALIDCHARS[r..r] | |
end | |
self.password = newpass.join() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment