From 86405d39f4d709638c95bd47973360674fab3c91 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Thu, 13 Aug 2015 18:04:14 -0300 Subject: [PATCH] Ignore levels with null value --- lib/ext/profile.rb | 2 +- test/unit/profile_test.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/ext/profile.rb b/lib/ext/profile.rb index a582068..37fcf18 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].to_i }.each_with_index do |rule, i| + (settings.get_setting(:rank_rules) || []).reject{|r| r[:points].blank?}.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 diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index dc80bda..df74ce7 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -21,6 +21,19 @@ class ProfileTest < ActiveSupport::TestCase assert_equal 2, profile.gamification_plugin_calculate_level end + should 'calculate profile level ignoring blank values' do + @settings.set_setting(:rank_rules, [ + {:points => 10}, + {:points => 20}, + {:points => 30}, + {:points => ""}, + {:points => nil}, + ]) + @settings.save! + profile.stubs(:points).returns(25) + assert_equal 2, profile.gamification_plugin_calculate_level + end + should 'calculate profile last level' do profile.stubs(:points).returns(35) assert_equal 3, profile.gamification_plugin_calculate_level -- libgit2 0.21.2