From 1aacf5e2ce67b002ac99317e0ffb943a8398ae31 Mon Sep 17 00:00:00 2001 From: "Matthew K. Fu JuYuan" Date: Tue, 18 Sep 2012 16:22:56 +0800 Subject: [PATCH] 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. --- app/models/user/user.rb | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/app/models/user/user.rb b/app/models/user/user.rb index 1aa789bf8..5ec88a11c 100644 --- a/app/models/user/user.rb +++ b/app/models/user/user.rb @@ -34,8 +34,40 @@ class User accepts_nested_attributes_for :attribute_values, :allow_destroy => true before_create :initialize_desktop + before_save :check_status_record 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 dept_hash = {} @@ -91,4 +123,21 @@ class User self.build_desktop 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