diff --git a/app/models/badge.rb b/app/models/badge.rb index b891ef02d6f..d090c61a25f 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -35,6 +35,7 @@ end # updated_at :datetime # allow_title :boolean default(FALSE), not null # multiple_grant :boolean default(FALSE), not null +# icon :string(255) default("fa-certificate") # # Indexes # diff --git a/app/models/user_badge.rb b/app/models/user_badge.rb index 39453322f89..5781bd6527c 100644 --- a/app/models/user_badge.rb +++ b/app/models/user_badge.rb @@ -2,28 +2,25 @@ class UserBadge < ActiveRecord::Base belongs_to :badge belongs_to :user belongs_to :granted_by, class_name: 'User' + belongs_to :notification, dependent: :destroy validates :badge_id, presence: true, uniqueness: {scope: :user_id}, if: 'badge.single_grant?' 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 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 +# 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 # # Indexes # diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb index 11fbec47d45..35c1f228fc4 100644 --- a/app/services/badge_granter.rb +++ b/app/services/badge_granter.rb @@ -25,9 +25,8 @@ class BadgeGranter end if SiteSetting.enable_badges? - @user.notifications.create(notification_type: Notification.types[:granted_badge], - data: { badge_id: @badge.id, - badge_name: @badge.name }.to_json) + notification = @user.notifications.create(notification_type: Notification.types[:granted_badge], data: { badge_id: @badge.id, badge_name: @badge.name }.to_json) + user_badge.update_attributes notification_id: notification.id end end end @@ -48,8 +47,6 @@ class BadgeGranter user_badge.user.title = nil user_badge.user.save! end - - user_badge.notification && user_badge.notification.destroy! end end diff --git a/db/migrate/20140617053829_add_notification_id_to_user_badge.rb b/db/migrate/20140617053829_add_notification_id_to_user_badge.rb new file mode 100644 index 00000000000..e066ac3cf02 --- /dev/null +++ b/db/migrate/20140617053829_add_notification_id_to_user_badge.rb @@ -0,0 +1,5 @@ +class AddNotificationIdToUserBadge < ActiveRecord::Migration + def change + add_column :user_badges, :notification_id, :integer + end +end