Class: ReviewableScore

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/reviewable_score.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_new_types(type_names) ⇒ Object



24
25
26
27
28
# File 'app/models/reviewable_score.rb', line 24

def self.add_new_types(type_names)
  next_id = types.values.max + 1

  type_names.each_with_index { |name, idx| @types[name] = next_id + idx }
end

.calc_user_accuracy_bonus(agreed, disagreed) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/reviewable_score.rb', line 64

def self.calc_user_accuracy_bonus(agreed, disagreed)
  agreed ||= 0
  disagreed ||= 0

  total = (agreed + disagreed).to_f
  return 0.0 if total <= 5
  accuracy_axis = 0.7

  percent_correct = agreed / total
  positive_accuracy = percent_correct >= accuracy_axis

  bottom = positive_accuracy ? accuracy_axis : 0.0
  top = positive_accuracy ? 1.0 : accuracy_axis

  absolute_distance = positive_accuracy ? percent_correct - bottom : top - percent_correct

  axis_distance_multiplier = 1.0 / (top - bottom)
  positivity_multiplier = positive_accuracy ? 1.0 : -1.0

  (
    absolute_distance * axis_distance_multiplier * positivity_multiplier *
      (Math.log(total, 4) * 5.0)
  ).round(2)
end

.calculate_score(user, type_bonus, take_action_bonus) ⇒ Object



42
43
44
45
# File 'app/models/reviewable_score.rb', line 42

def self.calculate_score(user, type_bonus, take_action_bonus)
  score = user_flag_score(user) + type_bonus + take_action_bonus
  score > 0 ? score : 0
end

.reload_typesObject

When extending post action flags, we need to call this method in order to get the latests flags.



19
20
21
22
# File 'app/models/reviewable_score.rb', line 19

def self.reload_types
  @types = nil
  types
end

.score_transitionsObject



30
31
32
# File 'app/models/reviewable_score.rb', line 30

def self.score_transitions
  { approved: statuses[:agreed], rejected: statuses[:disagreed], ignored: statuses[:ignored] }
end

.typesObject

To keep things simple the types correspond to ‘PostActionType` for backwards compatibility, but we can add extra reasons for scores.



13
14
15
# File 'app/models/reviewable_score.rb', line 13

def self.types
  @types ||= PostActionType.flag_types.merge(needs_approval: 9)
end

.user_accuracy_bonus(user) ⇒ Object

A user’s accuracy bonus is:

if 5 or less flags => 0.0
if > 5 flags => (agreed flags / total flags) * 5.0


57
58
59
60
61
62
# File 'app/models/reviewable_score.rb', line 57

def self.user_accuracy_bonus(user)
  user_stat = user&.user_stat
  return 0.0 if user_stat.blank? || user.bot?

  calc_user_accuracy_bonus(user_stat.flags_agreed, user_stat.flags_disagreed)
end

.user_flag_score(user) ⇒ Object

A user’s flag score is:

1.0 + trust_level + user_accuracy_bonus
(trust_level is 5 for staff)


50
51
52
# File 'app/models/reviewable_score.rb', line 50

def self.user_flag_score(user)
  1.0 + (user.staff? ? 5.0 : user.trust_level.to_f) + user_accuracy_bonus(user)
end

Instance Method Details

#reviewable_conversationObject



89
90
91
92
# File 'app/models/reviewable_score.rb', line 89

def reviewable_conversation
  return if meta_topic.blank?
  Reviewable::Conversation.new(meta_topic)
end

#score_typeObject



34
35
36
# File 'app/models/reviewable_score.rb', line 34

def score_type
  Reviewable::Collection::Item.new(reviewable_score_type)
end

#took_action?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/reviewable_score.rb', line 38

def took_action?
  take_action_bonus > 0
end