badge_rules.rb
6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# +grant_on+ accepts:
# * Nothing (always grants)
# * A block which evaluates to boolean (recieves the object as parameter)
# * A block with a hash composed of methods to run on the target object with
# expected values (+votes: 5+ for instance).
#
module Merit
class BadgeRules
include Merit::BadgeRulesMethods
AVAILABLE_RULES = {
comment_author: [
{
action: 'comment#create',
default_threshold: 5,
to: :author,
target_profile: lambda {|comment| comment.profile },
value: lambda { |comment, author| author.present? ? author.comments.count : 0 }
}
],
comment_received: [
{
action: 'comment#create',
default_threshold: 5,
to: lambda {|comment| comment.source.author},
target_profile: lambda {|comment| comment.profile },
value: lambda { |comment, author| author.present? ? Comment.where(source_id: Article.where(author_id: author.id)).count : 0 }
}
],
article_author: [
{
action: 'article#create',
default_threshold: 5,
to: :author,
target_profile: lambda {|article| article.profile },
value: lambda { |article, author| author.present? ? TextArticle.where(author_id: author.id).count : 0 }
},
],
positive_votes_received: [
{
action: 'vote#create',
default_threshold: 5,
to: lambda {|vote| vote.voteable.author},
target_profile: lambda {|vote| vote.voteable.profile },
value: lambda { |vote, author| vote.voteable ? Vote.for_voteable(vote.voteable).where('vote > 0').count : 0}
}
],
negative_votes_received: [
{
action: 'vote#create',
default_threshold: 5,
to: lambda {|vote| vote.voteable.author},
target_profile: lambda {|vote| vote.voteable.profile },
value: lambda { |vote, author| Vote.for_voteable(vote.voteable).where('vote < 0').count }
}
],
votes_performed: [
{
action: 'vote#create',
default_threshold: 5,
to: lambda {|vote| vote.voter},
target_profile: lambda {|vote| vote.voteable.profile },
value: lambda { |vote, voter| voter ? Vote.for_voter(voter).count : 0 }
}
],
friendly: [
{
action: 'friendship#create',
default_threshold: 5,
to: lambda {|friendship| friendship.person},
value: lambda { |friendship, person| person.friends.count }
}
],
manual: [],
#FIXME review the name of the badges and see a way to make it generic
creative: [
{
action: 'comment#create',
default_threshold: 5,
to: :author,
target_profile: lambda {|comment| comment.profile },
value: lambda { |comment, author| author.present? ? author.comments.count : 0 }
},
{
action: 'article#create',
default_threshold: 5,
to: :author,
target_profile: lambda {|article| article.profile },
value: lambda { |article, author| author.present? ? author.articles.count : 0 }
},
],
observer: [
{
action: 'articlefollower#create',
default_threshold: 5,
to: lambda {|article| article.person },
target_profile: lambda {|article_follower| article_follower.article.profile },
model: 'ArticleFollower',
value: lambda { |article, person| person.present? ? person.article_followers.count : 0 }
}
],
mobilizer: [
{
action: 'Vote#create',
default_threshold: 5,
to: lambda { |vote| vote.voter },
target_profile: lambda {|vote| vote.voteable.profile },
value: lambda { |vote, voter| Vote.for_voter(voter).count }
},
{
action: 'Event#create',
default_threshold: 5,
to: lambda { |article| article.author },
target_profile: lambda {|article| article.profile },
value: lambda { |event, author| author.events.count }
},
],
generous: [
{
action: 'vote#create',
default_threshold: 5,
to: lambda {|vote| vote.voter},
target_profile: lambda {|vote| vote.voteable.profile },
value: lambda { |vote, voter| voter ? voter.votes.where('vote > 0').count : 0 }
},
{
action: 'comment#create',
default_threshold: 5,
to: :author,
target_profile: lambda {|comment| comment.profile },
value: lambda { |comment, author| author.present? ? author.comments.count : 0 }
}
],
articulator: [
{
action: 'articlefollower#create',
default_threshold: 5,
to: :person,
target_profile: lambda {|article_follower| article_follower.article.profile },
model: 'ArticleFollower',
value: lambda { |article_follower, person| person.present? ? person.article_followers.count : 0 }
},
{
action: 'comment#create',
default_threshold: 5,
to: :author,
target_profile: lambda {|comment| comment.profile },
value: lambda { |comment, author| author.present? ? author.comments.count : 0 }
},
]
}
def target_author(source, setting)
if setting[:to].is_a? Symbol
source.send(setting[:to])
else
setting[:to].call(source) rescue nil
end
end
def target_profile(source, setting)
setting[:target_profile].present? ? setting[:target_profile].call(source) : nil
end
def check_organization_badge(badge, source, setting)
!badge.owner.kind_of?(Organization) || badge.owner == target_profile(source, setting)
end
def initialize(environment=nil)
return if environment.nil?
@environment = environment
rules = AVAILABLE_RULES
rules.merge! CONFERENCE_RULES if defined? CONFERENCE_RULES
environment.gamification_plugin_badges.each do |badge|
next if rules[badge.name.to_sym].nil?
rules[badge.name.to_sym].each do |setting|
options = {badge: badge.name, level: badge.level, to: setting[:to]}
options[:model_name] = setting[:model] unless setting[:model].nil?
grant_on setting[:action], options do |source|
can_be_granted = true
rules[badge.name.to_sym].each do |s|
to = target_author(source, setting)
# pass source and to for different situations
action = (badge.custom_fields || {}).fetch(s[:action], {})
can_be_granted &= s[:value].call(source, to) >= action.fetch(:threshold, s[:default_threshold]).to_i
can_be_granted &= check_organization_badge(badge, source, setting)
end
can_be_granted
end
end
end
end
end
end