orbit-basic/app/models/user.rb

55 lines
1.6 KiB
Ruby
Raw Normal View History

class User
include Mongoid::Document
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
mount_uploader :avatar, AvatarUploader
2011-01-31 03:31:33 +00:00
field :admin, :type => Boolean, :default => true
field :active_attributes, :type => Array
embeds_many :user_attributes
before_update :clean_active_attributes
# Update or create the user_attribute records
2011-01-28 06:44:08 +00:00
def user_attributes=(*attrs)
attrs[0].each do |attributes|
if attributes[:id].blank?
user_attributes.build(attributes)
else
user_attribute = user_attributes.detect {|a| a.id.to_s == attributes[:id].to_s }
user_attribute.update_attributes(attributes)
end
end
end
# Get an user_attribute from model key
def get_attribute_from_model_key(key)
self.user_attributes.detect {|a| a.key.to_s == key.to_s }
end
# Get the active user_attribute_models
def get_active_attribute_models
self.active_attributes.map{ |attr| get_attribute_model(attr) } rescue []
end
2010-03-08 09:14:59 +00:00
# Get an user_attribute_model from key
def get_attribute_model(key)
UserAttributeModel.first(:conditions => {:key => key})
2010-03-08 09:14:59 +00:00
end
# Get the active attributes names or default to '-'
def get_attributes
(self.active_attributes.nil? || self.active_attributes.empty?) ? '-' : self.active_attributes.map{|attr| I18nVariable.first(:conditions => {:key => attr})[I18n.locale] rescue attr}.join(' / ')
end
protected
# Remove empty values
def clean_active_attributes
self.active_attributes.delete('') if self.active_attributes
2010-03-08 09:14:59 +00:00
end
end