Skip to content

Commit

Permalink
show users vote in poll
Browse files Browse the repository at this point in the history
  • Loading branch information
Rete2 committed Aug 25, 2017
1 parent 95ac7d0 commit 1cd0da8
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 25 deletions.
11 changes: 9 additions & 2 deletions app/assets/javascripts/app/views/poll_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions app/assets/stylesheets/poll.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
height: 100%;
text-align: left;
}

.users-vote {
background-color: $brand-primary;
}
}

.submit[disabled] {
Expand Down
17 changes: 14 additions & 3 deletions app/assets/templates/poll_tpl.jst.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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>
Expand Down
18 changes: 9 additions & 9 deletions app/models/poll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ def enough_poll_answers

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
8 changes: 3 additions & 5 deletions app/presenters/post_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def non_directly_retrieved_attributes
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
}
Expand Down Expand Up @@ -121,10 +121,8 @@ def user_reshare
@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?
Expand Down
1 change: 1 addition & 0 deletions config/locales/javascript/javascript.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,4 @@ en:
other: "<%=count%> votes"
show_result: "Show result"
close_result: "Hide result"
your_vote: "Your vote"
23 changes: 22 additions & 1 deletion spec/javascripts/app/views/poll_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
});
});
6 changes: 3 additions & 3 deletions spec/javascripts/jasmine_helpers/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
},

Expand Down
19 changes: 19 additions & 0 deletions spec/models/poll_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,23 @@
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
13 changes: 13 additions & 0 deletions spec/presenters/post_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@
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
Expand Down

0 comments on commit 1cd0da8

Please sign in to comment.