2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-07 12:02:53 +08:00
discourse/app/models/user_badge.rb

33 lines
1.1 KiB
Ruby
Raw Normal View History

2014-03-05 18:22:20 +05:30
class UserBadge < ActiveRecord::Base
belongs_to :badge
belongs_to :user
belongs_to :granted_by, class_name: 'User'
2014-05-21 12:52:42 +05:30
validates :badge_id, presence: true, uniqueness: {scope: :user_id}, if: 'badge.single_grant?'
2014-03-05 18:22:20 +05:30
validates :user_id, presence: true
validates :granted_at, presence: true
validates :granted_by, presence: true
# This may be inefficient, but not very easy to optimize unless the data hash
# is converted into a hstore.
def notification
@notification ||= self.user.notifications.where(notification_type: Notification.types[:granted_badge]).where("data LIKE ?", "%" + self.badge_id.to_s + "%").select {|n| n.data_hash["badge_id"] == self.badge_id }.first
end
2014-03-05 18:22:20 +05:30
end
# == Schema Information
#
# Table name: user_badges
#
# 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
#
# Indexes
#
2014-05-21 12:52:42 +05:30
# index_user_badges_on_badge_id_and_user_id (badge_id,user_id)
# index_user_badges_on_user_id (user_id)
2014-03-05 18:22:20 +05:30
#