From f76af2d2b6612849854174423e7fa667bf7c8b1f Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 14 Apr 2012 18:48:40 +0200 Subject: [PATCH] Improved caching if find_fulldata() and reduced data. --- app/models/user.rb | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 92d0f6522..969f125f5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -58,30 +58,53 @@ class User < ActiveRecord::Base # get user user = User.find(user_id) + data = user.attributes + # get linked accounts - user['accounts'] = {} + data['accounts'] = {} authorizations = user.authorizations() || [] authorizations.each do | authorization | - user['accounts'][authorization.provider] = { + data['accounts'][authorization.provider] = { :uid => authorization[:uid], :username => authorization[:username] } end # set roles - user['roles'] = user.roles.select('id, name').where(:active => true) - user['groups'] = user.groups.select('id, name').where(:active => true) - user['organization'] = user.organization - user['organizations'] = user.organizations.select('id, name').where(:active => true) + roles = [] + user.roles.select('id, name').where( :active => true ).each { |role| + roles.push role + } + data['roles'] = roles - @@cache[user_id] = user + groups = [] + user.groups.select('id, name').where( :active => true ).each { |group| + groups.push group + } + data['groups'] = groups + + organization = user.organization + data['organization'] = organization - return user + organizations = [] + user.organizations.select('id, name').where( :active => true ).each { |organization| + organizations.push organization + } + data['organizations'] = organizations + + @@cache[user_id] = data + + return data end - + + def cache_reset + @@cache[self.id] = nil + end + private def delete_cache + puts 'delete_cache', self.insepct @@cache[self.id] = nil end