Skip to content

Instantly share code, notes, and snippets.

@liangzan
Created March 2, 2012 09: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 liangzan/1957146 to your computer and use it in GitHub Desktop.
Save liangzan/1957146 to your computer and use it in GitHub Desktop.
require 'securerandom'
class User < ActiveRecord::Base
has_many :inquiries
has_many :host_inquiries, :class_name => 'Inquiry', :foreign_key => 'host_id'
validates_presence_of :password
before_save :create_new_password
after_save :certify_if_needed
INQUIRY_LIMIT = 3
def certify_if_needed
if !self.certified
inquiries = Inquiry.find(:all,
:conditions=> ["host_id = ? AND status IN ( ? , ? )",
self.id,
Inquiry.statusLookup['STATUS_ROOMORAMA_TO_PAY'],
Inquiry.statusLookup['STATUS_PAYMENT_COMPLETE']],
:limit => INQUIRY_LIMIT)
if inquiries.present? && inquiries.length >= INQUIRY_LIMIT
self.toggle!(:certified)
end
self.certified
else
true
end
end
def create_new_password
self.password = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment