From 3543ec933083e76ceb037c59dabfb38acfbad9c6 Mon Sep 17 00:00:00 2001 From: Joe Buhlig Date: Fri, 18 Mar 2016 09:29:05 -0500 Subject: [PATCH] Check category for custom fields --- .../topic-above-post-stream/topic-title-voting.hbs | 6 +++++- .../javascripts/initializers/feature-voting.js.es6 | 7 ++++++- plugin.rb | 12 ++++++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/topic-title-voting.hbs b/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/topic-title-voting.hbs index 0c5df1c..67a9c46 100644 --- a/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/topic-title-voting.hbs +++ b/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/topic-title-voting.hbs @@ -2,7 +2,11 @@
{{#if siteSettings.feature_voting_show_who_voted}} - {{model.vote_count}} + {{#if model.has_votes}} + {{model.vote_count}} + {{else}} +
{{model.vote_count}}
+ {{/if}} {{else}}
{{model.vote_count}}
{{/if}} diff --git a/assets/javascripts/initializers/feature-voting.js.es6 b/assets/javascripts/initializers/feature-voting.js.es6 index e51e74e..99fbee0 100644 --- a/assets/javascripts/initializers/feature-voting.js.es6 +++ b/assets/javascripts/initializers/feature-voting.js.es6 @@ -70,7 +70,12 @@ function startVoting(api){ getWhoVoted() { const { attrs, state } = this; var users = attrs.who_voted; - return state.whoVotedUsers = users.map(whoVotedAvatars); + if (users.length){ + return state.whoVotedUsers = users.map(whoVotedAvatars); + } + else{ + return state.whoVotedUsers = []; + } } }); } diff --git a/plugin.rb b/plugin.rb index 23de9c4..d85af1a 100755 --- a/plugin.rb +++ b/plugin.rb @@ -16,10 +16,10 @@ after_initialize do require_dependency 'topic_view_serializer' class ::TopicViewSerializer - attributes :can_vote, :single_vote, :vote_count, :user_voted, :who_voted + attributes :can_vote, :single_vote, :vote_count, :has_votes, :user_voted, :who_voted def can_vote - return object.topic.category.custom_fields["enable_topic_voting"] + return object.topic.category.respond_to?(:custom_fields) ? !!object.topic.category.custom_fields["enable_topic_voting"] : false end def single_vote @@ -34,6 +34,14 @@ after_initialize do object.topic.vote_count end + def has_votes + if object.topic.vote_count.to_i > 0 + return true + else + return false + end + end + def user_voted user = scope.user if user && user.custom_fields["votes"]