Skip to content

Commit

Permalink
Merge pull request #323 from strongloop/feature/do-not-inherit-base-m…
Browse files Browse the repository at this point in the history
…odel-option

Don't inherit settings.base when extending a model
  • Loading branch information
bajtos committed Oct 10, 2014
2 parents 0859fc4 + 14d178b commit d69b515
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/model-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
var ModelBaseClass = parent || this.defaultModelBaseClass;
var baseClass = settings.base || settings['super'];
if (baseClass) {
// Normalize base model property
settings.base = baseClass;
delete settings['super'];

if (isModelClass(baseClass)) {
ModelBaseClass = baseClass;
} else {
Expand Down Expand Up @@ -337,8 +341,15 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
}

// Merge the settings
var originalSubclassSettings = subclassSettings;
subclassSettings = mergeSettings(settings, subclassSettings);

// Ensure 'base' is not inherited. Note we don't have to delete 'super'
// as that is removed from settings by modelBuilder.define and thus
// it is never inherited
if (!originalSubclassSettings.base)
delete subclassSettings.base;

// Define the subclass
var subClass = modelBuilder.define(className, subclassProperties, subclassSettings, ModelClass);

Expand Down
20 changes: 20 additions & 0 deletions test/model-definition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,26 @@ describe('ModelDefinition class', function () {
assert(anotherChild.prototype instanceof baseChild);
});

it('should ignore inherited options.base', function() {
var memory = new DataSource({connector: Memory});
var modelBuilder = memory.modelBuilder;
var base = modelBuilder.define('base');
var child = base.extend('child', {}, { base: 'base' });
var grandChild = child.extend('grand-child');
assert.equal('child', grandChild.base.modelName);
assert(grandChild.prototype instanceof child);
});

it('should ignore inherited options.super', function() {
var memory = new DataSource({connector: Memory});
var modelBuilder = memory.modelBuilder;
var base = modelBuilder.define('base');
var child = base.extend('child', {}, { super: 'base' });
var grandChild = child.extend('grand-child');
assert.equal('child', grandChild.base.modelName);
assert(grandChild.prototype instanceof child);
});

it('should not serialize hidden properties into JSON', function () {
var memory = new DataSource({connector: Memory});
var modelBuilder = memory.modelBuilder;
Expand Down

0 comments on commit d69b515

Please sign in to comment.