diff --git a/lib/ext/profile.rb b/lib/ext/profile.rb index 0e783d7..a582068 100644 --- a/lib/ext/profile.rb +++ b/lib/ext/profile.rb @@ -8,7 +8,7 @@ class Profile settings = GamificationPlugin.settings(environment) score = self.points last_level = 0 - (settings.get_setting(:rank_rules) || []).sort_by {|r| r[:points] }.each_with_index do |rule, i| + (settings.get_setting(:rank_rules) || []).sort_by {|r| r[:points].to_i }.each_with_index do |rule, i| return last_level if score < rule[:points].to_i last_level = rule[:level] || i+1 end @@ -18,10 +18,11 @@ class Profile def gamification_plugin_level_percent settings = GamificationPlugin.settings(environment) rules = settings.get_setting(:rank_rules) - return 100 if rules.blank? || rules.length < level + return 100 if rules.blank? || rules.length < level+1 - next_level_points = rules[level][:points] - 100*points/next_level_points.to_f + current_level_points = level>0 ? rules[level-1][:points].to_i : 0 + next_level_points = rules[level][:points].to_f - current_level_points + 100*(points - current_level_points)/next_level_points end end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 2b925d8..c9ec528 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -54,4 +54,16 @@ class ProfileTest < ActiveSupport::TestCase assert_equal 40, profile.gamification_plugin_level_percent end + should 'return percentage of points earned in last level' do + profile.stubs(:level).returns(3) + profile.stubs(:points).returns(35) + assert_equal 100, profile.gamification_plugin_level_percent + end + + should 'return percentage of points earned in an intermediate level' do + profile.stubs(:level).returns(2) + profile.stubs(:points).returns(25) + assert_equal 50, profile.gamification_plugin_level_percent + end + end -- libgit2 0.21.2