From 60cdf17df1f5157b5d789282cfea4a2d623a42c4 Mon Sep 17 00:00:00 2001 From: Hugo Melo Date: Fri, 16 Oct 2015 16:08:37 -0300 Subject: [PATCH] Avoid errors matching condition on badges and points --- lib/merit/badge_rules.rb | 7 ++++--- lib/merit/point_rules.rb | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/merit/badge_rules.rb b/lib/merit/badge_rules.rb index 42ff7b8..45a584e 100644 --- a/lib/merit/badge_rules.rb +++ b/lib/merit/badge_rules.rb @@ -38,7 +38,7 @@ module Merit action: 'vote#create', default_threshold: 5, to: lambda {|vote| vote.voteable.author}, - value: lambda { |vote, author| Vote.for_voteable(vote.voteable).where('vote > 0').count } + value: lambda { |vote, author| vote.voteable ? Vote.for_voteable(vote.voteable).where('vote > 0').count : 0} } ], negative_votes_received: [ @@ -54,7 +54,7 @@ module Merit action: 'vote#create', default_threshold: 5, to: lambda {|vote| vote.voter}, - value: lambda { |vote, voter| Vote.for_voter(voter).count } + value: lambda { |vote, voter| voter ? Vote.for_voter(voter).count : 0 } } ], friendly: [ @@ -109,7 +109,7 @@ module Merit action: 'vote#create', default_threshold: 5, to: lambda {|vote| vote.voter}, - value: lambda { |vote, voter| voter.votes.where('vote > 0').count } + value: lambda { |vote, voter| voter ? voter.votes.where('vote > 0').count : 0 } }, { action: 'comment#create', @@ -161,6 +161,7 @@ module Merit end # pass source and to for different situations action = (badge.custom_fields || {}).fetch(s[:action], {}) + debugger if source.is_a? Vote can_be_granted &= s[:value].call(source, to) >= action.fetch(:threshold, s[:default_threshold]).to_i end can_be_granted diff --git a/lib/merit/point_rules.rb b/lib/merit/point_rules.rb index 27a13d8..4ce0975 100644 --- a/lib/merit/point_rules.rb +++ b/lib/merit/point_rules.rb @@ -65,7 +65,7 @@ module Merit value: lambda {|vote| vote.vote}, description: _('Author of a voted content'), default_weight: 20, - condition: lambda {|vote, profile| profile.nil? or vote.voteable.profile == profile } + condition: lambda {|vote, profile| profile.nil? or (vote.voteable and vote.voteable.profile == profile) } }, vote_voteable: { action: 'vote#create', @@ -75,7 +75,7 @@ module Merit value: lambda {|vote| vote.vote}, description: _('Voted content'), default_weight: 30, - condition: lambda {|vote, profile| profile.nil? or vote.voteable.profile == profile } + condition: lambda {|vote, profile| profile.nil? or (vote.voteable and vote.voteable.profile == profile) } }, vote_voter: { action: 'vote#create', @@ -84,7 +84,7 @@ module Merit value: lambda {|vote| 1}, description: _('Voter'), default_weight: 10, - condition: lambda {|vote, profile| profile.nil? or vote.voteable.profile == profile } + condition: lambda {|vote, profile| profile.nil? or (vote.voteable and vote.voteable.profile == profile) } }, friends: { action: 'friendship#create', -- libgit2 0.21.2