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 38 action: 'vote#create',
39 39 default_threshold: 5,
40 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 44 negative_votes_received: [
... ... @@ -54,7 +54,7 @@ module Merit
54 54 action: 'vote#create',
55 55 default_threshold: 5,
56 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 60 friendly: [
... ... @@ -109,7 +109,7 @@ module Merit
109 109 action: 'vote#create',
110 110 default_threshold: 5,
111 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 115 action: 'comment#create',
... ... @@ -161,6 +161,7 @@ module Merit
161 161 end
162 162 # pass source and to for different situations
163 163 action = (badge.custom_fields || {}).fetch(s[:action], {})
  164 + debugger if source.is_a? Vote
164 165 can_be_granted &= s[:value].call(source, to) >= action.fetch(:threshold, s[:default_threshold]).to_i
165 166 end
166 167 can_be_granted
... ...
lib/merit/point_rules.rb
... ... @@ -65,7 +65,7 @@ module Merit
65 65 value: lambda {|vote| vote.vote},
66 66 description: _('Author of a voted content'),
67 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 70 vote_voteable: {
71 71 action: 'vote#create',
... ... @@ -75,7 +75,7 @@ module Merit
75 75 value: lambda {|vote| vote.vote},
76 76 description: _('Voted content'),
77 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 80 vote_voter: {
81 81 action: 'vote#create',
... ... @@ -84,7 +84,7 @@ module Merit
84 84 value: lambda {|vote| 1},
85 85 description: _('Voter'),
86 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 89 friends: {
90 90 action: 'friendship#create',
... ...