Commit 0ee65a55b16910bea00ad348f2d535b04e7dec9b

Authored by Victor Costa
2 parents faf2e1ac 048f746d

Merge branch 'working' into 'master'

Avoid recursive loop saving profile



See merge request !12
lib/merit/point_rules.rb
@@ -61,7 +61,6 @@ module Merit @@ -61,7 +61,6 @@ module Merit
61 action: 'vote#create', 61 action: 'vote#create',
62 undo_action: 'vote#destroy', 62 undo_action: 'vote#destroy',
63 to: lambda {|vote| vote.voteable.author}, 63 to: lambda {|vote| vote.voteable.author},
64 - profile: lambda {|vote| vote.voteable.profile},  
65 value: lambda {|vote| vote.vote}, 64 value: lambda {|vote| vote.vote},
66 description: _('Author of a voted content'), 65 description: _('Author of a voted content'),
67 default_weight: 20, 66 default_weight: 20,
@@ -71,7 +70,6 @@ module Merit @@ -71,7 +70,6 @@ module Merit
71 action: 'vote#create', 70 action: 'vote#create',
72 undo_action: 'vote#destroy', 71 undo_action: 'vote#destroy',
73 to: lambda {|vote| vote.voteable}, 72 to: lambda {|vote| vote.voteable},
74 - profile: lambda {|vote| vote.voteable.profile},  
75 value: lambda {|vote| vote.vote}, 73 value: lambda {|vote| vote.vote},
76 description: _('Voted content'), 74 description: _('Voted content'),
77 default_weight: 30, 75 default_weight: 30,
@@ -101,7 +99,6 @@ module Merit @@ -101,7 +99,6 @@ module Merit
101 value: 1, 99 value: 1,
102 description: _('Profile Completion'), 100 description: _('Profile Completion'),
103 default_weight: 100, 101 default_weight: 100,
104 - model_name: "User",  
105 condition: lambda {|person, profile| person.person? and person.profile_completion_score_condition }, 102 condition: lambda {|person, profile| person.person? and person.profile_completion_score_condition },
106 }, 103 },
107 follower: { 104 follower: {
@@ -159,8 +156,10 @@ module Merit @@ -159,8 +156,10 @@ module Merit
159 [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal| 156 [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal|
160 options = {on: action, to: setting[:to], category: categorization.id.to_s} 157 options = {on: action, to: setting[:to], category: categorization.id.to_s}
161 options[:model_name] = setting[:model] unless setting[:model].nil? 158 options[:model_name] = setting[:model] unless setting[:model].nil?
162 - score lambda {|target| signal * calculate_score(target, categorization.weight, setting[:value])}, options do |target|  
163 - condition(setting, target, categorization.profile) 159 + weight = categorization.weight
  160 + profile = categorization.profile
  161 + score lambda {|target| signal * calculate_score(target, weight, setting[:value])}, options do |target|
  162 + condition(setting, target, profile)
164 end 163 end
165 end 164 end
166 end 165 end
lib/merit/rank_observer.rb
@@ -5,7 +5,7 @@ module Merit @@ -5,7 +5,7 @@ module Merit
5 merit = changed_data[:merit_object] 5 merit = changed_data[:merit_object]
6 if merit.kind_of?(Merit::Score::Point) 6 if merit.kind_of?(Merit::Score::Point)
7 profile = merit.score.sash.profile 7 profile = merit.score.sash.profile
8 - profile.update_attribute(:level, profile.gamification_plugin_calculate_level) if profile.present? 8 + profile.update_column(:level, profile.gamification_plugin_calculate_level) if profile.present?
9 end 9 end
10 end 10 end
11 end 11 end
test/unit/profile_test.rb
@@ -44,6 +44,18 @@ class ProfileTest < ActiveSupport::TestCase @@ -44,6 +44,18 @@ class ProfileTest < ActiveSupport::TestCase
44 assert_equal 1, profile.gamification_plugin_calculate_level 44 assert_equal 1, profile.gamification_plugin_calculate_level
45 end 45 end
46 46
  47 + should 'update profile level when a profile action makes a score with zero point' do
  48 + #avoid loop when the score changes by zero and
  49 + person = create_user('testuser').person
  50 + Person.any_instance.stubs(:is_profile_complete?).returns(true)
  51 + create_point_rule_definition('profile_completion', nil, {value: 0})
  52 + GamificationPlugin.gamification_set_rules(environment)
  53 + assert_equal 0, person.level
  54 + assert_nothing_raised do
  55 + person.save
  56 + end
  57 + end
  58 +
47 should 'update profile level when the score changes' do 59 should 'update profile level when the score changes' do
48 create_point_rule_definition('article_author') 60 create_point_rule_definition('article_author')
49 community = fast_create(Community) 61 community = fast_create(Community)
views/gamification_plugin_badges/show.html.erb
@@ -24,9 +24,10 @@ @@ -24,9 +24,10 @@
24 24
25 <p> 25 <p>
26 <b>Threshold:</b> 26 <b>Threshold:</b>
27 - <% @gamification_plugin_badge.custom_fields.is_a? Hash %>  
28 - <% @gamification_plugin_badge.custom_fields.keys.each do |key| %>  
29 - <div><%= _(key) + ': ' + @gamification_plugin_badge.custom_fields[key].fetch("threshold", "") %></div> 27 + <% if @gamification_plugin_badge.custom_fields.is_a? Hash %>
  28 + <% @gamification_plugin_badge.custom_fields.keys.each do |key| %>
  29 + <div><%= _(key) + ': ' + @gamification_plugin_badge.custom_fields[key].fetch("threshold", "") %></div>
  30 + <% end %>
30 <% end %> 31 <% end %>
31 </p> 32 </p>
32 33