处理undefined method `update_user_level' for nil:NilClass问题

This commit is contained in:
z9hang 2014-06-23 09:27:21 +08:00
parent 9d9bc76af4
commit f16bec924c
6 changed files with 67 additions and 61 deletions

View File

@ -302,7 +302,7 @@ class Changeset < ActiveRecord::Base
def be_user_score
UserScore.project(:push_code, self.user,self, { changeset_id: self.id })
#更新用户等级
self.user.update_user_level
UserLevels.update_user_level(self.user)
end
end

View File

@ -38,7 +38,7 @@ class PraiseTread < ActiveRecord::Base
target_user = obj.author
UserScore.skill(:praised_by_user, User.current,target_user,self,{ praise_tread_id: self.id })
#更新用户等级
target_user.update_user_level
UserLevels.update_user_level(target_user)
end
end
end

View File

@ -17,6 +17,6 @@ class ProjectInfo < ActiveRecord::Base
#更新用户等级
def update_user_level
self.user.update_user_level
UserLevels.update_user_level(self.user)
end
end

View File

@ -885,63 +885,6 @@ class User < Principal
end
end
#获取user的等级 -by zjc
def get_level
if self.level.nil?
update_user_level
end
self.level.level
end
#更新用户等级 - by zjc
def update_user_level
user_level = self.level.nil? ? UserLevels.new : self.level
user_level.user = self
#判断user的等级
pis = self.project_infos #ProjectInfo.find_all_by_user_id(self.id)
isManager = false;
pis.each do |pi|
#判断是否为项目管理员
if self.allowed_to?({:controller => "projects", :action => "edit"}, pi.project)
isManager = true;
end
end
has_effective_praise_count = false;
self.messages.each do |message|
if message.parent_id.nil?
ptcs = PraiseTreadCache.find_all_by_object_id(message.id)
ptcs.each do |ptc|
if ptc.object_type == 'Message' && ptc.praise_num.to_i > 5
has_effective_praise_count = true
end
end
end
end
if !has_effective_praise_count
self.memos do |memo|
if memo.parent_id.nil?
ptcs = PraiseTreadCache.find_all_by_object_id(memo.id)
ptcs.each do |ptc|
if ptc.object_type == 'Memo' && ptc.praise_num > 5
has_effective_praise_count = true
end
end
end
end
end
if isManager || self.changesets.count > 100
user_level.level = 3
elsif (self.changesets.count > 1 && self.changesets.count <= 100) || has_effective_praise_count
user_level.level = 2
else
user_level.level = 1
end
user_level.save
self.reload
end
protected
def validate_password_length

View File

@ -2,4 +2,67 @@
class UserLevels < ActiveRecord::Base
attr_accessible :user_id, :level
belongs_to :user
#获取user的等级 -by zjc
def get_level(user)
unless user.nil?
if user.level.nil?
UserLevels.update_user_level(user)
end
user.level.level
end
end
#更新用户的等级 -by zjc
def self.update_user_level(user)
unless user.nil?
user_level = user.level.nil? ? UserLevels.new : user.level
user_level.user = user
#判断user的等级
pis = user.project_infos #ProjectInfo.find_all_by_user_id(self.id)
isManager = false;
pis.each do |pi|
#判断是否为项目管理员
if user.allowed_to?({:controller => "projects", :action => "edit"}, pi.project)
isManager = true;
end
end
has_effective_praise_count = false;
user.messages.each do |message|
if message.parent_id.nil?
ptcs = PraiseTreadCache.find_all_by_object_id(message.id)
ptcs.each do |ptc|
if ptc.object_type == 'Message' && ptc.praise_num.to_i > 5
has_effective_praise_count = true
end
end
end
end
if !has_effective_praise_count
user.memos do |memo|
if memo.parent_id.nil?
ptcs = PraiseTreadCache.find_all_by_object_id(memo.id)
ptcs.each do |ptc|
if ptc.object_type == 'Memo' && ptc.praise_num > 5
has_effective_praise_count = true
end
end
end
end
end
if isManager || user.changesets.count > 100
user_level.level = 3
elsif (user.changesets.count > 1 && user.changesets.count <= 100) || has_effective_praise_count
user_level.level = 2
else
user_level.level = 1
end
user_level.save
user.reload
end
end
end

View File

@ -75,7 +75,7 @@ namespace :user_score do
next
end
target_user = obj.author
level = pt.user.get_level
level = UserLevels.get_level(pt.user)#pt.user.get_level
if pt.praise_or_tread == 0
#踩帖
users_skill[pt.user.id] = users_skill[pt.user.id].to_i - 2 #踩别人帖-2分