Commit 7b2d77fa0f95f24179e5afa28ec548a1d3c76cec
1 parent
c608ace2
Exists in
master
and in
1 other branch
Change the way to add point rules
Showing
2 changed files
with
62 additions
and
25 deletions
Show diff stats
lib/merit/badge_rules.rb
@@ -39,24 +39,6 @@ module Merit | @@ -39,24 +39,6 @@ module Merit | ||
39 | voteable.votes.count == 2 | 39 | voteable.votes.count == 2 |
40 | end | 40 | end |
41 | 41 | ||
42 | - # If it has 10 comments, grant commenter-10 badge | ||
43 | - # grant_on 'comments#create', badge: 'commenter', level: 10 do |comment| | ||
44 | - # comment.user.comments.count == 10 | ||
45 | - # end | ||
46 | - | ||
47 | - # If it has 5 votes, grant relevant-commenter badge | ||
48 | - # grant_on 'comments#vote', badge: 'relevant-commenter', | ||
49 | - # to: :user do |comment| | ||
50 | - # | ||
51 | - # comment.votes.count == 5 | ||
52 | - # end | ||
53 | - | ||
54 | - # Changes his name by one wider than 4 chars (arbitrary ruby code case) | ||
55 | - # grant_on 'registrations#update', badge: 'autobiographer', | ||
56 | - # temporary: true, model_name: 'User' do |user| | ||
57 | - # | ||
58 | - # user.name.length > 4 | ||
59 | - # end | ||
60 | end | 42 | end |
61 | end | 43 | end |
62 | end | 44 | end |
lib/merit/point_rules.rb
@@ -12,16 +12,71 @@ module Merit | @@ -12,16 +12,71 @@ module Merit | ||
12 | class PointRules | 12 | class PointRules |
13 | include Merit::PointRulesMethods | 13 | include Merit::PointRulesMethods |
14 | 14 | ||
15 | + AVAILABLE_RULES = { | ||
16 | + :comment_author => { | ||
17 | + :action => 'comment#create', | ||
18 | + :undo_action => 'comment#destroy', | ||
19 | + :to => :author, | ||
20 | + :value => 1 | ||
21 | + }, | ||
22 | + :article_author => { | ||
23 | + :action => 'article#create', | ||
24 | + :undo_action => 'article#destroy', | ||
25 | + :to => :author, | ||
26 | + :value => 1 | ||
27 | + }, | ||
28 | + :vote_voteable_author => { | ||
29 | + :action => 'vote#create', | ||
30 | + :undo_action => 'vote#destroy', | ||
31 | + :to => lambda {|vote| vote.voteable.author}, | ||
32 | + :profile => lambda {|vote| vote.voteable.profile}, | ||
33 | + :value => lambda {|vote| vote.vote} | ||
34 | + }, | ||
35 | + :vote_voteable => { | ||
36 | + :action => 'vote#create', | ||
37 | + :undo_action => 'vote#destroy', | ||
38 | + :to => lambda {|vote| vote.voteable}, | ||
39 | + :profile => lambda {|vote| vote.voteable.profile}, | ||
40 | + :value => lambda {|vote| vote.vote} | ||
41 | + }, | ||
42 | + } | ||
43 | + | ||
44 | + # FIXME get value from environment | ||
45 | + def weight(action) | ||
46 | + case action | ||
47 | + when :comment_author | ||
48 | + 10 | ||
49 | + when :article_author | ||
50 | + 50 | ||
51 | + when :vote_voteable | ||
52 | + 5 | ||
53 | + when :vote_voteable_author | ||
54 | + 5 | ||
55 | + end | ||
56 | + end | ||
57 | + | ||
58 | + def calculate_score(target, action, value) | ||
59 | + value = value.call(target) if value.respond_to?(:call) | ||
60 | + weight(action) * value | ||
61 | + end | ||
62 | + | ||
15 | def initialize | 63 | def initialize |
16 | - score 10, :on => 'comment#create' | ||
17 | - score -10, :on => 'comment#destroy' | 64 | + AVAILABLE_RULES.each do |key, setting| |
65 | + score lambda {|target| calculate_score(target, key, setting[:value])}, :on => setting[:action], :to => setting[:to] | ||
66 | + if setting[:undo_action].present? | ||
67 | + score lambda {|target| -calculate_score(target, key, setting[:value])}, :on => setting[:undo_action], :to => setting[:to] | ||
68 | + end | ||
69 | + end | ||
70 | + | ||
71 | + #score lambda {|target| calculate_score(target, :comment_create, 1)}, :on => 'comment#create' | ||
72 | + #score lambda {|target| calculate_score(target, :comment_create, -1)}, :on => 'comment#destroy' | ||
18 | 73 | ||
19 | - score 50, :on => 'article#create' | ||
20 | - score -50, :on => 'article#destroy' | 74 | + #score lambda {|target| calculate_score(target, :article_create, 1)}, :on => 'article#create' |
75 | + #score lambda {|target| calculate_score(target, :article_create, -1)}, :on => 'article#destroy' | ||
21 | 76 | ||
22 | - score lambda {|vote| 5 * vote.vote}, :on => 'vote#create', :to => lambda {|vote| vote.voteable.author} | ||
23 | - score lambda {|vote| 5 * vote.vote}, :on => 'vote#create', :to => lambda {|vote| vote.voteable} | ||
24 | - score lambda {|vote| -5 * vote.vote}, :on => 'vote#destroy', :to => lambda {|vote| vote.voteable.author} | 77 | + #score lambda {|target| calculate_score(target, :vote_create, target.vote)}, :on => 'vote#create', :to => lambda {|vote| vote.voteable.author} |
78 | + #score lambda {|target| calculate_score(target, :vote_create, target.vote)}, :on => 'vote#create', :to => lambda {|vote| vote.voteable} | ||
79 | + #score lambda {|target| calculate_score(target, :vote_create, -target.vote)}, :on => 'vote#destroy', :to => lambda {|vote| vote.voteable.author} | ||
25 | end | 80 | end |
26 | end | 81 | end |
27 | end | 82 | end |