From a79bf11edbe7bef5f67a57890d53407455445885 Mon Sep 17 00:00:00 2001 From: Vikhyat Korrapati Date: Sat, 24 May 2014 08:03:46 +0530 Subject: [PATCH] Admin badge creation bug fixes. --- .../controllers/admin_badges_controller.js | 35 ++++++++++++++++--- .../admin/templates/badges.js.handlebars | 2 +- .../javascripts/discourse/models/badge.js | 4 +-- app/models/badge.rb | 3 +- ...0520062826_add_multiple_award_to_badges.rb | 2 +- .../admin/badges_controller_spec.rb | 4 +-- .../admin_badges_controller_test.js | 4 +-- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/admin_badges_controller.js b/app/assets/javascripts/admin/controllers/admin_badges_controller.js index 114b106a8..baac6bf7f 100644 --- a/app/assets/javascripts/admin/controllers/admin_badges_controller.js +++ b/app/assets/javascripts/admin/controllers/admin_badges_controller.js @@ -21,15 +21,42 @@ Discourse.AdminBadgesController = Ember.ArrayController.extend({ /** Badge that is currently selected. + @property selectedItem @type {Discourse.Badge} **/ selectedItem: function() { - var selectedId = parseInt(this.get('selectedId')); + if (this.get('selectedId') === undefined || this.get('selectedId') === "undefined") { + // New Badge + return this.get('newBadge'); + } else { + // Existing Badge + var selectedId = parseInt(this.get('selectedId')); + return this.get('model').filter(function(badge) { + return parseInt(badge.get('id')) === selectedId; + })[0]; + } + }.property('selectedId', 'newBadge'), + + /** + Unsaved badge, if one exists. + + @property newBadge + @type {Discourse.Badge} + **/ + newBadge: function() { return this.get('model').filter(function(badge) { - return badge.get('id') === selectedId; + return badge.get('id') === undefined; })[0]; - }.property('selectedId'), + }.property('model.@each.id'), + + /** + Whether a new unsaved badge exists. + + @property newBadgeExists + @type {Discourse.Badge} + **/ + newBadgeExists: Em.computed.notEmpty('newBadge'), /** We don't allow setting a description if a translation for the given badge @@ -55,7 +82,7 @@ Discourse.AdminBadgesController = Ember.ArrayController.extend({ @method newBadge **/ - newBadge: function() { + createNewBadge: function() { var badge = Discourse.Badge.create({ name: I18n.t('admin.badges.new_badge') }); diff --git a/app/assets/javascripts/admin/templates/badges.js.handlebars b/app/assets/javascripts/admin/templates/badges.js.handlebars index 204960ba6..ae0524def 100644 --- a/app/assets/javascripts/admin/templates/badges.js.handlebars +++ b/app/assets/javascripts/admin/templates/badges.js.handlebars @@ -14,7 +14,7 @@ {{/each}} - + {{#if selectedItem}} diff --git a/app/assets/javascripts/discourse/models/badge.js b/app/assets/javascripts/discourse/models/badge.js index b225d3c96..f709ef29c 100644 --- a/app/assets/javascripts/discourse/models/badge.js +++ b/app/assets/javascripts/discourse/models/badge.js @@ -115,8 +115,8 @@ Discourse.Badge = Discourse.Model.extend({ name: this.get('name'), description: this.get('description'), badge_type_id: this.get('badge_type_id'), - allow_title: this.get('allow_title'), - multiple_grant: this.get('multiple_grant') + allow_title: !!this.get('allow_title'), + multiple_grant: !!this.get('multiple_grant') } }).then(function(json) { self.updateFromJson(json); diff --git a/app/models/badge.rb b/app/models/badge.rb index 2d69940e2..b891ef02d 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -5,6 +5,7 @@ class Badge < ActiveRecord::Base validates :name, presence: true, uniqueness: true validates :badge_type, presence: true validates :allow_title, inclusion: [true, false] + validates :multiple_grant, inclusion: [true, false] def self.trust_level_badge_ids (1..4).to_a @@ -33,7 +34,7 @@ end # created_at :datetime # updated_at :datetime # allow_title :boolean default(FALSE), not null -# multiple_grant :boolean default(FALSE) +# multiple_grant :boolean default(FALSE), not null # # Indexes # diff --git a/db/migrate/20140520062826_add_multiple_award_to_badges.rb b/db/migrate/20140520062826_add_multiple_award_to_badges.rb index b4495045f..4e7fb1bdf 100644 --- a/db/migrate/20140520062826_add_multiple_award_to_badges.rb +++ b/db/migrate/20140520062826_add_multiple_award_to_badges.rb @@ -1,6 +1,6 @@ class AddMultipleAwardToBadges < ActiveRecord::Migration def change - add_column :badges, :multiple_grant, :boolean, default: false + add_column :badges, :multiple_grant, :boolean, default: false, null: false reversible do |dir| dir.up do diff --git a/spec/controllers/admin/badges_controller_spec.rb b/spec/controllers/admin/badges_controller_spec.rb index b507c6a7a..478b61b68 100644 --- a/spec/controllers/admin/badges_controller_spec.rb +++ b/spec/controllers/admin/badges_controller_spec.rb @@ -35,12 +35,12 @@ describe Admin::BadgesController do context '.update' do it 'returns success' do - xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false + xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false, multiple_grant: false response.should be_success end it 'updates the badge' do - xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false + xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false, multiple_grant: true badge.reload.name.should eq('123456') end end diff --git a/test/javascripts/admin/controllers/admin_badges_controller_test.js b/test/javascripts/admin/controllers/admin_badges_controller_test.js index d677e1e07..ccf6aa7cf 100644 --- a/test/javascripts/admin/controllers/admin_badges_controller_test.js +++ b/test/javascripts/admin/controllers/admin_badges_controller_test.js @@ -15,9 +15,9 @@ test("canEditDescription", function() { ok(!controller.get('canEditDescription'), "shows the displayName when it is different from the name"); }); -test("newBadge", function() { +test("createNewBadge", function() { var controller = testController(Discourse.AdminBadgesController, []); - controller.send('newBadge'); + controller.send('createNewBadge'); equal(controller.get('model.length'), 1, "adds a new badge to the list of badges"); });