diff --git a/public/javascripts/app/collections/stream.js b/public/javascripts/app/collections/stream.js index 70fe1b5da..b16e137e1 100644 --- a/public/javascripts/app/collections/stream.js +++ b/public/javascripts/app/collections/stream.js @@ -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; diff --git a/public/javascripts/app/models/like.js b/public/javascripts/app/models/like.js index d18d96685..534317c8a 100644 --- a/public/javascripts/app/models/like.js +++ b/public/javascripts/app/models/like.js @@ -1,2 +1 @@ -app.models.Like = Backbone.Model.extend({ -}) +app.models.Like = Backbone.Model.extend({ }) diff --git a/public/javascripts/app/models/post.js b/public/javascripts/app/models/post.js index f5d707bc4..4cabdaa1b 100644 --- a/public/javascripts/app/models/post.js +++ b/public/javascripts/app/models/post.js @@ -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() }); } }); diff --git a/public/javascripts/app/models/reshare.js b/public/javascripts/app/models/reshare.js index a88f62538..c1c5105c8 100644 --- a/public/javascripts/app/models/reshare.js +++ b/public/javascripts/app/models/reshare.js @@ -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 + return this._rootPost + }, + + reshare : function(){ + this.rootPost().reshare() + }, + + reshareAuthor : function(){ + return this.rootPost().reshareAuthor() } }); diff --git a/public/javascripts/app/models/status_message.js b/public/javascripts/app/models/status_message.js index fb73daad4..7a2c8120a 100644 --- a/public/javascripts/app/models/status_message.js +++ b/public/javascripts/app/models/status_message.js @@ -1,3 +1 @@ -app.models.StatusMessage = app.models.Post.extend({ - url : function() { return "/status_messages"; } -}); +app.models.StatusMessage = app.models.Post.extend({ }); diff --git a/public/javascripts/app/views/feedback_view.js b/public/javascripts/app/views/feedback_view.js index c45dbdbf8..68dc013a1 100644 --- a/public/javascripts/app/views/feedback_view.js +++ b/public/javascripts/app/views/feedback_view.js @@ -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); + } + }); } }) diff --git a/public/javascripts/app/views/post_view.js b/public/javascripts/app/views/post_view.js index 0fb51bb57..adb46d6b8 100644 --- a/public/javascripts/app/views/post_view.js +++ b/public/javascripts/app/views/post_view.js @@ -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(){ diff --git a/public/javascripts/app/views/reshare_feedback_view.js b/public/javascripts/app/views/reshare_feedback_view.js deleted file mode 100644 index 3c89e5f2d..000000000 --- a/public/javascripts/app/views/reshare_feedback_view.js +++ /dev/null @@ -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(); - } -}); diff --git a/spec/javascripts/app/models/post_spec.js b/spec/javascripts/app/models/post_spec.js index d0c3c5191..ed5aede9f 100644 --- a/spec/javascripts/app/models/post_spec.js +++ b/spec/javascripts/app/models/post_spec.js @@ -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") - }) - }) }); diff --git a/spec/javascripts/app/models/reshare_spec.js b/spec/javascripts/app/models/reshare_spec.js index 5181f8f8f..5f61f0ad7 100644 --- a/spec/javascripts/app/models/reshare_spec.js +++ b/spec/javascripts/app/models/reshare_spec.js @@ -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"}}) - }); + 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() + }) + }) }); diff --git a/spec/javascripts/app/views/feedback_view_spec.js b/spec/javascripts/app/views/feedback_view_spec.js index 8f060bc54..070b0822c 100644 --- a/spec/javascripts/app/views/feedback_view_spec.js +++ b/spec/javascripts/app/views/feedback_view_spec.js @@ -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(); }) }) }) diff --git a/spec/javascripts/app/views/post_view_spec.js b/spec/javascripts/app/views/post_view_spec.js index d775005b4..790913afd 100644 --- a/spec/javascripts/app/views/post_view_spec.js +++ b/spec/javascripts/app/views/post_view_spec.js @@ -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(); diff --git a/spec/javascripts/app/views/reshare_feedback_view_spec.js b/spec/javascripts/app/views/reshare_feedback_view_spec.js deleted file mode 100644 index 5bcbc0fb2..000000000 --- a/spec/javascripts/app/views/reshare_feedback_view_spec.js +++ /dev/null @@ -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(); - }) - -})