orbit-basic/app/models/user.rb

56 lines
1.4 KiB
Ruby
Raw Normal View History

class User
include Mongoid::Document
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
mount_uploader :avatar, AvatarUploader
2011-01-31 03:31:33 +00:00
field :admin, :type => Boolean, :default => true
field :active_attributes, :type => Array
2011-02-01 02:46:26 +00:00
embeds_one :user_info
embeds_many :user_roles
before_update :clean_active_attributes
2011-02-01 02:46:26 +00:00
# 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
2011-02-01 02:46:26 +00:00
user_role = user_roles.detect {|a| a.id.to_s == roles[:id].to_s }
user_role.update_roles(roles)
end
end
end
2011-02-01 02:46:26 +00:00
# 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
2011-02-01 02:46:26 +00:00
# Get the active user_role_models
def get_active_role_models
self.active_roles.map{ |role| get_role_model(role) } rescue []
end
2010-03-08 09:14:59 +00:00
2011-02-01 02:46:26 +00:00
# Get an user_role_model from key
def get_role_model(key)
UserRoleModel.first(:conditions => {:key => key})
2010-03-08 09:14:59 +00:00
end
2011-02-01 02:46:26 +00:00
# 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
2011-02-01 02:46:26 +00:00
def clean_active_roles
self.active_roles.delete('') if self.active_roles
2010-03-08 09:14:59 +00:00
end
end