Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Add method for removing behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
brianloveswords committed Oct 10, 2012
1 parent 761b49b commit 8a86228
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
19 changes: 15 additions & 4 deletions models/badge.js
Expand Up @@ -13,12 +13,12 @@ var maxLength = function (field, length) {
}

var BehaviorSchema = new Schema({
name: {
shortname: {
type: String,
trim: true,
required: true
},
required: {
count: {
type: Number,
min: 0,
required: true
Expand Down Expand Up @@ -57,6 +57,7 @@ var BadgeSchema = new Schema({
},
behaviors: {
type: [BehaviorSchema],
unique: true
},
prerequisites: {
type: [String]
Expand All @@ -72,9 +73,19 @@ function setShortnameDefault(next) {
}
BadgeSchema.pre('validate', setShortnameDefault);

Badge.findByBehavior = function findByBehavior(name, callback) {
var searchTerms = { behaviors: { '$elemMatch': { name: name }}};
Badge.findByBehavior = function findByBehavior(shortname, callback) {
var searchTerms = { behaviors: { '$elemMatch': { shortname: shortname }}};
return Badge.find(searchTerms, callback);
};

Badge.prototype.removeBehavior = function removeBehavior(shortname) {
var behaviors = this.behaviors.filter(function (behavior) {
if (behavior.shortname === shortname)
return null;
return behavior;
});
this.behaviors = behaviors;
return this;
};

module.exports = Badge;
18 changes: 15 additions & 3 deletions test/badge-model.test.js
Expand Up @@ -18,23 +18,23 @@ var fixtures = {
shortname: 'link-badge-basic',
description: 'For doing links.',
behaviors: [
{ name: 'link', required: 5 }
{ shortname: 'link', count: 5 }
]
}),
'link-advanced': new Badge({
name : 'Link Badge, advanced',
shortname: 'link-badge-advanced',
description: 'For doing lots of links.',
behaviors: [
{ name: 'link', required: 10 }
{ shortname: 'link', count: 10 }
]
}),
'comment': new Badge({
name : 'Commenting badge',
shortname: 'comment-badge',
description: 'For doing lots of comments.',
behaviors: [
{ name: 'comment', required: 5 }
{ shortname: 'comment', count: 5 }
]
})
};
Expand Down Expand Up @@ -97,6 +97,18 @@ test.applyFixtures(fixtures, function () {
});
});

test('Badge#removeBehavior', function (t) {
var badge = validBadge();
badge.behaviors = [
{ shortname: 'link', count: 10 },
{ shortname: 'comment', count: 20 }
];
badge.removeBehavior('link');
t.same(badge.behaviors.length, 1, 'should have one left');
t.same(badge.behaviors[0].shortname, 'comment', 'should be the comment one');
t.end();
});


// necessary to stop the test runner
test('shutting down #', function (t) {
Expand Down

0 comments on commit 8a86228

Please sign in to comment.