2014-03-05 20:52:20 +08:00
|
|
|
class UserBadge < ActiveRecord::Base
|
|
|
|
belongs_to :badge
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :granted_by, class_name: 'User'
|
2014-06-17 13:59:28 +08:00
|
|
|
belongs_to :notification, dependent: :destroy
|
2014-06-28 03:08:03 +08:00
|
|
|
belongs_to :post
|
2014-03-05 20:52:20 +08:00
|
|
|
|
2014-05-21 15:22:42 +08:00
|
|
|
validates :badge_id, presence: true, uniqueness: {scope: :user_id}, if: 'badge.single_grant?'
|
2014-03-05 20:52:20 +08:00
|
|
|
validates :user_id, presence: true
|
|
|
|
validates :granted_at, presence: true
|
|
|
|
validates :granted_by, presence: true
|
2014-06-17 23:11:52 +08:00
|
|
|
|
|
|
|
after_create do
|
|
|
|
Badge.increment_counter 'grant_count', self.badge_id
|
2015-02-26 08:50:58 +08:00
|
|
|
DiscourseEvent.trigger(:user_badge_granted, self.badge_id, self.user_id)
|
2014-06-17 23:11:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
after_destroy do
|
|
|
|
Badge.decrement_counter 'grant_count', self.badge_id
|
2015-02-26 08:50:58 +08:00
|
|
|
DiscourseEvent.trigger(:user_badge_removed, self.badge_id, self.user_id)
|
2014-06-17 23:11:52 +08:00
|
|
|
end
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_badges
|
|
|
|
#
|
2014-06-17 13:59:28 +08:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# badge_id :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# granted_at :datetime not null
|
|
|
|
# granted_by_id :integer not null
|
|
|
|
# post_id :integer
|
|
|
|
# notification_id :integer
|
2014-08-11 07:21:06 +08:00
|
|
|
# seq :integer default(0), not null
|
2014-03-05 20:52:20 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2014-07-15 09:29:44 +08:00
|
|
|
# index_user_badges_on_badge_id_and_user_id (badge_id,user_id)
|
|
|
|
# index_user_badges_on_badge_id_and_user_id_and_post_id (badge_id,user_id,post_id) UNIQUE
|
2014-08-11 07:21:06 +08:00
|
|
|
# index_user_badges_on_badge_id_and_user_id_and_seq (badge_id,user_id,seq) UNIQUE
|
2016-02-23 07:33:53 +08:00
|
|
|
# index_user_badges_on_user_id (user_id)
|
2014-03-05 20:52:20 +08:00
|
|
|
#
|