Commit 60cdf17df1f5157b5d789282cfea4a2d623a42c4

Authored by Hugo Melo
1 parent 55909011

Avoid errors matching condition on badges and points

lib/merit/badge_rules.rb
@@ -38,7 +38,7 @@ module Merit @@ -38,7 +38,7 @@ module Merit
38 action: 'vote#create', 38 action: 'vote#create',
39 default_threshold: 5, 39 default_threshold: 5,
40 to: lambda {|vote| vote.voteable.author}, 40 to: lambda {|vote| vote.voteable.author},
41 - value: lambda { |vote, author| Vote.for_voteable(vote.voteable).where('vote > 0').count } 41 + value: lambda { |vote, author| vote.voteable ? Vote.for_voteable(vote.voteable).where('vote > 0').count : 0}
42 } 42 }
43 ], 43 ],
44 negative_votes_received: [ 44 negative_votes_received: [
@@ -54,7 +54,7 @@ module Merit @@ -54,7 +54,7 @@ module Merit
54 action: 'vote#create', 54 action: 'vote#create',
55 default_threshold: 5, 55 default_threshold: 5,
56 to: lambda {|vote| vote.voter}, 56 to: lambda {|vote| vote.voter},
57 - value: lambda { |vote, voter| Vote.for_voter(voter).count } 57 + value: lambda { |vote, voter| voter ? Vote.for_voter(voter).count : 0 }
58 } 58 }
59 ], 59 ],
60 friendly: [ 60 friendly: [
@@ -109,7 +109,7 @@ module Merit @@ -109,7 +109,7 @@ module Merit
109 action: 'vote#create', 109 action: 'vote#create',
110 default_threshold: 5, 110 default_threshold: 5,
111 to: lambda {|vote| vote.voter}, 111 to: lambda {|vote| vote.voter},
112 - value: lambda { |vote, voter| voter.votes.where('vote > 0').count } 112 + value: lambda { |vote, voter| voter ? voter.votes.where('vote > 0').count : 0 }
113 }, 113 },
114 { 114 {
115 action: 'comment#create', 115 action: 'comment#create',
@@ -161,6 +161,7 @@ module Merit @@ -161,6 +161,7 @@ module Merit
161 end 161 end
162 # pass source and to for different situations 162 # pass source and to for different situations
163 action = (badge.custom_fields || {}).fetch(s[:action], {}) 163 action = (badge.custom_fields || {}).fetch(s[:action], {})
  164 + debugger if source.is_a? Vote
164 can_be_granted &= s[:value].call(source, to) >= action.fetch(:threshold, s[:default_threshold]).to_i 165 can_be_granted &= s[:value].call(source, to) >= action.fetch(:threshold, s[:default_threshold]).to_i
165 end 166 end
166 can_be_granted 167 can_be_granted
lib/merit/point_rules.rb
@@ -65,7 +65,7 @@ module Merit @@ -65,7 +65,7 @@ module Merit
65 value: lambda {|vote| vote.vote}, 65 value: lambda {|vote| vote.vote},
66 description: _('Author of a voted content'), 66 description: _('Author of a voted content'),
67 default_weight: 20, 67 default_weight: 20,
68 - condition: lambda {|vote, profile| profile.nil? or vote.voteable.profile == profile } 68 + condition: lambda {|vote, profile| profile.nil? or (vote.voteable and vote.voteable.profile == profile) }
69 }, 69 },
70 vote_voteable: { 70 vote_voteable: {
71 action: 'vote#create', 71 action: 'vote#create',
@@ -75,7 +75,7 @@ module Merit @@ -75,7 +75,7 @@ module Merit
75 value: lambda {|vote| vote.vote}, 75 value: lambda {|vote| vote.vote},
76 description: _('Voted content'), 76 description: _('Voted content'),
77 default_weight: 30, 77 default_weight: 30,
78 - condition: lambda {|vote, profile| profile.nil? or vote.voteable.profile == profile } 78 + condition: lambda {|vote, profile| profile.nil? or (vote.voteable and vote.voteable.profile == profile) }
79 }, 79 },
80 vote_voter: { 80 vote_voter: {
81 action: 'vote#create', 81 action: 'vote#create',
@@ -84,7 +84,7 @@ module Merit @@ -84,7 +84,7 @@ module Merit
84 value: lambda {|vote| 1}, 84 value: lambda {|vote| 1},
85 description: _('Voter'), 85 description: _('Voter'),
86 default_weight: 10, 86 default_weight: 10,
87 - condition: lambda {|vote, profile| profile.nil? or vote.voteable.profile == profile } 87 + condition: lambda {|vote, profile| profile.nil? or (vote.voteable and vote.voteable.profile == profile) }
88 }, 88 },
89 friends: { 89 friends: {
90 action: 'friendship#create', 90 action: 'friendship#create',