DEV: ensure compatibilty with stable

This commit is contained in:
Régis Hanol 2020-08-17 16:21:16 +02:00
parent 142b3db6b8
commit 8de0fa9ecf
1 changed files with 17 additions and 15 deletions

View File

@ -69,26 +69,28 @@ after_initialize do
end
end
TopicQuery.results_filter_callbacks << ->(_type, result, user, options) {
result = result.includes(:topic_vote_count)
if TopicQuery.respond_to?(:results_filter_callbacks)
TopicQuery.results_filter_callbacks << ->(_type, result, user, options) {
result = result.includes(:topic_vote_count)
if user
result = result.select("topics.*, COALESCE((SELECT 1 FROM discourse_voting_votes WHERE user_id = #{user.id} AND topic_id = topics.id), 0) AS current_user_voted")
if user
result = result.select("topics.*, COALESCE((SELECT 1 FROM discourse_voting_votes WHERE user_id = #{user.id} AND topic_id = topics.id), 0) AS current_user_voted")
if options[:state] == "my_votes"
result = result.joins("INNER JOIN discourse_voting_votes ON discourse_voting_votes.topic_id = topics.id AND discourse_voting_votes.user_id = #{user.id}")
if options[:state] == "my_votes"
result = result.joins("INNER JOIN discourse_voting_votes ON discourse_voting_votes.topic_id = topics.id AND discourse_voting_votes.user_id = #{user.id}")
end
end
end
if options[:order] == "votes"
sort_dir = (options[:ascending] == "true") ? "ASC" : "DESC"
result = result
.joins("LEFT JOIN discourse_voting_topic_vote_count ON discourse_voting_topic_vote_count.topic_id = topics.id")
.reorder("COALESCE(discourse_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}")
end
if options[:order] == "votes"
sort_dir = (options[:ascending] == "true") ? "ASC" : "DESC"
result = result
.joins("LEFT JOIN discourse_voting_topic_vote_count ON discourse_voting_topic_vote_count.topic_id = topics.id")
.reorder("COALESCE(discourse_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}")
end
result
}
result
}
end
add_to_serializer(:category, :custom_fields) do
object.custom_fields.merge(enable_topic_voting: DiscourseVoting::CategorySetting.find_by(category_id: object.id).present?)