2014-03-05 07:52:20 -05:00
|
|
|
class UserBadge < ActiveRecord::Base
|
|
|
|
belongs_to :badge
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :granted_by, class_name: 'User'
|
2014-06-17 01:59:28 -04:00
|
|
|
belongs_to :notification, dependent: :destroy
|
2014-06-27 15:08:03 -04:00
|
|
|
belongs_to :post
|
2014-03-05 07:52:20 -05:00
|
|
|
|
2014-05-21 03:22:42 -04:00
|
|
|
validates :badge_id, presence: true, uniqueness: {scope: :user_id}, if: 'badge.single_grant?'
|
2014-03-05 07:52:20 -05:00
|
|
|
validates :user_id, presence: true
|
|
|
|
validates :granted_at, presence: true
|
|
|
|
validates :granted_by, presence: true
|
2014-06-17 11:11:52 -04:00
|
|
|
|
|
|
|
after_create do
|
|
|
|
Badge.increment_counter 'grant_count', self.badge_id
|
|
|
|
end
|
|
|
|
|
|
|
|
after_destroy do
|
|
|
|
Badge.decrement_counter 'grant_count', self.badge_id
|
|
|
|
end
|
2014-06-27 15:27:26 -04:00
|
|
|
|
2014-08-08 15:13:11 -04:00
|
|
|
|
2014-06-27 15:27:26 -04:00
|
|
|
# Make sure we don't have duplicate badges.
|
|
|
|
def self.ensure_consistency!
|
2014-08-08 15:13:11 -04:00
|
|
|
dup_ids = exec_sql("SELECT u1.id
|
|
|
|
FROM user_badges u1, user_badges u2, badges
|
|
|
|
WHERE u1.badge_id = badges.id
|
|
|
|
AND u1.user_id = u2.user_id
|
|
|
|
AND u1.badge_id = u2.badge_id
|
|
|
|
AND (NOT badges.multiple_grant)
|
|
|
|
AND u1.granted_at > u2.granted_at
|
|
|
|
LIMIT 1000").to_a
|
|
|
|
|
|
|
|
dup_ids << exec_sql("SELECT u1.id
|
|
|
|
FROM user_badges u1, user_badges u2, badges
|
|
|
|
WHERE u1.badge_id = badges.id
|
|
|
|
AND u1.user_id = u2.user_id
|
|
|
|
AND u1.badge_id = u2.badge_id
|
|
|
|
AND badges.multiple_grant
|
|
|
|
AND u1.post_id = u2.post_id
|
|
|
|
AND u1.granted_at > u2.granted_at
|
|
|
|
LIMIT 1000").to_a
|
|
|
|
|
|
|
|
dup_ids.flatten!
|
|
|
|
dup_ids.map! {|row| row['id'].to_i }
|
|
|
|
dup_ids.uniq!
|
2014-06-27 15:27:26 -04:00
|
|
|
UserBadge.where(id: dup_ids).destroy_all
|
|
|
|
end
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_badges
|
|
|
|
#
|
2014-06-17 01:59:28 -04: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-03-05 07:52:20 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2014-07-14 21:29:44 -04: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-03-05 07:52:20 -05:00
|
|
|
#
|