Commit 233d63e98757da6ce483d4ea3992ef524e4e7be0

Authored by Victor Costa
1 parent 223c5340

Fix calculation of level percentage

Showing 2 changed files with 17 additions and 4 deletions   Show diff stats
lib/ext/profile.rb
@@ -8,7 +8,7 @@ class Profile @@ -8,7 +8,7 @@ class Profile
8 settings = GamificationPlugin.settings(environment) 8 settings = GamificationPlugin.settings(environment)
9 score = self.points 9 score = self.points
10 last_level = 0 10 last_level = 0
11 - (settings.get_setting(:rank_rules) || []).sort_by {|r| r[:points] }.each_with_index do |rule, i| 11 + (settings.get_setting(:rank_rules) || []).sort_by {|r| r[:points].to_i }.each_with_index do |rule, i|
12 return last_level if score < rule[:points].to_i 12 return last_level if score < rule[:points].to_i
13 last_level = rule[:level] || i+1 13 last_level = rule[:level] || i+1
14 end 14 end
@@ -18,10 +18,11 @@ class Profile @@ -18,10 +18,11 @@ class Profile
18 def gamification_plugin_level_percent 18 def gamification_plugin_level_percent
19 settings = GamificationPlugin.settings(environment) 19 settings = GamificationPlugin.settings(environment)
20 rules = settings.get_setting(:rank_rules) 20 rules = settings.get_setting(:rank_rules)
21 - return 100 if rules.blank? || rules.length < level 21 + return 100 if rules.blank? || rules.length < level+1
22 22
23 - next_level_points = rules[level][:points]  
24 - 100*points/next_level_points.to_f 23 + current_level_points = level>0 ? rules[level-1][:points].to_i : 0
  24 + next_level_points = rules[level][:points].to_f - current_level_points
  25 + 100*(points - current_level_points)/next_level_points
25 end 26 end
26 27
27 end 28 end
test/unit/profile_test.rb
@@ -54,4 +54,16 @@ class ProfileTest &lt; ActiveSupport::TestCase @@ -54,4 +54,16 @@ class ProfileTest &lt; ActiveSupport::TestCase
54 assert_equal 40, profile.gamification_plugin_level_percent 54 assert_equal 40, profile.gamification_plugin_level_percent
55 end 55 end
56 56
  57 + should 'return percentage of points earned in last level' do
  58 + profile.stubs(:level).returns(3)
  59 + profile.stubs(:points).returns(35)
  60 + assert_equal 100, profile.gamification_plugin_level_percent
  61 + end
  62 +
  63 + should 'return percentage of points earned in an intermediate level' do
  64 + profile.stubs(:level).returns(2)
  65 + profile.stubs(:points).returns(25)
  66 + assert_equal 50, profile.gamification_plugin_level_percent
  67 + end
  68 +
57 end 69 end