diff --git a/app/controllers/admin/badges_controller.rb b/app/controllers/admin/badges_controller.rb index 900b92b8e77..cd5170ee98d 100644 --- a/app/controllers/admin/badges_controller.rb +++ b/app/controllers/admin/badges_controller.rb @@ -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 diff --git a/app/models/badge.rb b/app/models/badge.rb index 3ba23b1f63f..aa7dbcdbf21 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -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 diff --git a/db/fixtures/006_badges.rb b/db/fixtures/006_badges.rb index 855ae399948..5bb3f7b2478 100644 --- a/db/fixtures/006_badges.rb +++ b/db/fixtures/006_badges.rb @@ -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 diff --git a/db/migrate/20140806003116_fixup_badge_ids.rb b/db/migrate/20140806003116_fixup_badge_ids.rb new file mode 100644 index 00000000000..61dc0a4caa7 --- /dev/null +++ b/db/migrate/20140806003116_fixup_badge_ids.rb @@ -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 diff --git a/spec/models/badge_spec.rb b/spec/models/badge_spec.rb index 050d5bb9948..07fddcf0b41 100644 --- a/spec/models/badge_spec.rb +++ b/spec/models/badge_spec.rb @@ -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