From 02ca7a32f08f7dd203d1feee302cbb6fa762f606 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Tue, 27 Dec 2011 15:17:02 -0500 Subject: [PATCH] added reshare logic back (doesn't append it to the stream...) --- app/controllers/reshares_controller.rb | 5 +- app/views/templates/feedback.ujs | 2 +- config/assets.yml | 1 - public/javascripts/app/models/post.js | 14 +++-- public/javascripts/app/models/reshare.js | 3 ++ public/javascripts/app/views/feedback_view.js | 11 ++++ spec/javascripts/app/models/post-spec.js | 27 ++++++++-- .../app/views/feedback_view_spec.js | 54 +++++++++++++++++++ 8 files changed, 107 insertions(+), 10 deletions(-) create mode 100644 public/javascripts/app/models/reshare.js diff --git a/app/controllers/reshares_controller.rb b/app/controllers/reshares_controller.rb index 3e5d5a73b..ff19b8a7f 100644 --- a/app/controllers/reshares_controller.rb +++ b/app/controllers/reshares_controller.rb @@ -9,6 +9,9 @@ class ResharesController < ApplicationController current_user.dispatch_post(@reshare, :url => post_url(@reshare), :additional_subscribers => @reshare.root.author) end - respond_with @reshare + respond_to do |format| + format.html { respond_with @reshare } + format.json{ render :json => @reshare.as_api_response(:backbone), :status => 201 } + end end end diff --git a/app/views/templates/feedback.ujs b/app/views/templates/feedback.ujs index 970723d30..f3ddd4f0b 100644 --- a/app/views/templates/feedback.ujs +++ b/app/views/templates/feedback.ujs @@ -4,7 +4,7 @@ ยท - <% if(public) { %> + <% if(public && author.id != current_user.id) { %> Reshare diff --git a/config/assets.yml b/config/assets.yml index e8dc44fc5..5baa8f0db 100644 --- a/config/assets.yml +++ b/config/assets.yml @@ -47,7 +47,6 @@ javascripts: - public/javascripts/view.js - public/javascripts/stream.js - - public/javascripts/content-updater.js - public/javascripts/aspects-dropdown.js - public/javascripts/contact-edit.js - public/javascripts/contact-list.js diff --git a/public/javascripts/app/models/post.js b/public/javascripts/app/models/post.js index 6288503f2..c4e43ada1 100644 --- a/public/javascripts/app/models/post.js +++ b/public/javascripts/app/models/post.js @@ -1,5 +1,5 @@ app.models.Post = Backbone.Model.extend({ - initialize: function() { + initialize : function() { this.comments = new app.collections.Comments(this.get("last_three_comments")); this.comments.url = this.url() + '/comments'; @@ -7,7 +7,7 @@ app.models.Post = Backbone.Model.extend({ this.likes.url = this.url() + '/likes'; }, - url: function(){ + url : function() { if(this.id) { return "/posts/" + this.id; } else { @@ -15,7 +15,15 @@ app.models.Post = Backbone.Model.extend({ } }, - createdAt: function(){ + createdAt : function() { return +new Date(this.get("created_at")) / 1000; + }, + + rootGuid : function() { + if(this.get("root")){ + return this.get("root").guid; + } else { + return this.get("guid"); + } } }); diff --git a/public/javascripts/app/models/reshare.js b/public/javascripts/app/models/reshare.js new file mode 100644 index 000000000..bf09bf17c --- /dev/null +++ b/public/javascripts/app/models/reshare.js @@ -0,0 +1,3 @@ +app.models.Reshare = Backbone.Model.extend({ + urlRoot: "/reshares" +}); diff --git a/public/javascripts/app/views/feedback_view.js b/public/javascripts/app/views/feedback_view.js index 929e55e11..9b7bd5b6d 100644 --- a/public/javascripts/app/views/feedback_view.js +++ b/public/javascripts/app/views/feedback_view.js @@ -3,6 +3,7 @@ app.views.Feedback = app.views.StreamObject.extend({ events: { "click .like_action": "toggleLike", + "click .reshare_action": "resharePost" }, initialize : function() { @@ -31,4 +32,14 @@ app.views.Feedback = app.views.StreamObject.extend({ this.like = this.model.likes.create(); } }, + + resharePost : function(evt){ + if(evt) { evt.preventDefault(); } + + if(window.confirm("Reshare " + this.model.get("author").name + "'s post?")) { + var reshare = new app.models.Reshare({root_guid : this.model.rootGuid()}); + reshare.save(); + return reshare; + } + } }) diff --git a/spec/javascripts/app/models/post-spec.js b/spec/javascripts/app/models/post-spec.js index a9b32f1cf..7991e4b23 100644 --- a/spec/javascripts/app/models/post-spec.js +++ b/spec/javascripts/app/models/post-spec.js @@ -1,12 +1,31 @@ describe("app.models.Post", function() { + beforeEach(function(){ + this.post = new app.models.Post(); + }) + describe("createdAt", function() { - var post = new app.models.Post(); it("returns the post's created_at as an integer", function() { var date = new Date; - post.set({ created_at: +date * 1000 }); + this.post.set({ created_at: +date * 1000 }); - expect(typeof post.createdAt()).toEqual("number"); - expect(post.createdAt()).toEqual(+date); + expect(typeof this.post.createdAt()).toEqual("number"); + expect(this.post.createdAt()).toEqual(+date); }); }); + + describe("rootGuid", 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.rootGuid()).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.rootGuid()).toBe("1234") + }) + }) }); diff --git a/spec/javascripts/app/views/feedback_view_spec.js b/spec/javascripts/app/views/feedback_view_spec.js index f3730c969..7521e2653 100644 --- a/spec/javascripts/app/views/feedback_view_spec.js +++ b/spec/javascripts/app/views/feedback_view_spec.js @@ -90,6 +90,60 @@ describe("app.views.Feedback", function(){ expect(this.link().text()).toContain('Like'); }) }) + + context("when the post is public", function(){ + beforeEach(function(){ + this.post.attributes.public = true + this.view.render(); + }) + + it("shows a reshare_action link", function(){ + expect($(this.view.el).html()).toContain('reshare_action') + }); + }) + + context("when the post is not public", function(){ + beforeEach(function(){ + this.post.attributes.public = false + this.view.render(); + }) + + it("shows a reshare_action link", function(){ + expect($(this.view.el).html()).not.toContain('reshare_action') + }); + }) + + context("when the current user owns the post", function(){ + beforeEach(function(){ + this.post.attributes.author = window.current_user + this.post.attributes.public = true + this.view.render(); + }) + + it("does not display a reshare_action link", function(){ + expect($(this.view.el).html()).not.toContain('reshare_action') + }) + }) + + context("reshares", function(){ + beforeEach(function(){ + this.post.attributes.public = true + this.view.render(); + }) + + it("displays a confirmation dialog", function(){ + spyOn(window, "confirm") + + this.view.$(".reshare_action").first().click(); + expect(window.confirm).toHaveBeenCalled(); + }) + + it("creates a reshare if the confirmation dialog is accepted", function(){ + spyOn(window, "confirm").andReturn(true); + + expect(this.view.resharePost().constructor).toBe(app.models.Reshare); + }) + }) }) })