-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathability.rb
More file actions
68 lines (54 loc) · 2.07 KB
/
ability.rb
File metadata and controls
68 lines (54 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Ability < ApplicationRecord
include CommunityRelated
include AbilitiesHelper
validates :internal_id, uniqueness: { scope: [:community_id], case_sensitive: false }
def manual?
post_score_threshold.nil? && edit_score_threshold.nil? && flag_score_threshold.nil?
end
# Gets the edit score percent for a given user
# @param user [User, nil] user to get the percent for
# @return [Integer] edit score percent
def edit_score_percent_for(user)
return 0 if edit_score_threshold.nil? || user.nil?
return 100 if edit_score_threshold.zero?
linear_score = linearize_progress(user.community_user.edit_score)
linear_threshold = linearize_progress(edit_score_threshold)
(linear_score / linear_threshold * 100).to_i
end
# Gets the flag score percent for a given user
# @param user [User, nil] user to get the percent for
# @return [Integer] flag score percent
def flag_score_percent_for(user)
return 0 if flag_score_threshold.nil? || user.nil?
return 100 if flag_score_threshold.zero?
linear_score = linearize_progress(user.community_user.flag_score)
linear_threshold = linearize_progress(flag_score_threshold)
(linear_score / linear_threshold * 100).to_i
end
# Gets the post score percent for a given user
# @param user [User, nil] user to get the percent for
# @return [Integer] post score percent
def post_score_percent_for(user)
return 0 if post_score_threshold.nil? || user.nil?
return 100 if post_score_threshold.zero?
linear_score = linearize_progress(user.community_user.post_score)
linear_threshold = linearize_progress(post_score_threshold)
(linear_score / linear_threshold * 100).to_i
end
def self.on_user(user)
Ability.where(id: UserAbility.where(community_user: user.community_user).select(:ability_id).distinct)
end
def self.trust_levels
{
0 => 'everyone',
1 => 'anyone with a user account',
2 => 'all but new users',
3 => 'veteran users',
4 => 'moderators only',
5 => 'staff only'
}
end
def self.[](key)
find_by internal_id: key
end
end