Skip to content

Instantly share code, notes, and snippets.

@deepakprasanna
Created March 7, 2011 20:19
Show Gist options
  • Save deepakprasanna/859130 to your computer and use it in GitHub Desktop.
Save deepakprasanna/859130 to your computer and use it in GitHub Desktop.
class User
include Mongoid::Document
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
field :first_name
field :last_name
field :title
field :direct_phone
field :direct_extenson
field :brokerage_company_name
field :brokerage_company_phone
field :company_extenson
field :address
field :address1
field :city
field :state
field :zip
field :company_url
field :specialities
field :office, :type => Boolean, :default => false
field :industrial, :type => Boolean, :default => false
field :medical, :type => Boolean, :default => false
field :retail, :type => Boolean, :default => false
field :orange_county, :type => Boolean
field :los_angeles, :type => Boolean
field :riverside, :type => Boolean
field :agree
field :roles, :default => []
field :suspended, :type => Boolean, :default => false
field :added_by_admin, :type => Boolean, :default => false
field :requirement_count
validates_presence_of :first_name, :last_name, :brokerage_company_name
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :zip, :roles
attr_accessible :title, :direct_phone,:direct_extenson, :brokerage_company_name,:brokerage_company_phone,:company_extenson, :address, :address1, :city, :state, :company_url
attr_accessible :office, :industrial, :medical, :retail, :orange_county, :los_angeles, :riverside
validate :safe_domain
before_create :skip_confirmation_if_not_safe_domain
after_create :mark_invitation
after_create :destroy_unsafe_user
def destroy_unsafe_user
if not self.from_safe_domain?
self.destroy
end
end
def skip_confirmation_if_not_safe_domain
#In the user registrations from other domains also in the sign_up page
#in that case skip registrations
if not self.from_safe_domain?
self.skip_confirmation!
end
end
def mark_invitation
@invitation = Invitation.find(:first,:conditions => {:to => self.email})
@invitation.update_attributes(:status => "Registered") if not @invitation.nil?
end
def from_safe_domain?
domain = self.email.split(/@\s*/)
return SafeDomain.exists?(:conditions => {:domain => domain[1]})
end
def safe_domain
domain = self.email.split(/@\s*/)
if self.added_by_admin
if not SafeDomain.exists?(:conditions => {:domain => domain[1]})
errors.add_to_base("The user does not belong to a valid safe domain") if not domain.empty?
end
end
end
def is_admin?
return self.roles.include?("admin")
end
def update_user(params={})
if (params[:password].blank? && params[:password_confirmation].blank? && params[:current_password].blank?)
params.delete(:password)
params.delete(:password_confirmation)
update_attributes(params)
else
update_with_password(params)
end
end
def search(term)
interested_fields = ["first_name", "last_name", "email"]
users = []
interested_fields.each do |i|
users = users + User.find(:all, :conditions => { i => /^#{term}/i})
end
return users.uniq
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment