FEATURE: respect tags_sort_alphabetically setting when display tags (#10889)

Currently, tag labels are displayed in random order.

They should be displayed in alphabetical or popularity order based on SiteSetting (tags_sort_alphabetically)

Meta: https://meta.discourse.org/t/how-to-apply-tag-sorts-by-popularity-to-topic-list-currently-it-seems-only-apply-to-tag-page/163186/7
This commit is contained in:
Krzysztof Kotlarek 2020-10-13 08:23:04 +11:00 committed by GitHub
parent acf5a26058
commit 6be60b0ae5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -11,7 +11,8 @@ module TopicTagsMixin
def tags def tags
# Calling method `pluck` along with `includes` causing N+1 queries # Calling method `pluck` along with `includes` causing N+1 queries
tags = topic.tags.map(&:name) order_setting = SiteSetting.tags_sort_alphabetically ? { name: :asc } : { topic_count: :desc }
tags = topic.tags.order(order_setting).map(&:name)
if scope.is_staff? if scope.is_staff?
tags tags

View File

@ -212,6 +212,30 @@ describe TopicViewSerializer do
end end
end end
describe 'tags order' do
fab!(:tag1) { Fabricate(:tag, name: 'ctag', topic_count: 5) }
fab!(:tag2) { Fabricate(:tag, name: 'btag', topic_count: 9) }
fab!(:tag3) { Fabricate(:tag, name: 'atag', topic_count: 3) }
before do
SiteSetting.tagging_enabled = true
topic.tags << tag1
topic.tags << tag2
topic.tags << tag3
end
it 'tags are automatically sorted by tag popularity' do
json = serialize_topic(topic, user)
expect(json[:tags]).to eq(%w(btag ctag atag))
end
it 'tags can be sorted alphabetically' do
SiteSetting.tags_sort_alphabetically = true
json = serialize_topic(topic, user)
expect(json[:tags]).to eq(%w(atag btag ctag))
end
end
context "with flags" do context "with flags" do
fab!(:post) { Fabricate(:post, topic: topic) } fab!(:post) { Fabricate(:post, topic: topic) }
fab!(:other_post) { Fabricate(:post, topic: topic) } fab!(:other_post) { Fabricate(:post, topic: topic) }