diff --git a/Changelog.md b/Changelog.md
index dac53baf4..b3dd6ac19 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -15,6 +15,7 @@
## Features
* Ask for confirmation when leaving a submittable comment field [#7530](https://github.com/diaspora/diaspora/pull/7530)
+* Show users vote in polls [#7550](https://github.com/diaspora/diaspora/pull/7550)
# 0.7.0.0
diff --git a/app/assets/javascripts/app/views/poll_view.js b/app/assets/javascripts/app/views/poll_view.js
index 751e47538..275fb02d6 100644
--- a/app/assets/javascripts/app/views/poll_view.js
+++ b/app/assets/javascripts/app/views/poll_view.js
@@ -17,15 +17,22 @@ app.views.Poll = app.views.Base.extend({
var isReshare = (this.model.get('post_type') === 'Reshare');
var showForm = defaultPresenter.loggedIn &&
!isReshare &&
- !this.model.get('already_participated_in_poll');
+ !this.model.get("poll_participation_answer_id");
var originalPostLink = isReshare && this.model.get('root') ?
'' + Diaspora.I18n.t('poll.original_post') + '' :
'';
+ var answerGiven = this.model.get("poll_participation_answer_id");
+
+ if (defaultPresenter.poll && defaultPresenter.poll.poll_answers) {
+ defaultPresenter.poll.poll_answers.forEach(function(answer) {
+ _.extend(answer, {isCurrentUserVote: answerGiven ? answer.id === answerGiven : false});
+ });
+ }
return _.extend(defaultPresenter, {
show_form: showForm,
is_reshare: isReshare,
- original_post_link: originalPostLink
+ originalPostLink: originalPostLink
});
},
diff --git a/app/assets/stylesheets/color_themes/_color_theme_override.scss b/app/assets/stylesheets/color_themes/_color_theme_override.scss
index 7655d08f9..37ec7066b 100644
--- a/app/assets/stylesheets/color_themes/_color_theme_override.scss
+++ b/app/assets/stylesheets/color_themes/_color_theme_override.scss
@@ -6,7 +6,5 @@ body {
.left-navbar .hoverable:hover { background-color: $main-color-essence; }
- .poll_form .progress .bar { background-color: $main-color-dark; }
-
.badge { background-color: $brand-primary; }
}
diff --git a/app/assets/stylesheets/color_themes/_color_theme_override_dark.scss b/app/assets/stylesheets/color_themes/_color_theme_override_dark.scss
index ec5e4ef46..a09028a3e 100644
--- a/app/assets/stylesheets/color_themes/_color_theme_override_dark.scss
+++ b/app/assets/stylesheets/color_themes/_color_theme_override_dark.scss
@@ -22,6 +22,7 @@ body {
.poll_form .progress {
background-color: $gray-dark;
.bar { background-color: $gray-light; }
+ .users-vote { background-color: $brand-primary; }
}
.stream-element .collapsible {
diff --git a/app/assets/stylesheets/poll.scss b/app/assets/stylesheets/poll.scss
index 21a5e8402..5bd220a98 100644
--- a/app/assets/stylesheets/poll.scss
+++ b/app/assets/stylesheets/poll.scss
@@ -26,6 +26,10 @@
height: 100%;
text-align: left;
}
+
+ .users-vote {
+ background-color: $brand-primary;
+ }
}
.submit[disabled] {
diff --git a/app/assets/templates/poll_tpl.jst.hbs b/app/assets/templates/poll_tpl.jst.hbs
index a1a9be576..2a8b221c2 100644
--- a/app/assets/templates/poll_tpl.jst.hbs
+++ b/app/assets/templates/poll_tpl.jst.hbs
@@ -13,13 +13,24 @@
{{#poll.poll_answers}}
-
+ {{#if isCurrentUserVote}}
+
+ {{else}}
+
+ {{/if}}
({{t "poll.answer_count" count=vote_count}})
-
+ {{#if isCurrentUserVote}}
+
+ {{else}}
+
+ {{/if}}
{{/poll.poll_answers}}
@@ -38,7 +49,7 @@
{{#if is_reshare }}
{{/if}}
diff --git a/app/models/poll.rb b/app/models/poll.rb
index 15589d601..05f8635d1 100644
--- a/app/models/poll.rb
+++ b/app/models/poll.rb
@@ -21,19 +21,19 @@ class Poll < ApplicationRecord
def as_json(options={})
{
- :poll_id => self.id,
- :post_id => self.status_message.id,
- :question => self.question,
- :poll_answers => self.poll_answers,
- :participation_count => self.participation_count,
+ poll_id: id,
+ post_id: status_message.id,
+ question: question,
+ poll_answers: poll_answers,
+ participation_count: participation_count
}
end
+ def participation_answer(user)
+ poll_participations.find_by(author_id: user.person.id)
+ end
+
def participation_count
poll_answers.sum("vote_count")
end
-
- def already_participated?(user)
- poll_participations.where(:author_id => user.person.id).present?
- end
end
diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb
index a1187fba9..7ed18afd3 100644
--- a/app/presenters/post_presenter.rb
+++ b/app/presenters/post_presenter.rb
@@ -67,7 +67,7 @@ class PostPresenter < BasePresenter
title: title,
location: @post.post_location,
poll: @post.poll,
- already_participated_in_poll: already_participated_in_poll,
+ poll_participation_answer_id: poll_participation_answer_id,
participation: participate?,
interactions: build_interactions_json
}
@@ -121,10 +121,8 @@ class PostPresenter < BasePresenter
@post.reshare_for(current_user).try(:as_api_response, :backbone)
end
- def already_participated_in_poll
- if @post.poll && user_signed_in?
- @post.poll.already_participated?(current_user)
- end
+ def poll_participation_answer_id
+ @post.poll&.participation_answer(current_user)&.poll_answer_id if user_signed_in?
end
def participate?
diff --git a/config/locales/javascript/javascript.en.yml b/config/locales/javascript/javascript.en.yml
index 8f4ed6953..e89cf6109 100644
--- a/config/locales/javascript/javascript.en.yml
+++ b/config/locales/javascript/javascript.en.yml
@@ -344,3 +344,4 @@ en:
other: "<%=count%> votes"
show_result: "Show result"
close_result: "Hide result"
+ your_vote: "Your vote"
diff --git a/spec/javascripts/app/views/poll_view_spec.js b/spec/javascripts/app/views/poll_view_spec.js
index 93d685332..034b0a5eb 100644
--- a/spec/javascripts/app/views/poll_view_spec.js
+++ b/spec/javascripts/app/views/poll_view_spec.js
@@ -65,7 +65,7 @@ describe("app.views.Poll", function(){
expect(this.view.$('form').length).toBe(1);
});
it("hides vote form when user voted before", function(){
- this.view.model.attributes.already_participated_in_poll = true;
+ this.view.model.set("poll_participation_answer_id", this.view.poll.poll_answers[0].id);
this.view.render();
expect(this.view.$('form').length).toBe(0);
});
@@ -75,4 +75,25 @@ describe("app.views.Poll", function(){
expect(this.view.$('form').length).toBe(0);
});
});
+
+ describe("answer given", function() {
+ it("adds 'users-vote' class to progress bar for the option the user voted for", function() {
+ var answer = this.view.poll.poll_answers[0];
+ this.view.model.set("poll_participation_answer_id", answer.id);
+ expect(this.view.$(".poll_progress_bar.users-vote").length).toBe(1);
+ });
+
+ it("doesn't add 'users-vote' class to progress bar of the options the user didn't vote for", function() {
+ var answer1 = this.view.poll.poll_answers[0];
+ var answer2 = this.view.poll.poll_answers[1];
+ this.view.model.set("poll_participation_answer_id", answer1.id);
+ expect(this.view.$(".poll_progress_bar[data-answerid='" + answer2.id + "']").hasClass("users-vote")).toBe(false);
+ });
+
+ it("adds label next to the answer the user voted for", function() {
+ var answer = this.view.poll.poll_answers[0];
+ this.view.model.set("poll_participation_answer_id", answer.id);
+ expect(this.view.$(".label.label-primary").length).toBe(1);
+ });
+ });
});
diff --git a/spec/javascripts/jasmine_helpers/factory.js b/spec/javascripts/jasmine_helpers/factory.js
index ba97e5b71..217c68952 100644
--- a/spec/javascripts/jasmine_helpers/factory.js
+++ b/spec/javascripts/jasmine_helpers/factory.js
@@ -211,9 +211,9 @@ var factory = {
},
postWithPoll : function(overrides) {
- var defaultAttrs = _.extend(factory.postAttrs(), {"author" : this.author()});
- defaultAttrs = _.extend(defaultAttrs, {"already_participated_in_poll" : false});
- defaultAttrs = _.extend(defaultAttrs, {"poll" : factory.poll()});
+ var defaultAttrs = _.extend(factory.postAttrs(), {"author": this.author()});
+ defaultAttrs = _.extend(defaultAttrs, {"poll_participation_answer_id": null});
+ defaultAttrs = _.extend(defaultAttrs, {"poll": factory.poll()});
return new app.models.Post(_.extend(defaultAttrs, overrides));
},
diff --git a/spec/models/poll_spec.rb b/spec/models/poll_spec.rb
index f18eb4fab..8a8add824 100644
--- a/spec/models/poll_spec.rb
+++ b/spec/models/poll_spec.rb
@@ -20,4 +20,23 @@ describe Poll, type: :model do
expect(poll.errors).to have_key(:question)
end
end
+
+ describe "poll_participation" do
+ it "should return the answer object after a user voted in a poll" do
+ answer = poll.poll_answers.build(answer: "1")
+ answer.poll = poll
+ poll.poll_answers.build(answer: "2").poll = poll
+ poll.save
+ participation = poll.poll_participations.create(poll_answer: answer, author: alice.person)
+ expect(poll.participation_answer(alice)).to eql(participation)
+ end
+
+ it "should return nil if a user did not participate in a poll" do
+ answer = poll.poll_answers.build(answer: "1")
+ answer.poll = poll
+ poll.poll_answers.build(answer: "2").poll = poll
+ poll.save
+ expect(poll.participation_answer(alice)).to eql(nil)
+ end
+ end
end
diff --git a/spec/presenters/post_presenter_spec.rb b/spec/presenters/post_presenter_spec.rb
index 4110cedeb..d3bbd6cd0 100644
--- a/spec/presenters/post_presenter_spec.rb
+++ b/spec/presenters/post_presenter_spec.rb
@@ -135,6 +135,19 @@ describe PostPresenter do
presenter = PostPresenter.new(status_message_with_poll)
expect(presenter.as_json).to be_a(Hash)
end
+
+ it "returns the answer id of the current user's poll participation" do
+ presenter = PostPresenter.new(status_message_with_poll, alice)
+ poll_answer = status_message_with_poll.poll.poll_answers.first
+ poll_participation = status_message_with_poll.poll.poll_participations
+ poll_participation = poll_participation.create(poll_answer: poll_answer, author: alice.person)
+ expect(presenter.as_json[:poll_participation_answer_id]).to eql(poll_participation.poll_answer_id)
+ end
+
+ it "returns nil if the user did not participate in a poll" do
+ presenter = PostPresenter.new(status_message_with_poll, alice)
+ expect(presenter.as_json[:poll_participation_answer_id]).to eql(nil)
+ end
end
describe "#tags" do