Commit 43ac6601dbe9af168a95ed351bee23d8137c978b
1 parent
97c7b22a
Exists in
master
and in
1 other branch
Adapt badge rules for composed actions
Showing
1 changed file
with
83 additions
and
45 deletions
Show diff stats
lib/merit/badge_rules.rb
... | ... | @@ -9,48 +9,76 @@ module Merit |
9 | 9 | include Merit::BadgeRulesMethods |
10 | 10 | |
11 | 11 | AVAILABLE_RULES = { |
12 | - :comment_author => { | |
13 | - :action => 'comment#create', | |
14 | - :default_threshold => 5, | |
15 | - :to => :author, | |
16 | - :value => lambda { |comment| comment.author.present? ? comment.author.comments.count : 0 } | |
17 | - }, | |
18 | - :comment_received => { | |
19 | - :action => 'comment#create', | |
20 | - :default_threshold => 5, | |
21 | - :to => lambda {|comment| comment.source.author}, | |
22 | - :value => lambda { |comment| comment.source.author.present? ? Comment.where(:source_id => Article.where(:author_id => comment.source.author.id)).count : 0 } | |
23 | - }, | |
24 | - :article_author => { | |
25 | - :action => 'article#create', | |
26 | - :default_threshold => 5, | |
27 | - :to => :author, | |
28 | - :value => lambda { |article| article.author.present? ? article.environment.articles.text_articles.where(:author_id => article.author.id).count : 0 } | |
29 | - }, | |
30 | - :positive_votes_received => { | |
31 | - :action => 'vote#create', | |
32 | - :default_threshold => 5, | |
33 | - :to => lambda {|vote| vote.voteable.author}, | |
34 | - :value => lambda { |vote| Vote.for_voteable(vote.voteable).where('vote > 0').count } | |
35 | - }, | |
36 | - :negative_votes_received => { | |
37 | - :action => 'vote#create', | |
38 | - :default_threshold => 5, | |
39 | - :to => lambda {|vote| vote.voteable.author}, | |
40 | - :value => lambda { |vote| Vote.for_voteable(vote.voteable).where('vote < 0').count } | |
41 | - }, | |
42 | - :votes_performed => { | |
43 | - :action => 'vote#create', | |
44 | - :default_threshold => 5, | |
45 | - :to => lambda {|vote| vote.voter}, | |
46 | - :value => lambda { |vote| Vote.for_voter(vote.voter).count } | |
47 | - }, | |
48 | - :friendly => { | |
49 | - :action => 'friendship#create', | |
50 | - :default_threshold => 5, | |
51 | - :to => lambda {|friendship| friendship.person}, | |
52 | - :value => lambda { |friendship| friendship.person.friends.count } | |
53 | - } | |
12 | + :comment_author => [ | |
13 | + { | |
14 | + :action => 'comment#create', | |
15 | + :default_threshold => 5, | |
16 | + :to => :author, | |
17 | + :value => lambda { |author| author.present? ? author.comments.count : 0 } | |
18 | + } | |
19 | + ], | |
20 | + :comment_received => [ | |
21 | + { | |
22 | + :action => 'comment#create', | |
23 | + :default_threshold => 5, | |
24 | + :to => lambda {|comment| comment.source.author}, | |
25 | + :value => lambda { |author| author.present? ? Comment.where(:source_id => Article.where(:author_id => author.id)).count : 0 } | |
26 | + } | |
27 | + ], | |
28 | + :article_author => [ | |
29 | + { | |
30 | + :action => 'article#create', | |
31 | + :default_threshold => 5, | |
32 | + :to => :author, | |
33 | + :value => lambda { |author| author.present? ? author.environment.articles.text_articles.where(:author_id => author.id).count : 0 } | |
34 | + }, | |
35 | + ], | |
36 | + :positive_votes_received => [ | |
37 | + { | |
38 | + :action => 'vote#create', | |
39 | + :default_threshold => 5, | |
40 | + :to => lambda {|vote| vote.voteable.author}, | |
41 | + :value => lambda { |vote| Vote.for_voteable(vote.voteable).where('vote > 0').count } | |
42 | + } | |
43 | + ], | |
44 | + :negative_votes_received => [ | |
45 | + { | |
46 | + :action => 'vote#create', | |
47 | + :default_threshold => 5, | |
48 | + :to => lambda {|vote| vote.voteable.author}, | |
49 | + :value => lambda { |vote| Vote.for_voteable(vote.voteable).where('vote < 0').count } | |
50 | + } | |
51 | + ], | |
52 | + :votes_performed => [ | |
53 | + { | |
54 | + :action => 'vote#create', | |
55 | + :default_threshold => 5, | |
56 | + :to => lambda {|vote| vote.voter}, | |
57 | + :value => lambda { |vote| Vote.for_voter(vote.voter).count } | |
58 | + } | |
59 | + ], | |
60 | + :friendly => [ | |
61 | + { | |
62 | + :action => 'friendship#create', | |
63 | + :default_threshold => 5, | |
64 | + :to => lambda {|friendship| friendship.person}, | |
65 | + :value => lambda { |person| person.friends.count } | |
66 | + } | |
67 | + ], | |
68 | + :creative => [ | |
69 | + { | |
70 | + :action => 'comment#create', | |
71 | + :default_threshold => 5, | |
72 | + :to => :author, | |
73 | + :value => lambda { |author| author.present? ? author.comments.count : 0 } | |
74 | + }, | |
75 | + { | |
76 | + :action => 'proposal#create', | |
77 | + :default_threshold => 5, | |
78 | + :to => :author, | |
79 | + :value => lambda { |author| author.present? ? author.proposals.count : 0 } | |
80 | + }, | |
81 | + ] | |
54 | 82 | } |
55 | 83 | |
56 | 84 | def initialize(environment=nil) |
... | ... | @@ -58,9 +86,19 @@ module Merit |
58 | 86 | @environment = environment |
59 | 87 | |
60 | 88 | environment.gamification_plugin_badges.all.each do |badge| |
61 | - setting = AVAILABLE_RULES[badge.name.to_sym] | |
62 | - grant_on setting[:action], :badge => badge.name, :level => badge.level, :to => setting[:to] do |source| | |
63 | - setting[:value].call(source) >= (badge.custom_fields || {}).fetch(:threshold, setting[:default_threshold]).to_i | |
89 | + settings = AVAILABLE_RULES[badge.name.to_sym] | |
90 | + settings.each_with_index do |setting,i| | |
91 | + grant_on setting[:action], :badge => badge.name, :level => badge.level, :to => setting[:to] do |source| | |
92 | + can_be_granted = true | |
93 | + settings.each_with_index do |s,j| | |
94 | + if s[:to].is_a? Symbol | |
95 | + receiver = source.send s[:to] | |
96 | + else | |
97 | + receiver = s[:to].call source | |
98 | + end | |
99 | + can_be_granted &= s[:value].call(receiver) >= (badge.custom_fields || {}).fetch(:threshold, s[:default_threshold]).to_i | |
100 | + end | |
101 | + end | |
64 | 102 | end |
65 | 103 | end |
66 | 104 | end | ... | ... |