FEATURE: add votes tab for on "votable" categories

This commit is contained in:
Sam 2017-03-06 15:00:56 -05:00
parent 619737d209
commit bddea3ce7c
5 changed files with 45 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import property from 'ember-addons/ember-computed-decorators';
import computed from 'ember-addons/ember-computed-decorators';
import Category from 'discourse/models/category';
import { withPluginApi } from 'discourse/lib/plugin-api';
@ -46,7 +46,12 @@ export default {
Category.reopen({
@property('custom_fields.enable_topic_voting')
@computed("url")
votesUrl(url) {
return `${url}/l/votes`;
},
@computed('custom_fields.enable_topic_voting')
enable_topic_voting: {
get(enableField) {
return enableField === "true";

View File

@ -0,0 +1 @@
<a title="{{I18n "feature_voting.votes_nav_help"}}" href="{{category.votesUrl}}">{{I18n "feature_voting.vote_title_plural"}}</a>

View File

@ -0,0 +1,16 @@
export default {
votedPath(){
return "foobar";
},
setupComponent(args, component) {
const filterMode = args.filterMode;
// no endsWith in IE
if (filterMode && filterMode.indexOf("votes", filterMode.length - 5) !== -1) {
component.set("classNames", ["active"]);
}
},
shouldRender(args, component) {
const category = component.get("category");
return !!(category && category.get("can_vote"));
}
};

View File

@ -1,6 +1,7 @@
en:
js:
feature_voting:
votes_nav_help: "topics with the most votes"
voted: "You voted on this topic"
allow_topic_voting: "Allow users to vote on topics in this category"
vote_title: "Vote"

View File

@ -15,6 +15,20 @@ Discourse.anonymous_filters.push(:votes)
after_initialize do
require_dependency 'basic_category_serializer'
class ::BasicCategorySerializer
attributes :can_vote
def include_can_vote?
Category.can_vote?(object.id)
end
def can_vote
true
end
end
require_dependency 'post_serializer'
class ::PostSerializer
attributes :can_vote
@ -71,6 +85,8 @@ after_initialize do
@allowed_voting_cache = DistributedCache.new("allowed_voting")
def self.can_vote?(category_id)
return false unless SiteSetting.feature_voting_enabled
unless set = @allowed_voting_cache["allowed"]
set = reset_voting_cache
end
@ -189,7 +205,10 @@ after_initialize do
end
def list_votes
create_list(:votes, {order: "votes"})
create_list(:votes, unordered: true) do |topics|
topics.joins("left join topic_custom_fields tfv ON tfv.topic_id = topics.id AND tfv.name = 'vote_count'")
.order("coalesce(tfv.value,'0')::integer desc, topics.bumped_at desc")
end
end
end