FIX: system badges where created under id 100

This commit is contained in:
Sam 2014-08-06 10:51:39 +10:00
parent 70b5dfc4fa
commit 0b01310c84
5 changed files with 39 additions and 5 deletions

View File

@ -46,6 +46,7 @@ class Admin::BadgesController < Admin::AdminController
def create
badge = Badge.new
update_badge_from_params(badge)
badge.id = nil
badge.save!
render_serialized(badge, BadgeSerializer, root: "badge")
end

View File

@ -176,6 +176,8 @@ SQL
scope :enabled, ->{ where(enabled: true) }
before_create :ensure_not_system
# fields that can not be edited on system badges
def self.protected_system_fields
[:badge_type_id, :multiple_grant, :target_posts, :show_posts, :query, :trigger, :auto_revoke, :listable]
@ -203,10 +205,6 @@ SQL
!self.multiple_grant?
end
def system?
id && id < 100
end
def default_name=(val)
self.name ||= val
end
@ -221,6 +219,13 @@ SQL
self.badge_grouping_id = val
end
end
protected
def ensure_not_system
unless id
self.id = [Badge.maximum(:id) + 1, 100].max
end
end
end
# == Schema Information

View File

@ -56,6 +56,7 @@ trust_level_badges.each do |spec|
# allow title for leader and elder
b.default_allow_title = spec[:id] > 2
b.system = true
end
end
@ -69,6 +70,7 @@ Badge.seed do |b|
b.query = Badge::Queries::Reader
b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.auto_revoke = false
b.system = true
end
Badge.seed do |b|
@ -81,6 +83,7 @@ Badge.seed do |b|
b.query = Badge::Queries::ReadGuidelines
b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.trigger = Badge::Trigger::UserChange
b.system = true
end
Badge.seed do |b|
@ -93,6 +96,7 @@ Badge.seed do |b|
b.query = Badge::Queries::FirstLink
b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.trigger = Badge::Trigger::PostRevision
b.system = true
end
Badge.seed do |b|
@ -105,6 +109,7 @@ Badge.seed do |b|
b.query = Badge::Queries::FirstQuote
b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.trigger = Badge::Trigger::PostRevision
b.system = true
end
Badge.seed do |b|
@ -117,6 +122,7 @@ Badge.seed do |b|
b.query = Badge::Queries::FirstLike
b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.trigger = Badge::Trigger::PostAction
b.system = true
end
Badge.seed do |b|
@ -129,6 +135,7 @@ Badge.seed do |b|
b.query = Badge::Queries::FirstFlag
b.default_badge_grouping_id = BadgeGrouping::Community
b.trigger = Badge::Trigger::PostAction
b.system = true
end
Badge.seed do |b|
@ -142,6 +149,7 @@ Badge.seed do |b|
b.default_badge_grouping_id = BadgeGrouping::GettingStarted
# don't trigger for now, its too expensive
b.trigger = Badge::Trigger::None
b.system = true
end
Badge.seed do |b|
@ -154,6 +162,7 @@ Badge.seed do |b|
b.query = Badge::Queries::Welcome
b.default_badge_grouping_id = BadgeGrouping::Community
b.trigger = Badge::Trigger::PostAction
b.system = true
end
Badge.seed do |b|
@ -164,6 +173,7 @@ Badge.seed do |b|
b.query = Badge::Queries::Autobiographer
b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.trigger = Badge::Trigger::UserChange
b.system = true
end
Badge.seed do |b|
@ -174,6 +184,7 @@ Badge.seed do |b|
b.query = Badge::Queries::Editor
b.default_badge_grouping_id = BadgeGrouping::Community
b.trigger = Badge::Trigger::PostRevision
b.system = true
end
#
@ -195,5 +206,13 @@ like_badges.each do |spec|
b.query = Badge::Queries.like_badge(Badge.like_badge_counts[spec[:id]])
b.default_badge_grouping_id = BadgeGrouping::Posting
b.trigger = Badge::Trigger::PostAction
b.system = true
end
end
Badge.where("NOT system AND id < 100").each do |badge|
new_id = [Badge.maximum(:id) + 1, 100].max
old_id = badge.id
badge.update_columns(id: new_id)
UserBadge.where(badge_id: old_id).update_all(badge_id: new_id)
end

View File

@ -0,0 +1,9 @@
class FixupBadgeIds < ActiveRecord::Migration
def change
# badge ids were below 100, for user badges, this really messed stuff up
# to resolve this add a "system" flag which we can use to figure out what
# badges to bump
add_column :badges, :system, :boolean, default: false, null: false
end
end

View File

@ -4,7 +4,7 @@ require_dependency 'badge'
describe Badge do
it 'has a valid system attribute for new badges' do
Badge.new.system?.should be_false
Badge.create!(name: "test", badge_type_id: 1).system?.should be_false
end
end