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 def create
badge = Badge.new badge = Badge.new
update_badge_from_params(badge) update_badge_from_params(badge)
badge.id = nil
badge.save! badge.save!
render_serialized(badge, BadgeSerializer, root: "badge") render_serialized(badge, BadgeSerializer, root: "badge")
end end

View File

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

View File

@ -56,6 +56,7 @@ trust_level_badges.each do |spec|
# allow title for leader and elder # allow title for leader and elder
b.default_allow_title = spec[:id] > 2 b.default_allow_title = spec[:id] > 2
b.system = true
end end
end end
@ -69,6 +70,7 @@ Badge.seed do |b|
b.query = Badge::Queries::Reader b.query = Badge::Queries::Reader
b.default_badge_grouping_id = BadgeGrouping::GettingStarted b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.auto_revoke = false b.auto_revoke = false
b.system = true
end end
Badge.seed do |b| Badge.seed do |b|
@ -81,6 +83,7 @@ Badge.seed do |b|
b.query = Badge::Queries::ReadGuidelines b.query = Badge::Queries::ReadGuidelines
b.default_badge_grouping_id = BadgeGrouping::GettingStarted b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.trigger = Badge::Trigger::UserChange b.trigger = Badge::Trigger::UserChange
b.system = true
end end
Badge.seed do |b| Badge.seed do |b|
@ -93,6 +96,7 @@ Badge.seed do |b|
b.query = Badge::Queries::FirstLink b.query = Badge::Queries::FirstLink
b.default_badge_grouping_id = BadgeGrouping::GettingStarted b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.trigger = Badge::Trigger::PostRevision b.trigger = Badge::Trigger::PostRevision
b.system = true
end end
Badge.seed do |b| Badge.seed do |b|
@ -105,6 +109,7 @@ Badge.seed do |b|
b.query = Badge::Queries::FirstQuote b.query = Badge::Queries::FirstQuote
b.default_badge_grouping_id = BadgeGrouping::GettingStarted b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.trigger = Badge::Trigger::PostRevision b.trigger = Badge::Trigger::PostRevision
b.system = true
end end
Badge.seed do |b| Badge.seed do |b|
@ -117,6 +122,7 @@ Badge.seed do |b|
b.query = Badge::Queries::FirstLike b.query = Badge::Queries::FirstLike
b.default_badge_grouping_id = BadgeGrouping::GettingStarted b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.trigger = Badge::Trigger::PostAction b.trigger = Badge::Trigger::PostAction
b.system = true
end end
Badge.seed do |b| Badge.seed do |b|
@ -129,6 +135,7 @@ Badge.seed do |b|
b.query = Badge::Queries::FirstFlag b.query = Badge::Queries::FirstFlag
b.default_badge_grouping_id = BadgeGrouping::Community b.default_badge_grouping_id = BadgeGrouping::Community
b.trigger = Badge::Trigger::PostAction b.trigger = Badge::Trigger::PostAction
b.system = true
end end
Badge.seed do |b| Badge.seed do |b|
@ -142,6 +149,7 @@ Badge.seed do |b|
b.default_badge_grouping_id = BadgeGrouping::GettingStarted b.default_badge_grouping_id = BadgeGrouping::GettingStarted
# don't trigger for now, its too expensive # don't trigger for now, its too expensive
b.trigger = Badge::Trigger::None b.trigger = Badge::Trigger::None
b.system = true
end end
Badge.seed do |b| Badge.seed do |b|
@ -154,6 +162,7 @@ Badge.seed do |b|
b.query = Badge::Queries::Welcome b.query = Badge::Queries::Welcome
b.default_badge_grouping_id = BadgeGrouping::Community b.default_badge_grouping_id = BadgeGrouping::Community
b.trigger = Badge::Trigger::PostAction b.trigger = Badge::Trigger::PostAction
b.system = true
end end
Badge.seed do |b| Badge.seed do |b|
@ -164,6 +173,7 @@ Badge.seed do |b|
b.query = Badge::Queries::Autobiographer b.query = Badge::Queries::Autobiographer
b.default_badge_grouping_id = BadgeGrouping::GettingStarted b.default_badge_grouping_id = BadgeGrouping::GettingStarted
b.trigger = Badge::Trigger::UserChange b.trigger = Badge::Trigger::UserChange
b.system = true
end end
Badge.seed do |b| Badge.seed do |b|
@ -174,6 +184,7 @@ Badge.seed do |b|
b.query = Badge::Queries::Editor b.query = Badge::Queries::Editor
b.default_badge_grouping_id = BadgeGrouping::Community b.default_badge_grouping_id = BadgeGrouping::Community
b.trigger = Badge::Trigger::PostRevision b.trigger = Badge::Trigger::PostRevision
b.system = true
end end
# #
@ -195,5 +206,13 @@ like_badges.each do |spec|
b.query = Badge::Queries.like_badge(Badge.like_badge_counts[spec[:id]]) b.query = Badge::Queries.like_badge(Badge.like_badge_counts[spec[:id]])
b.default_badge_grouping_id = BadgeGrouping::Posting b.default_badge_grouping_id = BadgeGrouping::Posting
b.trigger = Badge::Trigger::PostAction b.trigger = Badge::Trigger::PostAction
b.system = true
end end
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 describe Badge do
it 'has a valid system attribute for new badges' 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
end end