show users vote in poll

closes #7550
This commit is contained in:
Rete2 2017-08-07 12:45:46 +02:00 committed by Benjamin Neff
parent e08440d7ba
commit b556ad5211
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
13 changed files with 99 additions and 25 deletions

View file

@ -15,6 +15,7 @@
## Features ## Features
* Ask for confirmation when leaving a submittable comment field [#7530](https://github.com/diaspora/diaspora/pull/7530) * 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 # 0.7.0.0

View file

@ -17,15 +17,22 @@ app.views.Poll = app.views.Base.extend({
var isReshare = (this.model.get('post_type') === 'Reshare'); var isReshare = (this.model.get('post_type') === 'Reshare');
var showForm = defaultPresenter.loggedIn && var showForm = defaultPresenter.loggedIn &&
!isReshare && !isReshare &&
!this.model.get('already_participated_in_poll'); !this.model.get("poll_participation_answer_id");
var originalPostLink = isReshare && this.model.get('root') ? 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>' : '<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, { return _.extend(defaultPresenter, {
show_form: showForm, show_form: showForm,
is_reshare: isReshare, is_reshare: isReshare,
original_post_link: originalPostLink originalPostLink: originalPostLink
}); });
}, },

View file

@ -6,7 +6,5 @@ body {
.left-navbar .hoverable:hover { background-color: $main-color-essence; } .left-navbar .hoverable:hover { background-color: $main-color-essence; }
.poll_form .progress .bar { background-color: $main-color-dark; }
.badge { background-color: $brand-primary; } .badge { background-color: $brand-primary; }
} }

View file

@ -22,6 +22,7 @@ body {
.poll_form .progress { .poll_form .progress {
background-color: $gray-dark; background-color: $gray-dark;
.bar { background-color: $gray-light; } .bar { background-color: $gray-light; }
.users-vote { background-color: $brand-primary; }
} }
.stream-element .collapsible { .stream-element .collapsible {

View file

@ -26,6 +26,10 @@
height: 100%; height: 100%;
text-align: left; text-align: left;
} }
.users-vote {
background-color: $brand-primary;
}
} }
.submit[disabled] { .submit[disabled] {

View file

@ -13,13 +13,24 @@
{{#poll.poll_answers}} {{#poll.poll_answers}}
<div class="result-row"> <div class="result-row">
<input type="radio" name="vote" value="{{id}}"/> <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"> <div class="poll-result pull-right">
<span class="percentage"></span> <span class="percentage"></span>
({{t "poll.answer_count" count=vote_count}}) ({{t "poll.answer_count" count=vote_count}})
</div> </div>
<div class="poll_progress_bar_wrapper progress"> <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>
</div> </div>
{{/poll.poll_answers}} {{/poll.poll_answers}}
@ -38,7 +49,7 @@
{{#if is_reshare }} {{#if is_reshare }}
<div class="poll_footer"> <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> </div>
{{/if}} {{/if}}
</div> </div>

View file

@ -21,19 +21,19 @@ class Poll < ApplicationRecord
def as_json(options={}) def as_json(options={})
{ {
:poll_id => self.id, poll_id: id,
:post_id => self.status_message.id, post_id: status_message.id,
:question => self.question, question: question,
:poll_answers => self.poll_answers, poll_answers: poll_answers,
:participation_count => self.participation_count, participation_count: participation_count
} }
end end
def participation_answer(user)
poll_participations.find_by(author_id: user.person.id)
end
def participation_count def participation_count
poll_answers.sum("vote_count") poll_answers.sum("vote_count")
end end
def already_participated?(user)
poll_participations.where(:author_id => user.person.id).present?
end
end end

View file

@ -67,7 +67,7 @@ class PostPresenter < BasePresenter
title: title, title: title,
location: @post.post_location, location: @post.post_location,
poll: @post.poll, poll: @post.poll,
already_participated_in_poll: already_participated_in_poll, poll_participation_answer_id: poll_participation_answer_id,
participation: participate?, participation: participate?,
interactions: build_interactions_json interactions: build_interactions_json
} }
@ -121,10 +121,8 @@ class PostPresenter < BasePresenter
@post.reshare_for(current_user).try(:as_api_response, :backbone) @post.reshare_for(current_user).try(:as_api_response, :backbone)
end end
def already_participated_in_poll def poll_participation_answer_id
if @post.poll && user_signed_in? @post.poll&.participation_answer(current_user)&.poll_answer_id if user_signed_in?
@post.poll.already_participated?(current_user)
end
end end
def participate? def participate?

View file

@ -344,3 +344,4 @@ en:
other: "<%=count%> votes" other: "<%=count%> votes"
show_result: "Show result" show_result: "Show result"
close_result: "Hide result" close_result: "Hide result"
your_vote: "Your vote"

View file

@ -65,7 +65,7 @@ describe("app.views.Poll", function(){
expect(this.view.$('form').length).toBe(1); expect(this.view.$('form').length).toBe(1);
}); });
it("hides vote form when user voted before", function(){ 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(); this.view.render();
expect(this.view.$('form').length).toBe(0); expect(this.view.$('form').length).toBe(0);
}); });
@ -75,4 +75,25 @@ describe("app.views.Poll", function(){
expect(this.view.$('form').length).toBe(0); 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);
});
});
}); });

View file

@ -211,9 +211,9 @@ var factory = {
}, },
postWithPoll : function(overrides) { postWithPoll : function(overrides) {
var defaultAttrs = _.extend(factory.postAttrs(), {"author" : this.author()}); var defaultAttrs = _.extend(factory.postAttrs(), {"author": this.author()});
defaultAttrs = _.extend(defaultAttrs, {"already_participated_in_poll" : false}); defaultAttrs = _.extend(defaultAttrs, {"poll_participation_answer_id": null});
defaultAttrs = _.extend(defaultAttrs, {"poll" : factory.poll()}); defaultAttrs = _.extend(defaultAttrs, {"poll": factory.poll()});
return new app.models.Post(_.extend(defaultAttrs, overrides)); return new app.models.Post(_.extend(defaultAttrs, overrides));
}, },

View file

@ -20,4 +20,23 @@ describe Poll, type: :model do
expect(poll.errors).to have_key(:question) expect(poll.errors).to have_key(:question)
end end
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 end

View file

@ -135,6 +135,19 @@ describe PostPresenter do
presenter = PostPresenter.new(status_message_with_poll) presenter = PostPresenter.new(status_message_with_poll)
expect(presenter.as_json).to be_a(Hash) expect(presenter.as_json).to be_a(Hash)
end 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 end
describe "#tags" do describe "#tags" do