class User inherit Mongo::Model collection :users rad.extension :user_model, self attr_reader :name def name= value @name = value.try :downcase end validates_presence_of :name validates_length_of :name, in: 4..40 validates_uniqueness_of :name validates_format_of :name, with: /\A[a-z_][a-z_0-9]*\Z/ EMAIL_NAME_REGEX = '[\w\.%\+\-]+'.freeze DOMAIN_HEAD_REGEX = '(?:[A-Z0-9\-]+\.)+'.freeze DOMAIN_TLD_REGEX = '(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)'.freeze EMAIL_REGEX = /\A#{EMAIL_NAME_REGEX}@#{DOMAIN_HEAD_REGEX}#{DOMAIN_TLD_REGEX}\z/i attr_reader :email def email= value @email = value.try :downcase end validates_length_of :email, in: 6..100, allow_blank: true validates_uniqueness_of :email #, allow_blank: true validates_format_of :email, with: EMAIL_REGEX, allow_blank: true timestamps! # # Autentication # inherit Models::OpenIdAuthentication inherit Models::PasswordAuthentication def validate_authentication if crypted_password.blank? and open_ids.blank? errors.add :password, t(:should_not_be_blank) end end protected :validate_authentication validate :validate_authentication # # Lifecycle # attr_writer :state def state; @state ||= 'inactive' end def activate; self.state = 'active' end def active?; state == 'active' end def inactivate; self.state = 'inactive' end def inactive?; state == 'inactive' end validates_presence_of :state # # Authorization # inherit Models::Authorized # # Avatar # inherit Mongo::Model::FileModel mount_file :avatar, Models::AvatarFile # # Profile # attr_accessor :first_name, :last_name assign do first_name String, true last_name String, true end # # Helpers # class << self def [] name by_name name.to_s end inject current: :user def avatar_url user_name "#{rad.users.avatars_path}/avatars/#{user_name}.icon" end end # # Other # def to_param; name end def dom_id; "user: #{super}" end def slug; name end end