Class: Badge
Defined Under Namespace
Modules: Trigger
Constant Summary
collapse
- BasicUser =
NOTE: These badge ids are not in order! They are grouped logically.
When picking an id, *search* for it.
1
- Member =
2
- Regular =
3
- Leader =
4
- Welcome =
5
- NicePost =
6
- GoodPost =
7
- GreatPost =
8
- Autobiographer =
9
- Editor =
10
- WikiEditor =
48
- FirstLike =
11
- FirstShare =
12
- FirstFlag =
13
- FirstLink =
14
- FirstQuote =
15
- FirstMention =
40
- FirstEmoji =
41
- FirstOnebox =
42
- FirstReplyByEmail =
43
- ReadGuidelines =
16
- Reader =
17
- NiceTopic =
18
- GoodTopic =
19
- GreatTopic =
20
- NiceShare =
21
- GoodShare =
22
- GreatShare =
23
- Anniversary =
24
- Promoter =
25
- Campaigner =
26
- Champion =
27
- PopularLink =
28
- HotLink =
29
- FamousLink =
30
- Appreciated =
36
- Respected =
37
- Admired =
31
- OutOfLove =
33
- HigherLove =
34
- CrazyInLove =
35
- ThankYou =
38
- GivesBack =
32
- Empathetic =
39
- Enthusiast =
45
- Aficionado =
46
- Devotee =
47
- NewUserOfTheMonth =
44
- AutobiographerMinBioLength =
10
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#sanitize_field
Methods included from GlobalPath
#cdn_path, #cdn_relative_path, #full_cdn_url, #path, #upload_cdn_path
Instance Attribute Details
#has_badge ⇒ Object
73
74
75
|
# File 'app/models/badge.rb', line 73
def has_badge
@has_badge
end
|
Class Method Details
.display_name(name) ⇒ Object
225
226
227
|
# File 'app/models/badge.rb', line 225
def self.display_name(name)
I18n.t(i18n_key(name), default: name)
end
|
.ensure_consistency! ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'app/models/badge.rb', line 159
def self.ensure_consistency!
DB.exec <<~SQL
DELETE FROM user_badges
USING user_badges ub
LEFT JOIN users u ON u.id = ub.user_id
WHERE u.id IS NULL
AND user_badges.id = ub.id
SQL
DB.exec <<~SQL
WITH X AS (
SELECT badge_id
, COUNT(user_id) users
FROM user_badges
GROUP BY badge_id
)
UPDATE badges
SET grant_count = X.users
FROM X
WHERE id = X.badge_id
AND grant_count <> X.users
SQL
end
|
.find_system_badge_id_from_translation_key(translation_key) ⇒ Object
233
234
235
236
237
|
# File 'app/models/badge.rb', line 233
def self.find_system_badge_id_from_translation_key(translation_key)
return unless translation_key.starts_with?("badges.")
badge_name_klass = translation_key.split(".").second.camelize
Badge.const_defined?(badge_name_klass) ? "Badge::#{badge_name_klass}".constantize : nil
end
|
.i18n_key(name) ⇒ Object
229
230
231
|
# File 'app/models/badge.rb', line 229
def self.i18n_key(name)
"badges.#{i18n_name(name)}.name"
end
|
.i18n_name(name) ⇒ Object
221
222
223
|
# File 'app/models/badge.rb', line 221
def self.i18n_name(name)
name.to_s.downcase.tr(" ", "_")
end
|
.like_badge_counts ⇒ Object
.protected_system_fields ⇒ Object
fields that can not be edited on system badges
140
141
142
|
# File 'app/models/badge.rb', line 140
def self.protected_system_fields
%i[name badge_type_id multiple_grant target_posts show_posts query trigger auto_revoke listable]
end
|
.trigger_hash ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
|
# File 'app/models/badge.rb', line 75
def self.trigger_hash
@trigger_hash ||=
Badge::Trigger
.constants
.map do |k|
name = k.to_s.underscore
[name, Badge::Trigger.const_get(k)] unless name =~ /deprecated/
end
.compact
.to_h
end
|
.trust_level_badge_ids ⇒ Object
144
145
146
|
# File 'app/models/badge.rb', line 144
def self.trust_level_badge_ids
(1..4).to_a
end
|
Instance Method Details
#awarded_for_trust_level? ⇒ Boolean
239
240
241
|
# File 'app/models/badge.rb', line 239
def awarded_for_trust_level?
id <= 4
end
|
#clear_user_titles! ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'app/models/badge.rb', line 183
def clear_user_titles!
DB.exec(<<~SQL, badge_id: self.id, updated_at: Time.zone.now)
UPDATE users AS u
SET title = '', updated_at = :updated_at
FROM user_profiles AS up
WHERE up.user_id = u.id AND up.granted_title_badge_id = :badge_id
SQL
DB.exec(<<~SQL, badge_id: self.id)
UPDATE user_profiles AS up
SET granted_title_badge_id = NULL
WHERE up.granted_title_badge_id = :badge_id
SQL
end
|
#default_allow_title=(val) ⇒ Object
259
260
261
262
|
# File 'app/models/badge.rb', line 259
def default_allow_title=(val)
return if !self.new_record?
self.allow_title = val
end
|
#default_badge_grouping_id=(val) ⇒ Object
269
270
271
272
273
274
|
# File 'app/models/badge.rb', line 269
def default_badge_grouping_id=(val)
if !self.badge_grouping_id || self.badge_grouping_id <= BadgeGrouping::Other
self.badge_grouping_id = val
end
end
|
#default_enabled=(val) ⇒ Object
264
265
266
267
|
# File 'app/models/badge.rb', line 264
def default_enabled=(val)
return if !self.new_record?
self.enabled = val
end
|
#default_icon=(val) ⇒ Object
252
253
254
255
256
257
|
# File 'app/models/badge.rb', line 252
def default_icon=(val)
if self.image_upload_id.blank?
self.icon ||= val
self.icon = val if self.icon == "certificate"
end
end
|
#description ⇒ Object
298
299
300
301
302
303
304
305
306
|
# File 'app/models/badge.rb', line 298
def description
key = "badges.#{i18n_name}.description"
I18n.t(
key,
default: self[:description] || "",
base_uri: Discourse.base_path,
max_likes_per_day: SiteSetting.max_likes_per_day,
)
end
|
#description=(val) ⇒ Object
308
309
310
|
# File 'app/models/badge.rb', line 308
def description=(val)
self[:description] = val if val != description
end
|
#display_name ⇒ Object
276
277
278
|
# File 'app/models/badge.rb', line 276
def display_name
self.class.display_name(name)
end
|
#for_beginners? ⇒ Boolean
#i18n_name ⇒ Object
320
321
322
|
# File 'app/models/badge.rb', line 320
def i18n_name
@i18n_name ||= self.class.i18n_name(name)
end
|
#image_url ⇒ Object
324
325
326
|
# File 'app/models/badge.rb', line 324
def image_url
upload_cdn_path(image_upload.url) if image_upload_id.present?
end
|
#long_description ⇒ Object
284
285
286
287
288
289
290
291
292
|
# File 'app/models/badge.rb', line 284
def long_description
key = "badges.#{i18n_name}.long_description"
I18n.t(
key,
default: self[:long_description] || "",
base_uri: Discourse.base_path,
max_likes_per_day: SiteSetting.max_likes_per_day,
)
end
|
#long_description=(val) ⇒ Object
294
295
296
|
# File 'app/models/badge.rb', line 294
def long_description=(val)
self[:long_description] = val if val != long_description
end
|
#manually_grantable? ⇒ Boolean
316
317
318
|
# File 'app/models/badge.rb', line 316
def manually_grantable?
query.blank? && !system?
end
|
#reset_grant_count! ⇒ Object
243
244
245
246
|
# File 'app/models/badge.rb', line 243
def reset_grant_count!
self.grant_count = UserBadge.where(badge_id: id).count
save!
end
|
#reset_user_titles! ⇒ Object
When a badge has its TranslationOverride cleared, reset all user titles granted to the standard name.
211
212
213
214
215
216
217
218
219
|
# File 'app/models/badge.rb', line 211
def reset_user_titles!
DB.exec(<<~SQL, granted_title_badge_id: self.id, updated_at: Time.zone.now)
UPDATE users AS u
SET title = badges.name, updated_at = :updated_at
FROM user_profiles AS up
INNER JOIN badges ON badges.id = up.granted_title_badge_id
WHERE up.user_id = u.id AND up.granted_title_badge_id = :granted_title_badge_id
SQL
end
|
#single_grant? ⇒ Boolean
248
249
250
|
# File 'app/models/badge.rb', line 248
def single_grant?
!self.multiple_grant?
end
|
#slug ⇒ Object
312
313
314
|
# File 'app/models/badge.rb', line 312
def slug
Slug.for(self.display_name, "-")
end
|
#translation_key ⇒ Object
280
281
282
|
# File 'app/models/badge.rb', line 280
def translation_key
self.class.i18n_key(name)
end
|
#update_user_titles!(new_title) ⇒ Object
Update all user titles based on a badge to the new name
199
200
201
202
203
204
205
206
|
# File 'app/models/badge.rb', line 199
def update_user_titles!(new_title)
DB.exec(<<~SQL, granted_title_badge_id: self.id, title: new_title, updated_at: Time.zone.now)
UPDATE users AS u
SET title = :title, updated_at = :updated_at
FROM user_profiles AS up
WHERE up.user_id = u.id AND up.granted_title_badge_id = :granted_title_badge_id
SQL
end
|