Permalink
Browse files

Ordering now works with the new sortKeys

  • Loading branch information...
nathansobo committed Jan 4, 2011
1 parent c232e60 commit 5140e57b8a181b1b4cbaeadc5d86919e49e87638
Showing with 56 additions and 70 deletions.
  1. +3 −2 lib/monarch/model/column.js
  2. +11 −46 lib/monarch/model/relations/ordering.js
  3. +42 −22 spec/monarch/model/relations/ordering_spec.js
@@ -4,7 +4,8 @@ _.constructor("Monarch.Model.Column", {
initialize: function(table, name, type) {
this.table = table;
this.name = name;
- this.qualifiedName = table.globalName + "." + name;
+ this.globalName = _.underscore(name);
+ this.qualifiedName = table.globalName + "." + this.globalName;
this.type = type;
},
@@ -44,7 +45,7 @@ _.constructor("Monarch.Model.Column", {
return {
type: "column",
table: this.table.globalName,
- name: _.underscore(this.name)
+ name: this.globalName
};
},
@@ -13,7 +13,11 @@ _.constructor("Monarch.Model.Relations.Ordering", Monarch.Model.Relations.Relati
tuples: function() {
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) {
@@ -38,55 +42,16 @@ _.constructor("Monarch.Model.Relations.Ordering", Monarch.Model.Relations.Relati
// private
- tupleInsertedRemotely: function(tuple) {
- var position = this.storedTuples.insert(this.buildSortKey(tuple), 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);
+ onOperandInsert: function(tuple) {
+ this.tupleInsertedRemotely(tuple);
},
- tupleRemovedRemotely: function(tuple) {
- var position = this.storedTuples.remove(this.buildSortKey(tuple));
- this.onRemoveNode.publish(tuple, position);
+ onOperandUpdate: function(tuple, changset) {
+ this.tupleUpdatedRemotely(tuple, changset);
},
- sortingOnColumn: function(column) {
- return _.detect(this.sortSpecifications, function(sortSpecification) {
- 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')));
+ onOperandRemove: function(tuple) {
+ this.tupleRemovedRemotely(tuple);
}
})
@@ -15,6 +15,8 @@ Screw.Unit(function(c) { with(c) {
ordering = new Monarch.Model.Relations.Ordering(operand, sortSpecifications);
// 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"});
user1 = User.createFromRemote({id: 1, age: 1, fullName: "A"});
user3 = User.createFromRemote({id: 3, age: 2, fullName: "C"});
@@ -58,49 +60,67 @@ Screw.Unit(function(c) { with(c) {
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() {
var record = User.createFromRemote({id: 5, age: 2, fullName: "D"});
- expect(insertCallback).to(haveBeenCalled, withArgs(record, 3));
- expect(ordering.storedTuples.at(3)).to(eq, record);
+ var sortKey = ordering.buildSortKey(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() {
- it("triggers #onUpdate callbacks with the updated tuple and its new and old index in the ordering", function() {
- var tuplesLengthBefore = ordering.storedTuples.length;
+ describe("when a tuple is updated in the operand", function() {
+ it("triggers update events with the updated tuple and its new and old index", function() {
+ var sizeBefore = ordering.size();
user3.update({age: 20000});
- expect(updateCallback).to(haveBeenCalled);
- var args = updateCallback.mostRecentArgs;
- expect(args[0]).to(eq, user3);
- expect(args[2]).to(eq, 3); // new index
- expect(args[3]).to(eq, 2); // old index
+
+ expect(updateCallback).to(haveBeenCalled, withArgs(
+ user3, // record
+ { age: { column: User.age, oldValue: 2, newValue: 20000 } }, // changeset
+ 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();
user3.update({age: 0});
- expect(updateCallback).to(haveBeenCalled);
- var args = updateCallback.mostRecentArgs;
- expect(args[0]).to(eq, user3);
- expect(args[2]).to(eq, 0); // new index
- expect(args[3]).to(eq, 3); // old index
+ expect(updateCallback).to(haveBeenCalled, withArgs(
+ user3, // record
+ { age: { column: User.age, oldValue: 20000, newValue: 0 } }, // changeset
+ 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.storedTuples.at(0)).to(eq, user3);
+ expect(ordering.size()).to(eq, sizeBefore);
+ expect(ordering.at(0)).to(eq, user3);
updateCallback.clear();
+
+
+ var oldDate = user3.signedUpAt();
+ var newDate = new Date();
+ var sortKey = ordering.buildSortKey(user3);
+
user3.update({signedUpAt: new Date()});
- expect(updateCallback).to(haveBeenCalled);
- expect(updateCallback.mostRecentArgs[2]).to(eq, 0);
- expect(updateCallback.mostRecentArgs[3]).to(eq, 0);
+
+ expect(updateCallback).to(haveBeenCalled, withArgs(
+ 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() {
it("triggers #onRemove callbacks with the removed tuple and its former index", function() {
+ var sortKey = ordering.buildSortKey(user2);
user2.remotelyDestroyed();
- expect(removeCallback).to(haveBeenCalled, withArgs(user2, 1));
+ expect(removeCallback).to(haveBeenCalled, withArgs(user2, 1, sortKey, sortKey));
});
});
});

0 comments on commit 5140e57

Please sign in to comment.