Skip to content

Commit 332948a

Browse files
committed
Add who voted to vote count
1 parent 8332f28 commit 332948a

File tree

8 files changed

+427
-4
lines changed

8 files changed

+427
-4
lines changed

Diff for: LICENSE.txt

+339
Large diffs are not rendered by default.

Diff for: assets/javascripts/discourse/templates/connectors/topic-above-post-stream/topic-title-voting.hbs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{{#if model.can_vote}}
22
<div class="voting title-voting">
33
<div class="voting-wrapper">
4-
<div class="vote-count">{{model.vote_count}}</div>
4+
{{#if siteSettings.feature_voting_show_who_voted}}
5+
<a href {{action "showWhoVoted"}} class="vote-count">{{model.vote_count}}</a>
6+
{{else}}
7+
<div class="vote-count">{{model.vote_count}}</div>
8+
{{/if}}
59
<div class="vote-label">
610
{{#if model.single_vote}}
711
{{i18n 'feature_voting.vote.one'}}
@@ -25,5 +29,10 @@
2529
{{/if}}
2630
{{/if}}
2731
{{/if}}
32+
{{#if siteSettings.feature_voting_show_who_voted}}
33+
{{#popup-menu visible=whoVotedVisible hide="hideWhoVoted" title="feature_voting.vote_title_plural" extraClasses="who-voted-popup-menu"}}
34+
<li>{{mount-widget widget="who-voted" args=model}}</li>
35+
{{/popup-menu}}
36+
{{/if}}
2837
</div>
2938
{{/if}}

Diff for: assets/javascripts/initializers/feature-voting.js.es6

+41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { withPluginApi } from 'discourse/lib/plugin-api';
22
import TopicRoute from 'discourse/routes/topic';
3+
import TopicController from 'discourse/controllers/topic';
4+
import { createWidget } from 'discourse/widgets/widget';
35

46
function startVoting(api){
57
TopicRoute.reopen({
@@ -39,6 +41,45 @@ function startVoting(api){
3941
}
4042
}
4143
})
44+
45+
TopicController.reopen({
46+
actions: {
47+
showWhoVoted() {
48+
this.set('whoVotedVisible', true);
49+
},
50+
51+
hideWhoVoted() {
52+
this.set('whoVotedVisible', false);
53+
}
54+
}
55+
})
56+
57+
createWidget('who-voted', {
58+
tagName: 'div.who-voted',
59+
60+
html(attrs, state) {
61+
const contents = this.attach('small-user-list', {
62+
users: this.getWhoVoted(),
63+
addSelf: attrs.liked,
64+
listClassName: 'who-voted',
65+
description: 'feature_voting.who_voted'
66+
})
67+
return contents;
68+
},
69+
70+
getWhoVoted() {
71+
const { attrs, state } = this;
72+
var users = attrs.who_voted;
73+
return state.whoVotedUsers = users.map(whoVotedAvatars);
74+
}
75+
});
76+
}
77+
78+
function whoVotedAvatars(user) {
79+
return { template: user.user.avatar_template,
80+
username: user.user.username,
81+
post_url: user.user.post_url,
82+
url: Discourse.getURL('/users/') + user.user.username_lower };
4283
}
4384

4485
export default {

Diff for: assets/stylesheets/feature-voting.scss

+16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
}
2323
.title-voting{
2424
padding-top:14px;
25+
position: relative;
2526
}
2627
.vote-button{
2728
background-color: $tertiary;
@@ -48,4 +49,19 @@
4849
line-height: 20px;
4950
cursor: pointer;
5051
}
52+
}
53+
.who-voted-popup-menu {
54+
position: absolute !important;
55+
left: 90px !important;
56+
top: 45px !important;
57+
58+
h3 {
59+
display: none;
60+
}
61+
ul{
62+
margin: 0px;
63+
}
64+
}
65+
.who-voted .trigger-user-card img {
66+
margin: 2px 0px;
5167
}

Diff for: config/locales/client.en.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ en:
99
voting_limit: "Limit"
1010
vote:
1111
one: "vote"
12-
multiple: "votes"
12+
multiple: "votes"
13+
who_voted: "voted for this"

Diff for: config/locales/server.en.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
en:
22
site_settings:
33
feature_voting_enabled: 'Allow users to vote on topics?'
4-
feature_voting_vote_limit: 'How many active votes are users allowed?'
4+
feature_voting_vote_limit: 'How many active votes are users allowed?'
5+
feature_voting_show_who_voted: 'Allow users to see who voted?'

Diff for: config/settings.yml

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ plugins:
44
client: true
55
feature_voting_vote_limit:
66
default: 10
7+
client: true
8+
feature_voting_show_who_voted:
9+
default: true
710
client: true

Diff for: plugin.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
require_dependency 'topic_view_serializer'
1818
class ::TopicViewSerializer
19-
attributes :can_vote, :single_vote, :vote_count, :user_voted
19+
attributes :can_vote, :single_vote, :vote_count, :user_voted, :who_voted
2020

2121
def can_vote
2222
return object.topic.category.custom_fields["enable_topic_voting"]
@@ -43,6 +43,14 @@ def user_voted
4343
return false
4444
end
4545
end
46+
47+
def who_voted
48+
users = []
49+
User.where(id: object.topic.who_voted).each do |user|
50+
users.push(UserSerializer.new(user, scope: scope, root: 'user'))
51+
end
52+
return users
53+
end
4654
end
4755

4856
add_to_serializer(:topic_list_item, :vote_count) { object.vote_count }
@@ -142,6 +150,11 @@ def vote_count
142150
end
143151
end
144152

153+
def who_voted
154+
user_ids = UserCustomField.where(name: "votes", value: self.id).pluck(:user_id)
155+
return user_ids
156+
end
157+
145158
end
146159

147160
require_dependency 'list_controller'

0 commit comments

Comments
 (0)