add user_info

This commit is contained in:
chris authored and ihower committed 2011-04-13 18:22:50 +08:00
1 parent a06aa15721
commit 318c264cae
30 files changed
+347 -218

No files matched your search

+2 -2
View File
@@ -8,7 +8,7 @@ class AttributeModel
field :locale, :type => Boolean
field :list_options, :type => Array
embedded_in :user_role_model, :inverse_of => :attribute_models
embedded_in :user_attribute_model, :inverse_of => :attribute_models
validates_uniqueness_of :key
# Destroy the i18n_variable
@@ -26,7 +26,7 @@ class AttributeModel
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_role_model.i18n_variable_id}))
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
+9 -4
View File
@@ -7,11 +7,11 @@ class User
mount_uploader :avatar, AvatarUploader
field :admin, :type => Boolean, :default => true
field :active_attributes, :type => Array
field :active_roles, :type => Array
embeds_one :user_info
embeds_many :user_roles
before_update :clean_active_attributes
embeds_many :user_infos
before_update :clean_active_roles
# Update or create the user_role records
def user_roles=(*attrs)
@@ -20,11 +20,16 @@ class User
user_roles.build(roles)
else
user_role = user_roles.detect {|a| a.id.to_s == roles[:id].to_s }
user_role.update_roles(roles)
user_role.update_attributes(roles)
end
end
end
# Get an user_info from model key
def get_info_from_model_key(key)
self.user_infos.detect {|a| a.key.to_s == key.to_s }
end
# 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 }
+24
View File
@@ -21,6 +21,30 @@ class UserAttributeModel
end
end
# Destroy the i18n_variables
def destroy_i18n_variables
self.i18n_variable.destroy rescue nil
self.attribute_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 i18n_variable for each attribute_models if marked to destroy
+1 -1
View File
@@ -4,6 +4,6 @@ class UserInfo
field :key
embedded_in :user, :inverse_of => :user_info
embedded_in :user, :inverse_of => :user_infos
end
+1 -1
View File
@@ -1,3 +1,3 @@
class UserInfoModel < UserAttributeModel
end
-24
View File
@@ -1,27 +1,3 @@
class UserRoleModel < UserAttributeModel
# Destroy the i18n_variables
def destroy_i18n_variables
self.i18n_variable.destroy rescue nil
self.attribute_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
end