From 9589cb2e0f10cc1ba4b3ba31f307a152954c018b Mon Sep 17 00:00:00 2001
From: Hincu Petru <hincupetru@gmail.com>
Date: Mon, 14 Apr 2014 09:46:34 +0000
Subject: [PATCH] disable poll voting for reshared post

---
 app/assets/javascripts/app/views/poll_view.js | 11 +++++++----
 app/assets/templates/poll_tpl.jst.hbs         |  7 +++++++
 app/models/reshare.rb                         |  4 ++++
 config/locales/javascript/javascript.en.yml   |  1 +
 spec/javascripts/app/views/poll_view_spec.js  | 17 +++++++++++++++++
 spec/models/reshare_spec.rb                   | 11 +++++++++++
 6 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/app/assets/javascripts/app/views/poll_view.js b/app/assets/javascripts/app/views/poll_view.js
index 1ec3d18698..38f943e33b 100644
--- a/app/assets/javascripts/app/views/poll_view.js
+++ b/app/assets/javascripts/app/views/poll_view.js
@@ -14,11 +14,14 @@ app.views.Poll = app.views.Base.extend({
 
   presenter: function(){
     var defaultPresenter = this.defaultPresenter();
-    var show_form = defaultPresenter.loggedIn && 
-                    !this.model.attributes.already_participated_in_poll;
+    var isResharePost = (this.model.get('post_type') == 'Reshare');
+    var show_form = defaultPresenter.loggedIn &&
+                    !isResharePost &&
+                    !this.model.get('already_participated_in_poll');
 
     return _.extend(defaultPresenter, {
-      show_form: show_form 
+      show_form: show_form,
+      is_reshare_post: isResharePost
     });
   },
 
@@ -97,7 +100,7 @@ app.views.Poll = app.views.Base.extend({
     var pollParticipation = new app.models.PollParticipation({
       poll_answer_id: answer_id,
       poll_id: this.poll.poll_id,
-      post_id: this.poll.post_id, 
+      post_id: this.poll.post_id,
     });
     var _this = this;
 
diff --git a/app/assets/templates/poll_tpl.jst.hbs b/app/assets/templates/poll_tpl.jst.hbs
index f9aad7f189..b55c923f84 100644
--- a/app/assets/templates/poll_tpl.jst.hbs
+++ b/app/assets/templates/poll_tpl.jst.hbs
@@ -37,6 +37,13 @@
           </div>
         {{/poll.poll_answers}}
       {{/if}}
+
+      {{#if is_reshare_post }}
+        <div class="poll_footer">
+          <a class="root_post_link" href="/posts/{{root.id}}">{{t "poll.vote_original_post" }}</a>
+        </div>
+      {{/if}}
+
     </div>
   </div>
 {{/if}}
diff --git a/app/models/reshare.rb b/app/models/reshare.rb
index 7e8fac45b5..c10cde1f00 100644
--- a/app/models/reshare.rb
+++ b/app/models/reshare.rb
@@ -49,6 +49,10 @@ class Reshare < Post
     absolute_root.try(:location).try(:address)
   end
 
+  def poll
+    absolute_root.try(:poll) || super
+  end
+
   def receive(recipient, sender)
     local_reshare = Reshare.where(:guid => self.guid).first
     if local_reshare && local_reshare.root.author_id == recipient.person.id
diff --git a/config/locales/javascript/javascript.en.yml b/config/locales/javascript/javascript.en.yml
index 03d7e53374..6cac42652c 100644
--- a/config/locales/javascript/javascript.en.yml
+++ b/config/locales/javascript/javascript.en.yml
@@ -227,6 +227,7 @@ en:
 
     poll:
       vote: "Vote"
+      vote_original_post: 'Vote original post'
       result: "Result"
       count:
         one: "1 vote so far"
diff --git a/spec/javascripts/app/views/poll_view_spec.js b/spec/javascripts/app/views/poll_view_spec.js
index 8f86d6eb38..288a938d30 100644
--- a/spec/javascripts/app/views/poll_view_spec.js
+++ b/spec/javascripts/app/views/poll_view_spec.js
@@ -44,6 +44,23 @@ describe("app.views.Poll", function(){
     });
   });
 
+  describe('reshared post', function(){
+    beforeEach(function(){
+      this.view.model.set('post_type', 'Reshare');
+      this.view.model.set('root', {id: 1});
+      this.view.render();
+    });
+
+    it('hide vote form', function(){
+      expect(this.view.$('form').length).toBe(0);
+    });
+
+    it("show a.root_post_link", function(){
+      var id = this.view.model.get('root').id;
+      expect(this.view.$('a.root_post_link').attr('href')).toBe('/posts/'+id);
+    });
+  });
+
   describe("vote form", function(){
     it('show vote form when user is logged in and not voted before', function(){
       expect(this.view.$('form').length).toBe(1);
diff --git a/spec/models/reshare_spec.rb b/spec/models/reshare_spec.rb
index ecc24c9de9..a18b0e4812 100644
--- a/spec/models/reshare_spec.rb
+++ b/spec/models/reshare_spec.rb
@@ -63,6 +63,17 @@ describe Reshare, :type => :model do
     end
   end
 
+  describe '#poll' do
+    before do
+      @root_post = FactoryGirl.create(:status_message_with_poll, public: true)
+      @reshare = FactoryGirl.create(:reshare, root: @root_post)
+    end
+
+    it 'contains root poll' do
+      @reshare.poll == @root_post.poll
+    end
+  end
+
   describe '#notification_type' do
     before do
       sm = FactoryGirl.build(:status_message, :author => alice.person, :public => true)
-- 
GitLab