Change role structure

Still needs to: change the views and create sub_roles in the dev.rake
This commit is contained in:
chris2tof committed 2011-12-10 18:12:39 +08:00
1 parent e0cdedfa2e
commit b7ac5478b4
45 files changed
+608 -492

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
class Attribute
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
embeds_many :attribute_fields
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :i18n_variable, :allow_destroy => true
accepts_nested_attributes_for :attribute_fields, :allow_destroy => true
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
def get_enabled_attribute_fields
self.attribute_fields.excludes('disabled' => true)
end
end
+37
View File
@@ -0,0 +1,37 @@
class AttributeField
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :markup
field :locale, :type => Boolean
field :list_options, :type => Array
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
embedded_in :attribute
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
has_many :attribute_values
validates_uniqueness_of :key
accepts_nested_attributes_for :i18n_variable, :allow_destroy => true
# 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
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
end
-62
View File
@@ -1,62 +0,0 @@
class AttributeModel
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
field :markup
field :locale, :type => Boolean
field :list_options, :type => Array
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
embedded_in :user_attribute_model
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 = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class}))
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 is set to be destroyed
def should_destroy?
should_destroy.to_i == 1 rescue nil
end
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
end
+2 -1
View File
@@ -5,6 +5,7 @@ class AttributeValue
field :key
belongs_to :user_attribute
belongs_to :attribute_field
belongs_to :user
end
+3
View File
@@ -0,0 +1,3 @@
class Info < Attribute
end
+29
View File
@@ -0,0 +1,29 @@
class Role
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
embeds_many :sub_roles
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
has_many :users
accepts_nested_attributes_for :i18n_variable, :allow_destroy => true
accepts_nested_attributes_for :sub_roles, :allow_destroy => true
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
# Get an role from key
def self.get_role_from_key(key)
self.first(:conditions => {:key => key})
end
end
+10
View File
@@ -0,0 +1,10 @@
class SubRole < Attribute
has_many :users
# Get an sub_role from key
def self.get_sub_role_from_key(key)
self.first(:conditions => {:key => key})
end
end
+4 -37
View File
@@ -10,43 +10,10 @@ class User
field :admin, :type => Boolean, :default => true
field :active_role
has_many :user_attributes
has_many :user_roles
has_many :user_infos
# 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_role = user_roles.detect {|a| a.id.to_s == roles[:id].to_s }
user_role.update_attributes(roles)
end
end
end
# Update or create the user_info records
def user_infos=(*attrs)
attrs[0].each do |infos|
if infos[:id].blank?
user_infos.build(infos)
else
user_info = user_infos.detect {|a| a.id.to_s == infos[:id].to_s }
user_info.update_attributes(infos)
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 }
end
has_many :attribute_values, :autosave => true, :dependent => :destroy
belongs_to :role
belongs_to :sub_role
accepts_nested_attributes_for :attribute_values, :allow_destroy => true
def name
infos = self.user_infos.detect {|info| info.key.to_s.eql?('profile') }
-21
View File
@@ -1,21 +0,0 @@
class UserAttribute
include Mongoid::Document
include Mongoid::Timestamps
field :key
has_many :attribute_values
# Update or create the attribute_value records
def attribute_values=(*args)
args[0].each do |value|
if value[:id].blank?
attribute_values.build(value)
else
attribute_value = attribute_values.detect {|a| a.id.to_s == value[:id].to_s }
attribute_value.update_attributes(value)
end
end
end
end
-74
View File
@@ -1,74 +0,0 @@
class UserAttributeModel
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
embeds_many :attribute_models
after_update :destroy_attrs
# Update or create the attribute_model records
def attribute_models=(*attrs)
attrs[0].each do |attributes|
if attributes[:id].blank?
attribute_models.build(attributes)
else
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_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
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled.blank? ? false : self.disabled
end
def get_enabled_attribute_models
self.attribute_models.excludes('disabled' => true)
end
protected
# Destroy the i18n_variable for each attribute_models if marked to destroy
def destroy_attrs
attribute_models.each do |a|
if a.should_destroy?
a.destroy_i18n_variable
end
end
end
end
-5
View File
@@ -1,5 +0,0 @@
class UserInfo < UserAttribute
belongs_to :user
end
-3
View File
@@ -1,3 +0,0 @@
class UserInfoModel < UserAttributeModel
end
-5
View File
@@ -1,5 +0,0 @@
class UserRole < UserAttribute
belongs_to :user
end
-8
View File
@@ -1,8 +0,0 @@
class UserRoleModel < UserAttributeModel
# Get an user_role_model from key
def self.get_role_model_from_key(key)
self.first(:conditions => {:key => key})
end
end