class User include Mongoid::Document devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable mount_uploader :avatar, AvatarUploader 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 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 # Get an user_attribute_model from key def get_attribute_model(key) UserAttributeModel.first(:conditions => {:key => key}) 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 end end