add user_info

This commit is contained in:
chris authored and ihower committed 2011-04-13 18:22:50 +08:00
1 parent 73b75ec0b6
commit 2ccc2c9b39
21 files changed
+123 -206

No files matched your search

-50
View File
@@ -1,50 +0,0 @@
class AttributeAttrModel
include Mongoid::Document
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
field :markup
field :locale, :type => Boolean
field :list_options, :type => Array
embedded_in :user_attribute_model, :inverse_of => :attribute_attr_models
validates_uniqueness_of :key
# Destroy the i18n_variable
def destroy_i18n_variable
self.i18n_variable.destroy rescue nil
end
# Get the i18n_variable
def i18n_variable
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end
# Update or create the i18n_variable record
def i18n_variable=(attr)
if self.i18n_variable_id
self.i18n_variable.update_attributes(attr)
else
var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class, :parent_id => self.user_attribute_model.i18n_variable_id}))
var.save
self.i18n_variable_id = var.id
end
end
# Convert the string list_options into an array
def select_list_options=(var)
self.list_options = var.gsub(' ', '').split(',')
end
# Convert the array list_options into a string
def select_list_options
self.list_options.to_a.join(', ')
end
# Check if the attribute_attr is set to be destroyed
def should_destroy?
should_destroy.to_i == 1 rescue nil
end
end
+23 -22
View File
@@ -9,46 +9,47 @@ class User
field :admin, :type => Boolean, :default => true
field :active_attributes, :type => Array
embeds_many :user_attributes
embeds_one :user_info
embeds_many :user_roles
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)
# Update or create the user_role records
def user_roles=(*attrs)
attrs[0].each do |roles|
if roles[:id].blank?
user_roles.build(roles)
else
user_attribute = user_attributes.detect {|a| a.id.to_s == attributes[:id].to_s }
user_attribute.update_attributes(attributes)
user_role = user_roles.detect {|a| a.id.to_s == roles[:id].to_s }
user_role.update_roles(roles)
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 }
# Get an user_role from model key
def get_role_from_model_key(key)
self.user_roles.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 []
# Get the active user_role_models
def get_active_role_models
self.active_roles.map{ |role| get_role_model(role) } rescue []
end
# Get an user_attribute_model from key
def get_attribute_model(key)
UserAttributeModel.first(:conditions => {:key => key})
# Get an user_role_model from key
def get_role_model(key)
UserRoleModel.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(' / ')
# Get the active roles names or default to '-'
def get_roles
(self.active_roles.nil? || self.active_roles.empty?) ? '-' : self.active_roles.map{|role| I18nVariable.first(:conditions => {:key => role})[I18n.locale] rescue role}.join(' / ')
end
protected
# Remove empty values
def clean_active_attributes
self.active_attributes.delete('') if self.active_attributes
def clean_active_roles
self.active_roles.delete('') if self.active_roles
end
end
-9
View File
@@ -1,9 +0,0 @@
class UserAttribute
include Mongoid::Document
field :key
embedded_in :user, :inverse_of => :user_attributes
end
+8 -33
View File
@@ -5,53 +5,28 @@ class UserAttributeModel
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
embeds_many :attribute_attr_models
embeds_many :attribute_models
after_update :destroy_attrs
# Update or create the attribute_attr_model records
def attribute_attr_models=(*attrs)
# Update or create the attribute_model records
def attribute_models=(*attrs)
attrs[0].each do |attributes|
if attributes[:id].blank?
attribute_attr_models.build(attributes)
attribute_models.build(attributes)
else
attribute_attr_model = attribute_attr_models.detect {|a| a.id.to_s == attributes[:id].to_s }
attribute_attr_model.update_attributes(attributes)
attribute_model = attribute_models.detect {|a| a.id.to_s == attributes[:id].to_s }
attribute_model.update_attributes(attributes)
end
end
end
# Destroy the i18n_variables
def destroy_i18n_variables
self.i18n_variable.destroy rescue nil
self.attribute_attr_models.each do |attr|
attr.destroy_i18n_variable
end
end
# Update or create the i18n_variable record
def i18n_variable=(attr)
if self.i18n_variable_id
self.i18n_variable.update_attributes(attr) rescue nil
else
var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class}))
var.save
self.i18n_variable_id = var.id
end
end
# Get the i18n_variable
def i18n_variable
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end
protected
# Destroy the attribute_attr_models if marked to destroy
# Destroy the i18n_variable for each attribute_models if marked to destroy
def destroy_attrs
attribute_attr_models.each do |a|
attribute_models.each do |a|
if a.should_destroy?
a.destroy
a.destroy_i18n_variable
end
end