Unverified Commit b556ad52 authored by Rete2's avatar Rete2 Committed by Benjamin Neff

show users vote in poll

closes #7550
parent e08440d7
......@@ -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
......
......@@ -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') ?
'<a href="/posts/' + this.model.get('root').id + '" class="root_post_link">' + Diaspora.I18n.t('poll.original_post') + '</a>' :
'';
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
});
},
......
......@@ -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; }
}
......@@ -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 {
......
......@@ -26,6 +26,10 @@
height: 100%;
text-align: left;
}
.users-vote {
background-color: $brand-primary;
}
}
.submit[disabled] {
......
......@@ -13,13 +13,24 @@
{{#poll.poll_answers}}
<div class="result-row">
<input type="radio" name="vote" value="{{id}}"/>
<label>{{answer}}</label>
{{#if isCurrentUserVote}}
<label>
{{answer}}
<span class="label label-primary">{{t "poll.your_vote"}}</span>
</label>
{{else}}
<label>{{answer}}</label>
{{/if}}
<div class="poll-result pull-right">
<span class="percentage"></span>
({{t "poll.answer_count" count=vote_count}})
</div>
<div class="poll_progress_bar_wrapper progress">
<div class="poll_progress_bar bar" data-answerid="{{id}}" style="height: 100%"></div>
{{#if isCurrentUserVote}}
<div class="poll_progress_bar bar users-vote" data-answerid="{{id}}" style="height: 100%"></div>
{{else}}
<div class="poll_progress_bar bar" data-answerid="{{id}}" style="height: 100%"></div>
{{/if}}
</div>
</div>
{{/poll.poll_answers}}
......@@ -38,7 +49,7 @@
{{#if is_reshare }}
<div class="poll_footer">
{{{t "poll.go_to_original_post" original_post_link=original_post_link}}}
{{{t "poll.go_to_original_post" original_post_link=originalPostLink}}}
</div>
{{/if}}
</div>
......
......@@ -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_count
poll_answers.sum("vote_count")
def participation_answer(user)
poll_participations.find_by(author_id: user.person.id)
end
def already_participated?(user)
poll_participations.where(:author_id => user.person.id).present?
def participation_count
poll_answers.sum("vote_count")
end
end
......@@ -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?
......
......@@ -344,3 +344,4 @@ en:
other: "<%=count%> votes"
show_result: "Show result"
close_result: "Hide result"
your_vote: "Your vote"
......@@ -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);
});
});
});
......@@ -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));
},
......
......@@ -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
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment