You can now like "reshares", stream is polymorphic

This commit is contained in:
Dennis Collinson 2012-01-17 17:22:44 -08:00
parent 86c8f99188
commit 9cde06a628
13 changed files with 51 additions and 128 deletions

View file

@ -9,7 +9,10 @@ app.collections.Stream = Backbone.Collection.extend({
return path;
},
model: app.models.Post,
model: function(attrs, options) {
var modelClass = app.models[attrs.post_type] || app.models.Post
return new modelClass(attrs, options);
},
parse: function(resp){
return resp.posts;

View file

@ -1,2 +1 @@
app.models.Like = Backbone.Model.extend({
})
app.models.Like = Backbone.Model.extend({ })

View file

@ -15,14 +15,13 @@ app.models.Post = Backbone.Model.extend({
}
},
reshareUrl : "reshares/",
reshare : function(){
var reshare = new app.models.Reshare();
reshare.save({root_guid : this.baseGuid()}, {
success : function(){
app.stream.collection.add(reshare.toJSON());
}
});
return reshare;
return this._reshare = this._reshare || new app.models.Reshare({root_guid : this.get("guid")});
},
reshareAuthor : function(){
return this.get("author")
},
toggleLike : function() {
@ -35,23 +34,16 @@ app.models.Post = Backbone.Model.extend({
},
createdAt : function() {
return +new Date(this.get("created_at")) / 1000;
return new Date(this.get("created_at")) / 1000;
},
baseGuid : function() {
if(this.get("root")){
return this.get("root").guid;
} else {
return this.get("guid");
}
likeUrl : function(){
return this.url() + "/likes"
},
baseAuthor : function() {
if(this.get("root")){
return this.get("root").author;
} else {
return this.get("author");
}
like : function() {
this.set({ user_like : this.likes.create({}, {url : this.likeUrl()}) });
},
unlike : function() {
@ -60,9 +52,5 @@ app.models.Post = Backbone.Model.extend({
likeModel.destroy();
this.set({ user_like : null });
},
like : function() {
this.set({ user_like : this.likes.create() });
}
});

View file

@ -1,8 +1,14 @@
app.models.Reshare = app.models.Post.extend({
url : function() { return "/reshares"; },
rootPost : function(){
this._rootPost = this._rootPost || new app.models.Post(this.get("root"))
return this._rootPost
},
reshare : function(){
this.rootPost().reshare()
},
reshareAuthor : function(){
return this.rootPost().reshareAuthor()
}
});

View file

@ -1,3 +1 @@
app.models.StatusMessage = app.models.Post.extend({
url : function() { return "/status_messages"; }
});
app.models.StatusMessage = app.models.Post.extend({ });

View file

@ -13,14 +13,15 @@ app.views.Feedback = app.views.StreamObject.extend({
this.model.toggleLike();
},
initialize: function(options){
this.setupRenderEvents();
this.reshareablePost = options.model;
},
resharePost : function(evt){
if(evt) { evt.preventDefault(); }
if(!window.confirm("Reshare " + this.reshareablePost.baseAuthor().name + "'s post?")) { return }
this.reshareablePost.reshare();
if(!window.confirm("Reshare " + this.model.reshareAuthor().name + "'s post?")) { return }
var reshare = this.model.reshare()
reshare.save({}, {
url: this.model.reshareUrl,
success : function(){
app.stream.collection.add(reshare);
}
});
}
})

View file

@ -35,12 +35,7 @@ app.views.Post = app.views.StreamObject.extend({
feedbackView : function(){
if(!window.app.user().current_user ) { return null }
var feedbackViewClass = this.resharedContent() ? app.views.ReshareFeedback : app.views.Feedback
return new feedbackViewClass({model : this.model});
},
resharedContent : function(){
return this.model.get('root')
return new app.views.Feedback({model : this.model});
},
postContentView: function(){

View file

@ -1,6 +0,0 @@
app.views.ReshareFeedback = app.views.Feedback.extend({
initialize : function(){
this.reshareablePost = (this.model instanceof app.models.Reshare) ? this.model.rootPost() : new app.models.Reshare(this.model.attributes).rootPost();
this.setupRenderEvents();
}
});

View file

@ -13,22 +13,6 @@ describe("app.models.Post", function() {
});
});
describe("baseGuid", function(){
it("returns the post's guid if the post does not have a root", function() {
this.post.attributes.root = null;
this.post.attributes.guid = "abcd";
expect(this.post.baseGuid()).toBe("abcd")
})
it("returns the post's root guid if the post has a root", function() {
this.post.attributes.root = {guid : "1234"}
this.post.attributes.guid = "abcd";
expect(this.post.baseGuid()).toBe("1234")
})
})
describe("toggleLike", function(){
it("calls unliked when the user_like exists", function(){
this.post.set({user_like : "123"});
@ -67,20 +51,4 @@ describe("app.models.Post", function() {
expect(app.models.Like.prototype.destroy).toHaveBeenCalled();
})
})
describe("baseAuthor", function(){
it("returns the post's guid if the post does not have a root", function() {
this.post.attributes.root = null;
this.post.attributes.author = "abcd";
expect(this.post.baseAuthor()).toBe("abcd")
})
it("returns the post's root guid if the post has a root", function() {
this.post.attributes.root = {author : "1234"}
this.post.attributes.author = "abcd";
expect(this.post.baseAuthor()).toBe("1234")
})
})
});

View file

@ -1,9 +1,9 @@
describe("app.models.Reshare", function(){
describe("rootPost", function(){
beforeEach(function(){
this.reshare = new app.models.Reshare({root: {a:"namaste", be : "aloha", see : "community"}})
});
describe("rootPost", function(){
it("should be the root attrs", function(){
expect(this.reshare.rootPost().get("be")).toBe("aloha")
});
@ -16,5 +16,13 @@ describe("app.models.Reshare", function(){
expect(this.reshare.rootPost()).toBe(this.reshare.rootPost())
});
});
describe(".reshare", function(){
it("reshares the root post", function(){
spyOn(this.reshare.rootPost(), "reshare")
this.reshare.reshare()
expect(this.reshare.rootPost().reshare).toHaveBeenCalled()
})
})
});

View file

@ -15,11 +15,6 @@ describe("app.views.Feedback", function(){
this.view = new app.views.Feedback({model: this.post});
});
describe("initialization", function(){
it("sets the model as the reshareable post", function(){
expect(this.view.reshareablePost).toBe(this.post);
})
})
describe(".render", function(){
beforeEach(function(){
@ -130,11 +125,11 @@ describe("app.views.Feedback", function(){
expect(window.confirm).toHaveBeenCalled();
})
it("reshares the reshareablePost", function(){
it("reshares the model", function(){
spyOn(window, "confirm").andReturn(true);
spyOn(this.view.reshareablePost, "reshare")
spyOn(this.view.model.reshare(), "save")
this.view.$(".reshare_action").first().click();
expect(this.view.reshareablePost.reshare).toHaveBeenCalled();
expect(this.view.model.reshare().save).toHaveBeenCalled();
})
})
})

View file

@ -18,14 +18,6 @@ describe("app.views.Post", function(){
this.reshare = this.collection.models[1];
})
context("for a reshare", function(){
it("should display ReshareFeedback", function(){
spyOn(app.views, "ReshareFeedback").andReturn(stubView("these are special reshare actions"));
var view = new app.views.Post({model : this.reshare}).render();
expect(view.$(".feedback").text().trim()).toBe("these are special reshare actions");
})
})
it("displays a reshare count", function(){
this.statusMessage.set({reshares_count : 2})
var view = new app.views.Post({model : this.statusMessage}).render();

View file

@ -1,24 +0,0 @@
describe("app.views.ReshareFeedback", function(){
beforeEach(function(){
var posts = $.parseJSON(spec.readFixture("multi_stream_json"))["posts"];
this.reshare = new app.models.Reshare(_.extend(posts[1], {public : true}));
this.view = new app.views.ReshareFeedback({model : this.reshare }).render()
})
it("inherits from feedback view", function(){
expect(this.view instanceof app.views.Feedback).toBeTruthy()
})
it("sets Up the root Post as the reshareable post", function(){
expect(this.view.reshareablePost).toBe(this.reshare.rootPost())
})
it("reshares the rootPost", function(){
spyOn(window, "confirm").andReturn(true);
spyOn(this.reshare.rootPost(), "reshare")
console.log(this.view.el)
this.view.$(".reshare_action").first().click();
expect(this.reshare.rootPost().reshare).toHaveBeenCalled();
})
})