Skip to content

Commit

Permalink
DS.FixtureAdapter writes changes to FIXTURES
Browse files Browse the repository at this point in the history
  • Loading branch information
twinturbo committed Feb 27, 2013
1 parent 73ad70c commit f44ee30
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
52 changes: 52 additions & 0 deletions packages/ember-data/lib/adapters/fixture_adapter.js
Expand Up @@ -46,6 +46,18 @@ DS.FixtureAdapter = DS.Adapter.extend({
Ember.assert('Not implemented: You must override the DS.FixtureAdapter::queryFixtures method to support querying the fixture store.');
},

storeFixture: function(type, fixture) {
if(!type.FIXTURES) {
type.FIXTURES = []
}

fixtures = type.FIXTURES

this.deleteLoadedFixture(type, fixture);

fixtures.push(fixture);
},

/*
Implement this method in order to provide provide json for CRUD methods
*/
Expand Down Expand Up @@ -124,6 +136,8 @@ DS.FixtureAdapter = DS.Adapter.extend({

fixture.id = this.generateIdForRecord(store, record);

this.storeFixture(type, fixture);

this.simulateRemoteCall(function() {
this.didCreateRecord(store, type, record, fixture);
}, this);
Expand All @@ -132,12 +146,18 @@ DS.FixtureAdapter = DS.Adapter.extend({
updateRecord: function(store, type, record) {
var fixture = this.mockJSON(type, record);

this.storeFixture(type, fixture);

this.simulateRemoteCall(function() {
this.didUpdateRecord(store, type, record, fixture);
}, this);
},

deleteRecord: function(store, type, record) {
var fixture = this.mockJSON(type, record);

this.deleteLoadedFixture(type, fixture);

this.simulateRemoteCall(function() {
this.didDeleteRecord(store, type, record);
}, this);
Expand All @@ -146,6 +166,38 @@ DS.FixtureAdapter = DS.Adapter.extend({
/*
@private
*/
deleteLoadedFixture: function(type, record) {
var id = this.extractId(type, record);

var existingFixture = this.findExistingFixture(type, record);

if(existingFixture) {
var fixtures = this.fixturesForType(type, record);
var index = records.indexOf(existingRecord);
records.splice(index, 1);
return true;
}
},

findExistingFixture: function(type, record) {
var fixtures = this.fixturesForType(type);
var id = this.extractId(type, record);

return this.findFixtureById(fixtures, id);
},

findFixtureById: function(fixtures, id) {
var adapter = this;

return Ember.A(fixtures).find(function(r) {
if(''+get(r, 'id') === ''+id) {
return true;
} else {
return false;
}
});
},

simulateRemoteCall: function(callback, context) {
if (get(this, 'simulateRemoteResponse')) {
// Schedule with setTimeout
Expand Down
4 changes: 4 additions & 0 deletions packages/ember-data/lib/serializers/fixture_serializer.js
Expand Up @@ -11,6 +11,10 @@ DS.FixtureSerializer = DS.Serializer.extend({
return value;
},

addAttribute: function(hash, key, value) {
hash[key] = value;
},

/**
@private
Expand Down
32 changes: 28 additions & 4 deletions packages/ember-data/tests/unit/fixture_adapter_test.js
Expand Up @@ -27,6 +27,9 @@ module("DS.FixtureAdapter", {

// Enable setTimeout.
Ember.testing = false;

Person.FIXTURES = [];
Phone.FIXTURES = [];
},
teardown: function() {
Ember.testing = true;
Expand Down Expand Up @@ -131,6 +134,8 @@ test("should load data asynchronously at the end of the runloop when simulateRem
test("should create record asynchronously when it is committed", function() {
stop();

equal(Person.FIXTURES.length, 0, "Fixtures is empty");

var paul = store.createRecord(Person, {firstName: 'Paul', lastName: 'Chavard', height: 70});

paul.on('didCreate', function() {
Expand All @@ -140,6 +145,14 @@ test("should create record asynchronously when it is committed", function() {
equal(get(paul, 'isNew'), false, "data loads asynchronously");
equal(get(paul, 'isDirty'), false, "data loads asynchronously");
equal(get(paul, 'height'), 70, "data from fixtures is saved correctly");

equal(Person.FIXTURES.length, 1, "Record added to FIXTURES");

var fixture = Person.FIXTURES[0]

equal(fixture.firstName, 'Paul');
equal(fixture.lastName, 'Chavard');
equal(fixture.height, 70);
});

store.commit();
Expand All @@ -153,6 +166,8 @@ test("should create record asynchronously when it is committed", function() {
test("should update record asynchronously when it is committed", function() {
stop();

equal(Person.FIXTURES.length, 0, "Fixtures is empty");

var paul = store.findByClientId(Person, store.load(Person, 1, {firstName: 'Paul', lastName: 'Chavard', height: 70}).clientId);

paul.set('height', 80);
Expand All @@ -163,6 +178,14 @@ test("should update record asynchronously when it is committed", function() {

equal(get(paul, 'isDirty'), false, "data loads asynchronously");
equal(get(paul, 'height'), 80, "data from fixtures is saved correctly");

equal(Person.FIXTURES.length, 1, "Record FIXTURES updated");

var fixture = Person.FIXTURES[0]

equal(fixture.firstName, 'Paul');
equal(fixture.lastName, 'Chavard');
equal(fixture.height, 80);
});

store.commit();
Expand All @@ -176,7 +199,9 @@ test("should update record asynchronously when it is committed", function() {
test("should delete record asynchronously when it is committed", function() {
stop();

var paul = store.findByClientId(Person, store.load(Person, 1, { firstName: 'Paul', lastName: 'Chavard', height: 70}).clientId);
equal(Person.FIXTURES.length, 0, "Fixtures empty");

var paul = store.findByClientId(Person, store.load(Person, 1, {firstName: 'Paul', lastName: 'Chavard', height: 70}).clientId);

paul.deleteRecord();

Expand All @@ -186,6 +211,8 @@ test("should delete record asynchronously when it is committed", function() {

equal(get(paul, 'isDeleted'), true, "data deleted asynchronously");
equal(get(paul, 'isDirty'), false, "data deleted asynchronously");

equal(Person.FIXTURES.length, 0, "Record removed from FIXTURES");
});

store.commit();
Expand Down Expand Up @@ -247,7 +274,6 @@ test("should coerce integer ids into string", function() {
});

test("should throw if ids are not defined in the FIXTURES", function() {

Person.FIXTURES = [{
firstName: "Adam",
lastName: "Hawkins",
Expand All @@ -257,6 +283,4 @@ test("should throw if ids are not defined in the FIXTURES", function() {
raises(function(){
Person.find("1");
}, /the id property must be defined for fixture/);


});

0 comments on commit f44ee30

Please sign in to comment.