2010-03-04 08:33:26 +00:00
|
|
|
class User
|
2011-04-13 10:19:51 +00:00
|
|
|
|
|
|
|
include Mongoid::Document
|
2010-03-04 08:33:26 +00:00
|
|
|
|
2011-04-13 10:19:51 +00:00
|
|
|
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
|
|
|
|
|
|
|
|
mount_uploader :avatar, AvatarUploader
|
|
|
|
|
|
|
|
field :admin, :type => Boolean, :default => false
|
|
|
|
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|
|
2011-04-13 10:19:51 +00:00
|
|
|
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
|
2010-03-04 08:33:26 +00:00
|
|
|
|
2011-04-13 10:19:51 +00:00
|
|
|
# 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) }
|
|
|
|
end
|
2010-03-08 09:14:59 +00:00
|
|
|
|
2011-04-13 10:19:51 +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
|
|
|
|
|
2011-04-13 10:19:51 +00:00
|
|
|
# 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
|
|
|
|
|
2011-04-13 10:19:51 +00:00
|
|
|
end
|