mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Show when a badge has been granted for a post (#29696)
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com> Co-authored-by: Jarek Radosz <jradosz@gmail.com> Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
This commit is contained in:
@@ -344,27 +344,28 @@ end
|
||||
#
|
||||
# Table name: badges
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# name :string not null
|
||||
# description :text
|
||||
# badge_type_id :integer not null
|
||||
# grant_count :integer default(0), not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# allow_title :boolean default(FALSE), not null
|
||||
# multiple_grant :boolean default(FALSE), not null
|
||||
# icon :string default("fa-certificate")
|
||||
# listable :boolean default(TRUE)
|
||||
# target_posts :boolean default(FALSE)
|
||||
# query :text
|
||||
# enabled :boolean default(TRUE), not null
|
||||
# auto_revoke :boolean default(TRUE), not null
|
||||
# badge_grouping_id :integer default(5), not null
|
||||
# trigger :integer
|
||||
# show_posts :boolean default(FALSE), not null
|
||||
# system :boolean default(FALSE), not null
|
||||
# long_description :text
|
||||
# image_upload_id :integer
|
||||
# id :integer not null, primary key
|
||||
# name :string not null
|
||||
# description :text
|
||||
# badge_type_id :integer not null
|
||||
# grant_count :integer default(0), not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# allow_title :boolean default(FALSE), not null
|
||||
# multiple_grant :boolean default(FALSE), not null
|
||||
# icon :string default("fa-certificate")
|
||||
# listable :boolean default(TRUE)
|
||||
# target_posts :boolean default(FALSE)
|
||||
# query :text
|
||||
# enabled :boolean default(TRUE), not null
|
||||
# auto_revoke :boolean default(TRUE), not null
|
||||
# badge_grouping_id :integer default(5), not null
|
||||
# trigger :integer
|
||||
# show_posts :boolean default(FALSE), not null
|
||||
# system :boolean default(FALSE), not null
|
||||
# show_in_post_header :boolean default(FALSE), not null
|
||||
# long_description :text
|
||||
# image_upload_id :integer
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
||||
@@ -35,6 +35,19 @@ class UserBadge < ActiveRecord::Base
|
||||
scope :for_enabled_badges,
|
||||
-> { where("user_badges.badge_id IN (SELECT id FROM badges WHERE enabled)") }
|
||||
|
||||
scope :by_post_and_user,
|
||||
->(posts) do
|
||||
posts.reduce(UserBadge.none) do |scope, post|
|
||||
scope.or(UserBadge.where(user_id: post.user_id, post_id: post.id))
|
||||
end
|
||||
end
|
||||
scope :for_post_header_badges,
|
||||
->(posts) do
|
||||
by_post_and_user(posts).where(
|
||||
"user_badges.badge_id IN (SELECT id FROM badges WHERE show_posts AND enabled AND listable AND show_in_post_header)",
|
||||
)
|
||||
end
|
||||
|
||||
validates :badge_id, presence: true, uniqueness: { scope: :user_id }, if: :single_grant_badge?
|
||||
|
||||
validates :user_id, presence: true
|
||||
|
||||
Reference in New Issue
Block a user