This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/models/user/user.rb

47 lines
1.6 KiB
Ruby

class User
include Mongoid::Document
include Mongoid::Timestamps
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
mount_uploader :avatar, AvatarUploader
field :admin, :type => Boolean, :default => true
field :active_role
has_many :attribute_values, :autosave => true, :dependent => :destroy
has_many :app_auths,as: :privilege_apps,:inverse_of => :privilege_lists
has_many :blocked_apps, :inverse_of => :blocked_users, :class_name => "AppAuth"
has_many :privilege_apps, :inverse_of => :privilege_users, :class_name => "AppAuth"
belongs_to :role
has_and_belongs_to_many :sub_roles
accepts_nested_attributes_for :attribute_values, :allow_destroy => true
def avb_apps
query = AppAuth.any_of({all: true},{privilege_user_ids: self.id},{roles: self.role.id}).excludes(blocked_user_ids: self.id)
end
def name
info = Class::Info.first(:conditions => {:key => 'profile'})
if info
first_name = get_attribute_values.detect {|value| value.key.to_s.eql?('first_name') }[I18n.locale.to_s] rescue nil
last_name = get_attribute_values.detect {|value| value.key.to_s.eql?('last_name') }[I18n.locale.to_s] rescue nil
return "#{first_name} #{last_name}"
else
return nil
end
end
def get_attribute_values
@attribute_values ||= self.attribute_values
end
def get_value_from_field_id(field_id)
values = get_attribute_values
value = values.detect {|value| value.attribute_field_id == field_id} rescue nil
value ? value : self.attribute_values.build
end
end