trabajo-afectivo/app/models/karma/user.rb

45 lines
1.1 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2016-07-10 22:47:21 +00:00
class Karma::User < ApplicationModel
self.table_name = 'karma_users'
def self.sync(user)
score = Karma.score_by_user(user)
level = level_by_score(score)
record = Karma::User.find_by(user_id: user.id)
if record
return record if record.score == score && record.level == level
record.score = score
record.level = level
record.save
return record
end
Karma::User.create(
user_id: user.id,
level: level,
score: score,
)
end
def self.by_user(user)
record = Karma::User.find_by(user_id: user.id)
return record if record
sync(user)
end
def self.level_by_score(score)
level = nil
karma_levels = Setting.get('karma_levels')
karma_levels.each { |local_level|
if !level
level = local_level[:name]
end
next if local_level[:start] && score < local_level[:start]
next if local_level[:end] && score > local_level[:end]
level = local_level[:name]
}
level
end
end