From 7b2d77fa0f95f24179e5afa28ec548a1d3c76cec Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Wed, 25 Mar 2015 10:16:41 -0300 Subject: [PATCH] Change the way to add point rules --- lib/merit/badge_rules.rb | 18 ------------------ lib/merit/point_rules.rb | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 25 deletions(-) diff --git a/lib/merit/badge_rules.rb b/lib/merit/badge_rules.rb index caf47bc..cffda98 100644 --- a/lib/merit/badge_rules.rb +++ b/lib/merit/badge_rules.rb @@ -39,24 +39,6 @@ module Merit voteable.votes.count == 2 end - # If it has 10 comments, grant commenter-10 badge - # grant_on 'comments#create', badge: 'commenter', level: 10 do |comment| - # comment.user.comments.count == 10 - # end - - # If it has 5 votes, grant relevant-commenter badge - # grant_on 'comments#vote', badge: 'relevant-commenter', - # to: :user do |comment| - # - # comment.votes.count == 5 - # end - - # Changes his name by one wider than 4 chars (arbitrary ruby code case) - # grant_on 'registrations#update', badge: 'autobiographer', - # temporary: true, model_name: 'User' do |user| - # - # user.name.length > 4 - # end end end end diff --git a/lib/merit/point_rules.rb b/lib/merit/point_rules.rb index f6503c0..4431ff3 100644 --- a/lib/merit/point_rules.rb +++ b/lib/merit/point_rules.rb @@ -12,16 +12,71 @@ module Merit class PointRules include Merit::PointRulesMethods + AVAILABLE_RULES = { + :comment_author => { + :action => 'comment#create', + :undo_action => 'comment#destroy', + :to => :author, + :value => 1 + }, + :article_author => { + :action => 'article#create', + :undo_action => 'article#destroy', + :to => :author, + :value => 1 + }, + :vote_voteable_author => { + :action => 'vote#create', + :undo_action => 'vote#destroy', + :to => lambda {|vote| vote.voteable.author}, + :profile => lambda {|vote| vote.voteable.profile}, + :value => lambda {|vote| vote.vote} + }, + :vote_voteable => { + :action => 'vote#create', + :undo_action => 'vote#destroy', + :to => lambda {|vote| vote.voteable}, + :profile => lambda {|vote| vote.voteable.profile}, + :value => lambda {|vote| vote.vote} + }, + } + + # FIXME get value from environment + def weight(action) + case action + when :comment_author + 10 + when :article_author + 50 + when :vote_voteable + 5 + when :vote_voteable_author + 5 + end + end + + def calculate_score(target, action, value) + value = value.call(target) if value.respond_to?(:call) + weight(action) * value + end + def initialize - score 10, :on => 'comment#create' - score -10, :on => 'comment#destroy' + AVAILABLE_RULES.each do |key, setting| + score lambda {|target| calculate_score(target, key, setting[:value])}, :on => setting[:action], :to => setting[:to] + if setting[:undo_action].present? + score lambda {|target| -calculate_score(target, key, setting[:value])}, :on => setting[:undo_action], :to => setting[:to] + end + end + + #score lambda {|target| calculate_score(target, :comment_create, 1)}, :on => 'comment#create' + #score lambda {|target| calculate_score(target, :comment_create, -1)}, :on => 'comment#destroy' - score 50, :on => 'article#create' - score -50, :on => 'article#destroy' + #score lambda {|target| calculate_score(target, :article_create, 1)}, :on => 'article#create' + #score lambda {|target| calculate_score(target, :article_create, -1)}, :on => 'article#destroy' - score lambda {|vote| 5 * vote.vote}, :on => 'vote#create', :to => lambda {|vote| vote.voteable.author} - score lambda {|vote| 5 * vote.vote}, :on => 'vote#create', :to => lambda {|vote| vote.voteable} - score lambda {|vote| -5 * vote.vote}, :on => 'vote#destroy', :to => lambda {|vote| vote.voteable.author} + #score lambda {|target| calculate_score(target, :vote_create, target.vote)}, :on => 'vote#create', :to => lambda {|vote| vote.voteable.author} + #score lambda {|target| calculate_score(target, :vote_create, target.vote)}, :on => 'vote#create', :to => lambda {|vote| vote.voteable} + #score lambda {|target| calculate_score(target, :vote_create, -target.vote)}, :on => 'vote#destroy', :to => lambda {|vote| vote.voteable.author} end end end -- libgit2 0.21.2