61 lines
1.6 KiB
Ruby
61 lines
1.6 KiB
Ruby
class User
|
|
|
|
include Mongoid::Document
|
|
|
|
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
|
|
|
|
mount_uploader :avatar, AvatarUploader
|
|
|
|
field :admin, :type => Boolean, :default => true
|
|
field :active_roles, :type => Array
|
|
|
|
embeds_many :user_roles
|
|
embeds_many :user_infos
|
|
before_update :clean_active_roles
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|
|
# 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_role_model from key
|
|
def get_role_model(key)
|
|
UserRoleModel.first(:conditions => {:key => key})
|
|
end
|
|
|
|
# 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_roles
|
|
self.active_roles.delete('') if self.active_roles
|
|
end
|
|
|
|
end
|