Skip to content
Browse files

Brought ProjectedTable into the skip list / sort key fold

  • Loading branch information...
1 parent 14dc39a commit cfff9d1e80b67c7d52d6535d49e7d14d6860f465 @nathansobo committed Jan 3, 2011
View
6 lib/monarch/model/relations/relation.js
@@ -261,6 +261,10 @@ _.constructor("Monarch.Model.Relations.Relation", {
if (changeset && changeset[columnName] && changeset[columnName].column === column) {
sortKey[qualifiedColumnName] = changeset[columnName].oldValue;
} else {
+
+ var field = tuple.field(column);
+ if (!field) console.debug(column);
+
sortKey[qualifiedColumnName] = tuple.field(column).value();
}
});
@@ -374,8 +378,6 @@ _.constructor("Monarch.Model.Relations.Relation", {
}, this);
},
-
-
onOperandRemove: function(tuple, index, newKey, oldKey) {
if (this.findByKey(oldKey)) this.tupleRemovedRemotely(tuple, newKey, oldKey);
},
View
1 lib/monarch/model/relations/selection.js
@@ -61,7 +61,6 @@ _.constructor("Monarch.Model.Relations.Selection", Monarch.Model.Relations.Relat
// private
-
onOperandInsert: function(tuple, index, newKey, oldKey) {
if (this.predicate.evaluate(tuple)) this.tupleInsertedRemotely(tuple, newKey, oldKey);
},
View
63 lib/monarch/model/relations/table_projection.js
@@ -8,7 +8,7 @@ _.constructor("Monarch.Model.Relations.TableProjection", Monarch.Model.Relations
initialize: function(operand, projectedTable) {
this.operand = operand;
this.projectedTable = projectedTable;
- this.sortSpecifications = projectedTable.sortSpecifications; // this should actually be based on the operand, but we have to remove columns projected away
+ this.sortSpecifications = this.projectOperandSortSpecifications();
this.initializeEventsSystem();
},
@@ -37,39 +37,44 @@ _.constructor("Monarch.Model.Relations.TableProjection", Monarch.Model.Relations
// private
- subscribeToOperands: function() {
- this.operandsSubscriptionBundle.add(this.operand.onInsert(function(compositeTuple) {
- var tuple = compositeTuple.record(this.projectedTable);
- if (!this.contains(tuple)) this.tupleInsertedRemotely(tuple);
- }, this));
+ onOperandInsert: function(compositeTuple, index, newKey, oldKey) {
+ if (this.findByKey(oldKey)) return;
+ this.tupleInsertedRemotely(compositeTuple.record(this.projectedTable), this.projectSortKey(newKey), this.projectSortKey(oldKey));
+ },
- this.operandsSubscriptionBundle.add(this.operand.onUpdate(function(compositeTuple, changeset) {
- var updatedColumnInProjectedTable = _.detect(changeset, function(change) {
- return change.column.table == this.projectedTable;
- }, this);
- var record = compositeTuple.record(this.projectedTable);
+ onOperandUpdate: function(compositeTuple, changeset, newIndex, oldIndex, newKey, oldKey) {
+ if (this.lastChangeset === changeset) return;
+ if (!this.changesetPertainsToProjectedTable(changeset)) return;
+ this.lastChangeset = changeset;
+ this.tupleUpdatedRemotely(compositeTuple.record(this.projectedTable), changeset, this.projectSortKey(newKey), this.projectSortKey(oldKey));
+ },
- if (updatedColumnInProjectedTable && !this.duplicatesLastUpdateEvent(record, changeset)) {
- this.lastUpdateEvent = [record, changeset];
- this.tupleUpdatedRemotely(record, changeset);
- }
- }, this));
+ onOperandRemove: function(compositeTuple, index, newKey, oldKey) {
+ var tuple = compositeTuple.record(this.projectedTable);
+ if (this.operand.find({id: tuple.id()})) return;
+ this.tupleRemovedRemotely(tuple, this.projectSortKey(newKey), this.projectSortKey(oldKey));
+ },
- this.operandsSubscriptionBundle.add(this.operand.onRemove(function(compositeTuple) {
- var tuple = compositeTuple.record(this.projectedTable);
- if (!this.operand.find(this.projectedTable.column('id').eq(tuple.id()))) {
- this.tupleRemovedRemotely(tuple);
- }
- }, this));
+ changesetPertainsToProjectedTable: function(changeset) {
+ return _.detect(changeset, function(change) {
+ return change.column.table === this.projectedTable;
+ }, this);
+ },
+
+ projectOperandSortSpecifications: function() {
+ return _.filter(this.operand.sortSpecifications, function(sortSpec) {
+ return sortSpec.column.table === this.projectedTable;
+ }, this);
},
- duplicatesLastUpdateEvent: function(record, changeset) {
- if (!this.lastUpdateEvent) return false;
- var lastRecord = this.lastUpdateEvent[0];
- var lastChangset = this.lastUpdateEvent[1];
- if (lastRecord !== record) return false;
- if (!_(lastChangset).isEqual(changeset)) return false;
- return true;
+ projectSortKey: function(sortKey) {
+ var projectedSortKey = {};
+ _.each(sortKey, function(value, key) {
+ if (this.projectedTable.globalName === key.split(".")[0]) {
+ projectedSortKey[key] = value;
+ }
+ }, this);
+ return projectedSortKey;
}
});
View
61 spec/monarch/model/relations/table_projection_spec.js
@@ -5,16 +5,16 @@ Screw.Unit(function(c) { with(c) {
var projection, operand, user, blog1, blog2, post1, post2, post3;
useExampleDomainModel();
- before(function() {
+ init(function() {
user = User.createFromRemote({id: 'saltpeter', fullName: "Salt Peter"});
- blog1 = user.blogs().createFromRemote({id: 'blog1', name: "Blog 1"});
- blog2 = user.blogs().createFromRemote({id: 'blog2', name: "Blog 2"});
- post1 = blog1.blogPosts().createFromRemote({id: 'post1', body: "this is post 1"});
- post2 = blog1.blogPosts().createFromRemote({id: 'post2', body: "this is post 2"});
- post3 = blog2.blogPosts().createFromRemote({id: 'post3', body: "this is post 3"});
+ blog1 = user.blogs().createFromRemote({id: 1, name: "Blog 1", ownerId: 1});
+ blog2 = user.blogs().createFromRemote({id: 2, name: "Blog 2", ownerId: 2});
+ post1 = blog1.blogPosts().createFromRemote({id: 1, body: "this is post 1"});
+ post2 = blog1.blogPosts().createFromRemote({id: 2, body: "this is post 2"});
+ post3 = blog2.blogPosts().createFromRemote({id: 3, body: "this is post 3"});
operand = user.blogs().join(BlogPost).on(BlogPost.blogId.eq(Blog.id));
- projection = new Monarch.Model.Relations.TableProjection(operand, Blog.table);
+ projection = operand.project(Blog);
});
@@ -52,16 +52,18 @@ Screw.Unit(function(c) { with(c) {
describe("when a tuple is inserted into the operand", function() {
context("if the record corresponding to the projected table is NOT already present in the projection", function() {
- it("triggers onInsert callbacks with the record", function() {
- var blog = user.blogs().createFromRemote({id: 'bloggo'});
- var post = blog.blogPosts().createFromRemote();
+ it("triggers an insert event with the record", function() {
+ var blog = user.blogs().createFromRemote({id: 100});
+ var post = blog.blogPosts().createFromRemote({id: 1, blogId: 100});
+ var sortKey = projection.buildSortKey(blog);
+
expect(insertCallback).to(haveBeenCalled, once);
- expect(insertCallback).to(haveBeenCalled, withArgs(blog));
+ expect(insertCallback).to(haveBeenCalled, withArgs(blog, 2, sortKey, sortKey));
});
});
context("if the record corresponding to the projected table is already present in the projection", function() {
- it("does not trigger an on insert callback", function() {
+ it("does not trigger an insert evenrt", function() {
blog1.blogPosts().create();
expect(insertCallback).toNot(haveBeenCalled);
});
@@ -70,25 +72,21 @@ Screw.Unit(function(c) { with(c) {
describe("when a record is updated in the operand", function() {
context("if one of the updated columns is in the #projectedTable", function() {
- it("triggers onUpdate callbacks with the record's corresponding projected record and the changed columns", function() {
- var oldValue = blog2.name();
- var newValue = "I feeeel good!";
- blog2.update({name: newValue});
+ init(function() {
+ projection = Blog.orderBy('ownerId asc').joinTo(BlogPost).project(Blog);
+ });
+ it("triggers onUpdate callbacks with the record's corresponding projected record and the changed columns", function() {
+ blog1.update({ownerId: 100});
expect(updateCallback).to(haveBeenCalled, once);
- expect(updateCallback).to(haveBeenCalled, withArgs(blog2, {
- name: {
- column: Blog.name_,
- oldValue: oldValue,
- newValue: newValue
- },
- funProfitName: {
- column: Blog.funProfitName,
- oldValue: oldValue + " for Fun and Profit",
- newValue: newValue + " for Fun and Profit"
- }
- }));
+ expect(updateCallback).to(haveBeenCalled, withArgs(
+ blog1,
+ { ownerId: { column: Blog.ownerId, oldValue: 1, newValue: 100 } },
+ 1, 0, // new index, old index
+ { 'blogs.id': 1, 'blogs.owner_id': 100 },
+ { 'blogs.id': 1, 'blogs.owner_id': 1 }
+ ));
});
});
@@ -110,8 +108,9 @@ Screw.Unit(function(c) { with(c) {
describe("when a record is removed from the operand", function() {
context("if there are NO other tuples in the operand with the same record as their projected component", function() {
it("triggers onRemove callbacks with the removed record's corresponding projected record", function() {
+ var sortKey = projection.buildSortKey(blog2);
post3.destroy();
- expect(removeCallback).to(haveBeenCalled, withArgs(blog2));
+ expect(removeCallback).to(haveBeenCalled, withArgs(blog2, 1, sortKey, sortKey));
});
});
@@ -123,9 +122,5 @@ Screw.Unit(function(c) { with(c) {
});
});
});
-
- describe("subscription propagation", function() {
-
- });
});
}});

0 comments on commit cfff9d1

Please sign in to comment.
Something went wrong with that request. Please try again.