class User
  
  include Mongoid::Document
  include Mongoid::Timestamps
 
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
  
  mount_uploader :avatar, AvatarUploader
  
  field :admin, :type => Boolean, :default => true
  field :active_role
  
  has_many :attribute_values, :autosave => true, :dependent => :destroy
  belongs_to :role
  belongs_to :sub_role
  accepts_nested_attributes_for :attribute_values, :allow_destroy => true
  
  def name
    infos = self.user_infos.detect {|info| info.key.to_s.eql?('profile') }
    if infos
      first_name = infos.attribute_values.detect {|value| value.key.to_s.eql?('first_name') }[I18n.locale.to_s] rescue nil
      last_name = infos.attribute_values.detect {|value| value.key.to_s.eql?('last_name') }[I18n.locale.to_s] rescue nil
      return "#{first_name} #{last_name}"
    else
      return nil
    end
  end
  
end
