Skip to content

Commit

Permalink
Merge pull request #72 from gnarf/model-factory-injections
Browse files Browse the repository at this point in the history
Test under MODEL_FACTORY_INJECTIONS
  • Loading branch information
dgeb committed Jun 15, 2015
2 parents b28a269 + e1f4f6e commit 3f1d756
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
20 changes: 10 additions & 10 deletions lib/ember-orbit/model.js
Expand Up @@ -19,27 +19,27 @@ var Model = Ember.Object.extend(Ember.Evented, {

getKey: function(field) {
var store = get(this, 'store');
var pk = this.constructor.primaryKey;
var pk = get(this.constructor, 'primaryKey');

if (pk === field) {
return this.primaryId;
} else {
var type = this.constructor.typeKey;
var type = get(this.constructor, 'typeKey');
return store.retrieveKey(type, this.primaryId, field);
}
},

getAttribute: function(field) {
var store = get(this, 'store');
var type = this.constructor.typeKey;
var type = get(this.constructor, 'typeKey');
var id = get(this, 'primaryId');

return store.retrieveAttribute(type, id, field);
},

getLink: function(field) {
var store = get(this, 'store');
var type = this.constructor.typeKey;
var type = get(this.constructor, 'typeKey');
var id = get(this, 'primaryId');

var relatedRecord = store.retrieveLink(type, id, field) || null;
Expand All @@ -59,7 +59,7 @@ var Model = Ember.Object.extend(Ember.Evented, {

getLinks: function(field) {
var store = get(this, 'store');
var type = this.constructor.typeKey;
var type = get(this.constructor, 'typeKey');
var id = get(this, 'primaryId');

var relatedRecords = Ember.A(store.retrieveLinks(type, id, field) || []);
Expand All @@ -79,30 +79,30 @@ var Model = Ember.Object.extend(Ember.Evented, {

patch: function(field, value) {
var store = get(this, 'store');
var type = this.constructor.typeKey;
var type = get(this.constructor, 'typeKey');

return store.patch(type, this.primaryId, field, value);
},

addLink: function(field, relatedRecord) {
var store = get(this, 'store');
var type = this.constructor.typeKey;
var type = get(this.constructor, 'typeKey');
var relatedId = relatedRecord ? relatedRecord.primaryId : null;

return store.addLink(type, this.primaryId, field, relatedId);
},

removeLink: function(field, relatedRecord) {
var store = get(this, 'store');
var type = this.constructor.typeKey;
var type = get(this.constructor, 'typeKey');
var relatedId = relatedRecord ? relatedRecord.primaryId : null;

return store.removeLink(type, this.primaryId, field, relatedId);
},

remove: function() {
var store = get(this, 'store');
var type = this.constructor.typeKey;
var type = get(this.constructor, 'typeKey');

return store.remove(type, this.primaryId);
},
Expand All @@ -112,7 +112,7 @@ var Model = Ember.Object.extend(Ember.Evented, {
this._super();

var store = get(this, 'store');
var type = this.constructor.typeKey;
var type = get(this.constructor, 'typeKey');

store.unload(type, this.primaryId);
},
Expand Down
47 changes: 43 additions & 4 deletions test/tests/integration/model-integration-test.js
Expand Up @@ -13,7 +13,12 @@ var Planet,
Star,
store;

module("Integration - Model", {
// Run this integration test fully with MODEL_FACTORY_INJECTIONS both true and false
[true, false].forEach(function(MODEL_FACTORY_INJECTIONS) {

var flag = MODEL_FACTORY_INJECTIONS ? " w/ Ember.MODEL_FACTORY_INJECTIONS" : "";

module("Integration - Model" + flag, {
setup: function() {
Orbit.Promise = Ember.RSVP.Promise;

Expand All @@ -36,6 +41,7 @@ module("Integration - Model", {
});

store = createStore({
MODEL_FACTORY_INJECTIONS: MODEL_FACTORY_INJECTIONS,
models: {
star: Star,
moon: Moon,
Expand All @@ -59,9 +65,24 @@ test("store exists", function() {

test("store is properly linked to models", function() {
var schema = get(store, 'schema');
equal(schema.modelFor('star'), Star);
equal(schema.modelFor('planet'), Planet);
equal(schema.modelFor('moon'), Moon);
var tests = {
'star': Star,
'planet': Planet,
'moon': Moon,
};

expect(6);

for (var key in tests) {
var model = schema.modelFor(key);
if (MODEL_FACTORY_INJECTIONS) {
ok(model.proto() instanceof tests[key], key + ' model class is an extension of model class');
} else {
equal(model, tests[key], key + ' is equal to the model class');
}

equal(get(model, 'primaryKey'), 'id', key + ' primaryKey is id');
}
});

test("new models can be created and updated", function() {
Expand Down Expand Up @@ -94,6 +115,21 @@ test("new models can be created and updated", function() {
});
});

test("models can be found and accessed", function() {
expect(1);
var planetId;
Ember.run(function() {
store.add('planet', {name: 'Earth'})
.then(function(planet) {
planetId = planet.get('id');
return store.find('planet', planetId);
})
.then(function(planet) {
equal(planet.get('id'), planetId, 'Can retrieve primaryId');
});
});
});

test("new models will be assigned default values for attributes", function(){
expect(1);

Expand Down Expand Up @@ -428,3 +464,6 @@ test("models can be deleted", function() {
});
});
});

// MODEL_FACTORY_INJECTIONS
});
2 changes: 2 additions & 0 deletions test/tests/test-helper.js
Expand Up @@ -4,6 +4,8 @@ import Store from 'ember-orbit/store';
var createStore = function(options) {
options = options || {};

Ember.MODEL_FACTORY_INJECTIONS = !!options.MODEL_FACTORY_INJECTIONS;

var registry = new Ember.Registry();
var container = registry.container();
registry.register('schema:main', Schema);
Expand Down

0 comments on commit 3f1d756

Please sign in to comment.