Commit 8268e552f4edf8c4efbf393dfabfc44737e6f8f4
1 parent
43ac6601
Exists in
master
and in
1 other branch
Add badge types and fix initialize
Showing
2 changed files
with
119 additions
and
51 deletions
Show diff stats
lib/merit/badge_rules.rb
| @@ -9,75 +9,126 @@ module Merit | @@ -9,75 +9,126 @@ module Merit | ||
| 9 | include Merit::BadgeRulesMethods | 9 | include Merit::BadgeRulesMethods |
| 10 | 10 | ||
| 11 | AVAILABLE_RULES = { | 11 | AVAILABLE_RULES = { |
| 12 | - :comment_author => [ | 12 | + comment_author: [ |
| 13 | { | 13 | { |
| 14 | - :action => 'comment#create', | ||
| 15 | - :default_threshold => 5, | ||
| 16 | - :to => :author, | ||
| 17 | - :value => lambda { |author| author.present? ? author.comments.count : 0 } | 14 | + action: 'comment#create', |
| 15 | + default_threshold: 5, | ||
| 16 | + to: :author, | ||
| 17 | + value: lambda { |comment, author| author.present? ? author.conference_comments.count : 0 } | ||
| 18 | } | 18 | } |
| 19 | ], | 19 | ], |
| 20 | - :comment_received => [ | 20 | + comment_received: [ |
| 21 | { | 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 } | 22 | + action: 'comment#create', |
| 23 | + default_threshold: 5, | ||
| 24 | + to: lambda {|comment| comment.source.author}, | ||
| 25 | + value: lambda { |comment, author| author.present? ? Comment.where(source_id: Article.conference_articles.where(author_id: author.id)).count : 0 } | ||
| 26 | } | 26 | } |
| 27 | ], | 27 | ], |
| 28 | - :article_author => [ | 28 | + article_author: [ |
| 29 | { | 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 } | 30 | + action: 'article#create', |
| 31 | + default_threshold: 5, | ||
| 32 | + to: :author, | ||
| 33 | + value: lambda { |article, author| author.present? ? TextArticle.conference_articles.comments.where(author_id: author.id).count : 0 } | ||
| 34 | }, | 34 | }, |
| 35 | ], | 35 | ], |
| 36 | - :positive_votes_received => [ | 36 | + positive_votes_received: [ |
| 37 | { | 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 } | 38 | + action: 'vote#create', |
| 39 | + default_threshold: 5, | ||
| 40 | + to: lambda {|vote| vote.voteable.author}, | ||
| 41 | + value: lambda { |vote, author| Vote.for_voteable(vote.voteable).where('vote > 0').count } | ||
| 42 | } | 42 | } |
| 43 | ], | 43 | ], |
| 44 | - :negative_votes_received => [ | 44 | + negative_votes_received: [ |
| 45 | { | 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 } | 46 | + action: 'vote#create', |
| 47 | + default_threshold: 5, | ||
| 48 | + to: lambda {|vote| vote.voteable.author}, | ||
| 49 | + value: lambda { |vote, author| Vote.for_voteable(vote.voteable).where('vote < 0').count } | ||
| 50 | } | 50 | } |
| 51 | ], | 51 | ], |
| 52 | - :votes_performed => [ | 52 | + votes_performed: [ |
| 53 | { | 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 } | 54 | + action: 'vote#create', |
| 55 | + default_threshold: 5, | ||
| 56 | + to: lambda {|vote| vote.voter}, | ||
| 57 | + value: lambda { |vote, voter| Vote.for_voter(voter).count } | ||
| 58 | } | 58 | } |
| 59 | ], | 59 | ], |
| 60 | - :friendly => [ | 60 | + friendly: [ |
| 61 | { | 61 | { |
| 62 | - :action => 'friendship#create', | ||
| 63 | - :default_threshold => 5, | ||
| 64 | - :to => lambda {|friendship| friendship.person}, | ||
| 65 | - :value => lambda { |person| person.friends.count } | 62 | + action: 'friendship#create', |
| 63 | + default_threshold: 5, | ||
| 64 | + to: lambda {|friendship| friendship.person}, | ||
| 65 | + value: lambda { |friendship, person| person.friends.count } | ||
| 66 | } | 66 | } |
| 67 | ], | 67 | ], |
| 68 | - :creative => [ | 68 | + creative: [ |
| 69 | { | 69 | { |
| 70 | - :action => 'comment#create', | ||
| 71 | - :default_threshold => 5, | ||
| 72 | - :to => :author, | ||
| 73 | - :value => lambda { |author| author.present? ? author.comments.count : 0 } | 70 | + action: 'comment#create', |
| 71 | + default_threshold: 5, | ||
| 72 | + to: :author, | ||
| 73 | + value: lambda { |comment, author| author.present? ? author.comments.count : 0 } | ||
| 74 | }, | 74 | }, |
| 75 | { | 75 | { |
| 76 | - :action => 'proposal#create', | ||
| 77 | - :default_threshold => 5, | ||
| 78 | - :to => :author, | ||
| 79 | - :value => lambda { |author| author.present? ? author.proposals.count : 0 } | 76 | + action: 'article#create', |
| 77 | + default_threshold: 5, | ||
| 78 | + to: :author, | ||
| 79 | + value: lambda { |article, author| author.present? ? author.articles.count : 0 } | ||
| 80 | }, | 80 | }, |
| 81 | + ], | ||
| 82 | + observer: [ | ||
| 83 | + { | ||
| 84 | + action: 'article_follower#create', | ||
| 85 | + default_threshold: 5, | ||
| 86 | + to: lambda {|article| article.person }, | ||
| 87 | + value: lambda { |article, person| person.present? ? person.article_followers.count : 0 } | ||
| 88 | + } | ||
| 89 | + ], | ||
| 90 | + mobilizer: [ | ||
| 91 | + { | ||
| 92 | + action: 'Vote#create', | ||
| 93 | + default_threshold: 5, | ||
| 94 | + to: lambda { |vote| vote.voter }, | ||
| 95 | + value: lambda { |vote, voter| Vote.for_voter(voter).count } | ||
| 96 | + }, | ||
| 97 | + { | ||
| 98 | + action: 'Event#create', | ||
| 99 | + default_threshold: 5, | ||
| 100 | + to: lambda { |article| article.author }, | ||
| 101 | + value: lambda { |event, author| author.events.count } | ||
| 102 | + }, | ||
| 103 | + ], | ||
| 104 | + generous: [ | ||
| 105 | + { | ||
| 106 | + action: 'vote#create', | ||
| 107 | + default_threshold: 5, | ||
| 108 | + to: lambda {|vote| vote.voter}, | ||
| 109 | + value: lambda { |vote, voter| voter.votes.where('vote > 0').count } | ||
| 110 | + }, | ||
| 111 | + { | ||
| 112 | + action: 'comment#create', | ||
| 113 | + default_threshold: 5, | ||
| 114 | + to: :author, | ||
| 115 | + value: lambda { |comment, author| author.present? ? author.comments.count : 0 } | ||
| 116 | + } | ||
| 117 | + ], | ||
| 118 | + articulator: [ | ||
| 119 | + { | ||
| 120 | + action: 'article_follower#create', | ||
| 121 | + default_threshold: 5, | ||
| 122 | + to: :person, | ||
| 123 | + value: lambda { |article_follower, person| person.present? ? person.article_followers.count : 0 } | ||
| 124 | + }, | ||
| 125 | + { | ||
| 126 | + action: 'comment#create', | ||
| 127 | + default_threshold: 5, | ||
| 128 | + to: :author, | ||
| 129 | + value: lambda { |comment, author| author.present? ? author.comments.count : 0 } | ||
| 130 | + }, | ||
| 131 | + #mobilizer#create | ||
| 81 | ] | 132 | ] |
| 82 | } | 133 | } |
| 83 | 134 | ||
| @@ -85,19 +136,28 @@ module Merit | @@ -85,19 +136,28 @@ module Merit | ||
| 85 | return if environment.nil? | 136 | return if environment.nil? |
| 86 | @environment = environment | 137 | @environment = environment |
| 87 | 138 | ||
| 139 | + rules = AVAILABLE_RULES | ||
| 140 | + rules.merge! CONFERENCE_RULES if defined? CONFERENCE_RULES | ||
| 141 | + | ||
| 88 | environment.gamification_plugin_badges.all.each do |badge| | 142 | environment.gamification_plugin_badges.all.each do |badge| |
| 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| | 143 | + next if rules[badge.name.to_sym].nil? |
| 144 | + rules[badge.name.to_sym].each do |setting| | ||
| 145 | + grant_on setting[:action], badge: badge.name, level: badge.level, to: setting[:to] do |source| | ||
| 92 | can_be_granted = true | 146 | can_be_granted = true |
| 93 | - settings.each_with_index do |s,j| | 147 | + rules[badge.name.to_sym].each do |s| |
| 94 | if s[:to].is_a? Symbol | 148 | if s[:to].is_a? Symbol |
| 95 | - receiver = source.send s[:to] | 149 | + to = source.send(setting[:to]) |
| 96 | else | 150 | else |
| 97 | - receiver = s[:to].call source | 151 | + begin |
| 152 | + to = setting[:to].call(source) | ||
| 153 | + rescue | ||
| 154 | + to = nil | ||
| 155 | + end | ||
| 98 | end | 156 | end |
| 99 | - can_be_granted &= s[:value].call(receiver) >= (badge.custom_fields || {}).fetch(:threshold, s[:default_threshold]).to_i | 157 | + # pass source and to for different situations |
| 158 | + can_be_granted &= s[:value].call(source, to) >= (badge.custom_fields || {}).fetch(:threshold, s[:default_threshold]).to_i | ||
| 100 | end | 159 | end |
| 160 | + can_be_granted | ||
| 101 | end | 161 | end |
| 102 | end | 162 | end |
| 103 | end | 163 | end |