Skip to content

Instantly share code, notes, and snippets.

@ChunAllen
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChunAllen/753a91ca3d0795f0adfe to your computer and use it in GitHub Desktop.
Save ChunAllen/753a91ca3d0795f0adfe to your computer and use it in GitHub Desktop.
Roomarama Test Exercise
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