user status method. Now the sub_role shall be sat with it's status in order to record sub_role status into user object.

Sub_role can be removed normally and the method check_status_record  will make sure data is being saved as it should be.
Also unnecessary role will be removed if no sub role exists.
This commit is contained in:
Matthew K. Fu JuYuan 2012-09-18 16:22:56 +08:00
parent 43ecebf934
commit 5de433d219
1 changed files with 49 additions and 0 deletions

View File

@ -34,8 +34,40 @@ class User
accepts_nested_attributes_for :attribute_values, :allow_destroy => true accepts_nested_attributes_for :attribute_values, :allow_destroy => true
before_create :initialize_desktop before_create :initialize_desktop
before_save :check_status_record
scope :remote_account, where(:nccu_id.ne => nil) scope :remote_account, where(:nccu_id.ne => nil)
def set_sub_role(sub_role_id,status_id)
self.sub_roles << SubRole.find(sub_role_id)
self.status_record.store(sub_role_id,status_id)
end
def get_status(*params)
param = params[0]
case param.class.to_s
when 'String'
sr = ::SubRole.find sub_role_id
when 'Hash'
sr = ::SubRole.first({conditions:{key: param[:key]}})
end
if self.sub_roles.include?(sr)
return ::Status.find(status_record.fetch(sr.id.to_s))
else
nil
end
end
def self.find_by_status_and_sub_role_key(sub_role_key,status_key)
sr = ::SubRole.first({conditions: { key: sub_role_key }})
status = ::Status.first({conditions:{role_id: sr.role.id,key: status_key}})
find_by_status(sr.id,status.id)
end
def self.find_by_status(sub_role_id,status_id)
User.where("status_record.#{sub_role_id}" => status_id)
end
def create_dept_cache def create_dept_cache
dept_hash = {} dept_hash = {}
@ -91,4 +123,21 @@ class User
self.build_desktop self.build_desktop
end end
protected
def check_status_record
roles = sub_roles.collect{|t| t.role}.uniq #get all role from sub_roles
sub_roles_ary = sub_roles.collect{|t| t.id.to_s}
self.status_record = status_record.keep_if{|sub_role_id, status_id|
includeing = sub_roles_ary.include?(sub_role_id)
valide = false
if includeing
sub_role = SubRole.find sub_role_id
valide = sub_role.role.statuses.include? (Status.find status_id)
end
(includeing and valide)
}
end
end end