Skip to content

Commit

Permalink
Ordering now works with the new sortKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
nathansobo committed Jan 4, 2011
1 parent c232e60 commit 5140e57
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 70 deletions.
5 changes: 3 additions & 2 deletions lib/monarch/model/column.js
Expand Up @@ -4,7 +4,8 @@ _.constructor("Monarch.Model.Column", {
initialize: function(table, name, type) { initialize: function(table, name, type) {
this.table = table; this.table = table;
this.name = name; this.name = name;
this.qualifiedName = table.globalName + "." + name; this.globalName = _.underscore(name);
this.qualifiedName = table.globalName + "." + this.globalName;
this.type = type; this.type = type;
}, },


Expand Down Expand Up @@ -44,7 +45,7 @@ _.constructor("Monarch.Model.Column", {
return { return {
type: "column", type: "column",
table: this.table.globalName, table: this.table.globalName,
name: _.underscore(this.name) name: this.globalName
}; };
}, },


Expand Down
57 changes: 11 additions & 46 deletions lib/monarch/model/relations/ordering.js
Expand Up @@ -13,7 +13,11 @@ _.constructor("Monarch.Model.Relations.Ordering", Monarch.Model.Relations.Relati


tuples: function() { tuples: function() {
if (!this.comparator) this.comparator = this.buildComparator(); if (!this.comparator) this.comparator = this.buildComparator();
return this.operand.tuples().sort(this.comparator); return this.operand.tuples().sort(this.hitch('compareTuples'));
},

compareTuples: function(a, b) {
return this.comparator(this.buildSortKey(a), this.buildSortKey(b));
}, },


evaluateInRepository: function(repository) { evaluateInRepository: function(repository) {
Expand All @@ -38,55 +42,16 @@ _.constructor("Monarch.Model.Relations.Ordering", Monarch.Model.Relations.Relati


// private // private


tupleInsertedRemotely: function(tuple) { onOperandInsert: function(tuple) {
var position = this.storedTuples.insert(this.buildSortKey(tuple), tuple); this.tupleInsertedRemotely(tuple);
this.onInsertNode.publish(tuple, position);
},

tupleUpdatedRemotely: function($super, tuple, changeset) {
var positionMayChange = _.any(changeset, function(changedField) {
return this.sortingOnColumn(changedField.column);
}, this);

if (!positionMayChange) {
var currentPosition = this.storedTuples.indexOf(tuple);
$super(tuple, changeset, currentPosition, currentPosition);
return;
}

var oldPosition = this.storedTuples.remove(this.buildSortKey(tuple, changeset));
var newPosition = this.storedTuples.insert(this.buildSortKey(tuple), tuple);
$super(tuple, changeset, newPosition, oldPosition);
}, },


tupleRemovedRemotely: function(tuple) { onOperandUpdate: function(tuple, changset) {
var position = this.storedTuples.remove(this.buildSortKey(tuple)); this.tupleUpdatedRemotely(tuple, changset);
this.onRemoveNode.publish(tuple, position);
}, },


sortingOnColumn: function(column) { onOperandRemove: function(tuple) {
return _.detect(this.sortSpecifications, function(sortSpecification) { this.tupleRemovedRemotely(tuple);
return sortSpecification.column === column;
});
},

subscribeToOperands: function() {
this.operandsSubscriptionBundle.add(this.operand.onInsert(function(record) {
this.tupleInsertedRemotely(record);
}, this));

this.operandsSubscriptionBundle.add(this.operand.onRemove(function(record) {
this.tupleRemovedRemotely(record);
}, this));

this.operandsSubscriptionBundle.add(this.operand.onUpdate(function(record, changedFields) {
this.tupleUpdatedRemotely(record, changedFields);
}, this));

this.operandsSubscriptionBundle.add(this.operand.onDirty(this.hitch('recordMadeDirty')));
this.operandsSubscriptionBundle.add(this.operand.onClean(this.hitch('recordMadeClean')));
this.operandsSubscriptionBundle.add(this.operand.onInvalid(this.hitch('recordMadeInvalid')));
this.operandsSubscriptionBundle.add(this.operand.onValid(this.hitch('recordMadeValid')));
} }
}) })


Expand Down
64 changes: 42 additions & 22 deletions spec/monarch/model/relations/ordering_spec.js
Expand Up @@ -15,6 +15,8 @@ Screw.Unit(function(c) { with(c) {
ordering = new Monarch.Model.Relations.Ordering(operand, sortSpecifications); ordering = new Monarch.Model.Relations.Ordering(operand, sortSpecifications);


// created in a strange order to ensure that has no effect on sort // created in a strange order to ensure that has no effect on sort
// should go 1:(1 A), 2:(2 B), 3:(2 C), 4:(3 D)

user4 = User.createFromRemote({id: 4, age: 3, fullName: "D"}); user4 = User.createFromRemote({id: 4, age: 3, fullName: "D"});
user1 = User.createFromRemote({id: 1, age: 1, fullName: "A"}); user1 = User.createFromRemote({id: 1, age: 1, fullName: "A"});
user3 = User.createFromRemote({id: 3, age: 2, fullName: "C"}); user3 = User.createFromRemote({id: 3, age: 2, fullName: "C"});
Expand Down Expand Up @@ -58,49 +60,67 @@ Screw.Unit(function(c) { with(c) {
ordering.onUpdate(updateCallback); ordering.onUpdate(updateCallback);
}); });


describe("when a tuple is remotely inserted into the operand", function() { describe("when a tuple is inserted into the operand", function() {
it("triggers #onInsert callbacks with the inserted tuple and its index", function() { it("triggers #onInsert callbacks with the inserted tuple and its index", function() {
var record = User.createFromRemote({id: 5, age: 2, fullName: "D"}); var record = User.createFromRemote({id: 5, age: 2, fullName: "D"});
expect(insertCallback).to(haveBeenCalled, withArgs(record, 3)); var sortKey = ordering.buildSortKey(record);
expect(ordering.storedTuples.at(3)).to(eq, record);
expect(insertCallback).to(haveBeenCalled, withArgs(record, 3, sortKey, sortKey));
expect(ordering.at(3)).to(eq, record);
}); });
}); });


describe("when a tuple is remotely updated in the operand", function() { describe("when a tuple is updated in the operand", function() {
it("triggers #onUpdate callbacks with the updated tuple and its new and old index in the ordering", function() { it("triggers update events with the updated tuple and its new and old index", function() {
var tuplesLengthBefore = ordering.storedTuples.length; var sizeBefore = ordering.size();


user3.update({age: 20000}); user3.update({age: 20000});
expect(updateCallback).to(haveBeenCalled);
var args = updateCallback.mostRecentArgs; expect(updateCallback).to(haveBeenCalled, withArgs(
expect(args[0]).to(eq, user3); user3, // record
expect(args[2]).to(eq, 3); // new index { age: { column: User.age, oldValue: 2, newValue: 20000 } }, // changeset
expect(args[3]).to(eq, 2); // old index 3, 2, // new index, old index
{ 'users.id': 3, 'users.age': 20000, 'users.full_name': "C" }, // new sort key
{ 'users.id': 3, 'users.age': 2, 'users.full_name': "C" } // old sort key
));
expect(ordering.at(3)).to(eq, user3);


updateCallback.clear(); updateCallback.clear();
user3.update({age: 0}); user3.update({age: 0});
expect(updateCallback).to(haveBeenCalled);


var args = updateCallback.mostRecentArgs; expect(updateCallback).to(haveBeenCalled, withArgs(
expect(args[0]).to(eq, user3); user3, // record
expect(args[2]).to(eq, 0); // new index { age: { column: User.age, oldValue: 20000, newValue: 0 } }, // changeset
expect(args[3]).to(eq, 3); // old index 0, 3, // new index, old index
{ 'users.id': 3, 'users.age': 0, 'users.full_name': "C" }, // new sort key
{ 'users.id': 3, 'users.age': 20000, 'users.full_name': "C" } // old sort key
));


expect(ordering.storedTuples.length).to(eq, tuplesLengthBefore); expect(ordering.size()).to(eq, sizeBefore);
expect(ordering.storedTuples.at(0)).to(eq, user3); expect(ordering.at(0)).to(eq, user3);


updateCallback.clear(); updateCallback.clear();


var oldDate = user3.signedUpAt();
var newDate = new Date();
var sortKey = ordering.buildSortKey(user3);

user3.update({signedUpAt: new Date()}); user3.update({signedUpAt: new Date()});
expect(updateCallback).to(haveBeenCalled);
expect(updateCallback.mostRecentArgs[2]).to(eq, 0); expect(updateCallback).to(haveBeenCalled, withArgs(
expect(updateCallback.mostRecentArgs[3]).to(eq, 0); user3, // record
{ signedUpAt: { column: User.signedUpAt, oldValue: oldDate, newValue: newDate } }, // changeset
0, 0, sortKey, sortKey // new index, old index, new sort key, old sort key
));
}); });
}); });


describe("when a tuple is removed from the operand", function() { describe("when a tuple is removed from the operand", function() {
it("triggers #onRemove callbacks with the removed tuple and its former index", function() { it("triggers #onRemove callbacks with the removed tuple and its former index", function() {
var sortKey = ordering.buildSortKey(user2);
user2.remotelyDestroyed(); user2.remotelyDestroyed();
expect(removeCallback).to(haveBeenCalled, withArgs(user2, 1)); expect(removeCallback).to(haveBeenCalled, withArgs(user2, 1, sortKey, sortKey));
}); });
}); });
}); });
Expand Down

0 comments on commit 5140e57

Please sign in to comment.